working pages
This commit is contained in:
@@ -44,6 +44,7 @@ CREATE TABLE listings (
|
|||||||
phone VARCHAR(20),
|
phone VARCHAR(20),
|
||||||
online_ordering VARCHAR(20) NOT NULL DEFAULT 'none',
|
online_ordering VARCHAR(20) NOT NULL DEFAULT 'none',
|
||||||
county_id INTEGER NOT NULL,
|
county_id INTEGER NOT NULL,
|
||||||
|
town VARCHAR(100),
|
||||||
user_id INTEGER NOT NULL,
|
user_id INTEGER NOT NULL,
|
||||||
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
||||||
last_edited TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
|
last_edited TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ use axum::{
|
|||||||
use crate::auth::structs::{AppState, User};
|
use crate::auth::structs::{AppState, User};
|
||||||
use crate::company::structs::Company;
|
use crate::company::structs::Company;
|
||||||
use crate::listing::structs::{Listing, CreateListingRequest, UpdateListingRequest};
|
use crate::listing::structs::{Listing, CreateListingRequest, UpdateListingRequest};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::FromRow;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
pub async fn get_listings(
|
pub async fn get_listings(
|
||||||
@@ -13,7 +15,7 @@ pub async fn get_listings(
|
|||||||
Extension(user): Extension<User>,
|
Extension(user): Extension<User>,
|
||||||
) -> Result<Json<Vec<Listing>>, (StatusCode, Json<serde_json::Value>)> {
|
) -> Result<Json<Vec<Listing>>, (StatusCode, Json<serde_json::Value>)> {
|
||||||
match sqlx::query_as::<_, Listing>(
|
match sqlx::query_as::<_, Listing>(
|
||||||
"SELECT id, company_name, is_active, price_per_gallon, price_per_gallon_cash, note, minimum_order, service, bio_percent, phone, online_ordering, county_id, user_id, last_edited FROM listings WHERE user_id = $1 ORDER BY id DESC"
|
"SELECT id, company_name, is_active, price_per_gallon, price_per_gallon_cash, note, minimum_order, service, bio_percent, phone, online_ordering, county_id, town, user_id, last_edited FROM listings WHERE user_id = $1 ORDER BY id DESC"
|
||||||
)
|
)
|
||||||
.bind(user.id)
|
.bind(user.id)
|
||||||
.fetch_all(&*app_state.db)
|
.fetch_all(&*app_state.db)
|
||||||
@@ -33,7 +35,7 @@ pub async fn get_listing_by_id(
|
|||||||
Extension(user): Extension<User>,
|
Extension(user): Extension<User>,
|
||||||
) -> Result<Json<Listing>, (StatusCode, Json<serde_json::Value>)> {
|
) -> Result<Json<Listing>, (StatusCode, Json<serde_json::Value>)> {
|
||||||
match sqlx::query_as::<_, Listing>(
|
match sqlx::query_as::<_, Listing>(
|
||||||
"SELECT id, company_name, is_active, price_per_gallon, price_per_gallon_cash, note, minimum_order, service, bio_percent, phone, online_ordering, county_id, user_id, last_edited FROM listings WHERE id = $1 AND user_id = $2"
|
"SELECT id, company_name, is_active, price_per_gallon, price_per_gallon_cash, note, minimum_order, service, bio_percent, phone, online_ordering, county_id, town, user_id, last_edited FROM listings WHERE id = $1 AND user_id = $2"
|
||||||
)
|
)
|
||||||
.bind(listing_id)
|
.bind(listing_id)
|
||||||
.bind(user.id)
|
.bind(user.id)
|
||||||
@@ -62,7 +64,7 @@ pub async fn create_listing(
|
|||||||
|
|
||||||
// Create the listing directly without company validation
|
// Create the listing directly without company validation
|
||||||
match sqlx::query_as::<_, Listing>(
|
match sqlx::query_as::<_, Listing>(
|
||||||
"INSERT INTO listings (company_name, is_active, price_per_gallon, price_per_gallon_cash, note, minimum_order, service, bio_percent, phone, online_ordering, county_id, user_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12) RETURNING id, company_name, is_active, price_per_gallon, price_per_gallon_cash, note, minimum_order, service, bio_percent, phone, online_ordering, county_id, user_id, last_edited"
|
"INSERT INTO listings (company_name, is_active, price_per_gallon, price_per_gallon_cash, note, minimum_order, service, bio_percent, phone, online_ordering, county_id, town, user_id) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13) RETURNING id, company_name, is_active, price_per_gallon, price_per_gallon_cash, note, minimum_order, service, bio_percent, phone, online_ordering, county_id, town, user_id, last_edited"
|
||||||
)
|
)
|
||||||
.bind(&payload.company_name)
|
.bind(&payload.company_name)
|
||||||
.bind(payload.is_active)
|
.bind(payload.is_active)
|
||||||
@@ -75,6 +77,7 @@ pub async fn create_listing(
|
|||||||
.bind(&payload.phone)
|
.bind(&payload.phone)
|
||||||
.bind(&payload.online_ordering)
|
.bind(&payload.online_ordering)
|
||||||
.bind(payload.county_id)
|
.bind(payload.county_id)
|
||||||
|
.bind(&payload.town)
|
||||||
.bind(user.id)
|
.bind(user.id)
|
||||||
.fetch_one(&*app_state.db)
|
.fetch_one(&*app_state.db)
|
||||||
.await
|
.await
|
||||||
@@ -162,7 +165,7 @@ pub async fn update_listing(
|
|||||||
// This is a simplified version - in production, you'd want to build the query more safely
|
// This is a simplified version - in production, you'd want to build the query more safely
|
||||||
// For now, let's use a simpler approach
|
// For now, let's use a simpler approach
|
||||||
match sqlx::query_as::<_, Listing>(
|
match sqlx::query_as::<_, Listing>(
|
||||||
"UPDATE listings SET company_name = COALESCE($1, company_name), is_active = COALESCE($2, is_active), price_per_gallon = COALESCE($3, price_per_gallon), price_per_gallon_cash = COALESCE($4, price_per_gallon_cash), note = COALESCE($5, note), minimum_order = COALESCE($6, minimum_order), service = COALESCE($7, service), bio_percent = COALESCE($8, bio_percent), phone = COALESCE($9, phone), online_ordering = COALESCE($10, online_ordering), county_id = COALESCE($11, county_id), last_edited = CURRENT_TIMESTAMP WHERE id = $12 AND user_id = $13 RETURNING id, company_name, is_active, price_per_gallon, price_per_gallon_cash, note, minimum_order, service, bio_percent, phone, online_ordering, county_id, user_id, last_edited"
|
"UPDATE listings SET company_name = COALESCE($1, company_name), is_active = COALESCE($2, is_active), price_per_gallon = COALESCE($3, price_per_gallon), price_per_gallon_cash = COALESCE($4, price_per_gallon_cash), note = COALESCE($5, note), minimum_order = COALESCE($6, minimum_order), service = COALESCE($7, service), bio_percent = COALESCE($8, bio_percent), phone = COALESCE($9, phone), online_ordering = COALESCE($10, online_ordering), county_id = COALESCE($11, county_id), town = COALESCE($12, town), last_edited = CURRENT_TIMESTAMP WHERE id = $13 AND user_id = $14 RETURNING id, company_name, is_active, price_per_gallon, price_per_gallon_cash, note, minimum_order, service, bio_percent, phone, online_ordering, county_id, town, user_id, last_edited"
|
||||||
)
|
)
|
||||||
.bind(&payload.company_name)
|
.bind(&payload.company_name)
|
||||||
.bind(payload.is_active)
|
.bind(payload.is_active)
|
||||||
@@ -175,6 +178,7 @@ pub async fn update_listing(
|
|||||||
.bind(&payload.phone)
|
.bind(&payload.phone)
|
||||||
.bind(&payload.online_ordering)
|
.bind(&payload.online_ordering)
|
||||||
.bind(payload.county_id)
|
.bind(payload.county_id)
|
||||||
|
.bind(&payload.town)
|
||||||
.bind(listing_id)
|
.bind(listing_id)
|
||||||
.bind(user.id)
|
.bind(user.id)
|
||||||
.fetch_optional(&*app_state.db)
|
.fetch_optional(&*app_state.db)
|
||||||
@@ -197,7 +201,7 @@ pub async fn get_listings_by_county(
|
|||||||
Path(county_id): Path<i32>,
|
Path(county_id): Path<i32>,
|
||||||
) -> Result<Json<Vec<Listing>>, (StatusCode, Json<serde_json::Value>)> {
|
) -> Result<Json<Vec<Listing>>, (StatusCode, Json<serde_json::Value>)> {
|
||||||
match sqlx::query_as::<_, Listing>(
|
match sqlx::query_as::<_, Listing>(
|
||||||
"SELECT id, company_name, is_active, price_per_gallon, price_per_gallon_cash, note, minimum_order, service, bio_percent, phone, online_ordering, county_id, user_id, last_edited FROM listings WHERE county_id = $1 AND is_active = true ORDER BY last_edited DESC"
|
"SELECT id, company_name, is_active, price_per_gallon, price_per_gallon_cash, note, minimum_order, service, bio_percent, phone, online_ordering, county_id, town, user_id, last_edited FROM listings WHERE county_id = $1 AND is_active = true ORDER BY last_edited DESC"
|
||||||
)
|
)
|
||||||
.bind(county_id)
|
.bind(county_id)
|
||||||
.fetch_all(&*app_state.db)
|
.fetch_all(&*app_state.db)
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ pub struct Listing {
|
|||||||
pub phone: Option<String>,
|
pub phone: Option<String>,
|
||||||
pub online_ordering: String,
|
pub online_ordering: String,
|
||||||
pub county_id: i32,
|
pub county_id: i32,
|
||||||
|
pub town: Option<String>,
|
||||||
pub user_id: i32,
|
pub user_id: i32,
|
||||||
pub last_edited: DateTime<Utc>,
|
pub last_edited: DateTime<Utc>,
|
||||||
}
|
}
|
||||||
@@ -34,6 +35,7 @@ pub struct CreateListingRequest {
|
|||||||
pub phone: Option<String>,
|
pub phone: Option<String>,
|
||||||
pub online_ordering: String,
|
pub online_ordering: String,
|
||||||
pub county_id: i32,
|
pub county_id: i32,
|
||||||
|
pub town: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
@@ -49,4 +51,5 @@ pub struct UpdateListingRequest {
|
|||||||
pub phone: Option<String>,
|
pub phone: Option<String>,
|
||||||
pub online_ordering: Option<String>,
|
pub online_ordering: Option<String>,
|
||||||
pub county_id: Option<i32>,
|
pub county_id: Option<i32>,
|
||||||
|
pub town: Option<String>,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user