diff --git a/schema.sql b/schema.sql index 8a5452b..03d3b88 100755 --- a/schema.sql +++ b/schema.sql @@ -44,6 +44,7 @@ CREATE TABLE listings ( phone VARCHAR(20), online_ordering VARCHAR(20) NOT NULL DEFAULT 'none', county_id INTEGER NOT NULL, + town VARCHAR(100), user_id INTEGER NOT NULL, created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP, last_edited TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP diff --git a/src/listing/data.rs b/src/listing/data.rs index 47ad729..2479cce 100644 --- a/src/listing/data.rs +++ b/src/listing/data.rs @@ -6,6 +6,8 @@ use axum::{ use crate::auth::structs::{AppState, User}; use crate::company::structs::Company; use crate::listing::structs::{Listing, CreateListingRequest, UpdateListingRequest}; +use serde::{Deserialize, Serialize}; +use sqlx::FromRow; use serde_json::json; pub async fn get_listings( @@ -13,7 +15,7 @@ pub async fn get_listings( Extension(user): Extension, ) -> Result>, (StatusCode, Json)> { 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) .fetch_all(&*app_state.db) @@ -33,7 +35,7 @@ pub async fn get_listing_by_id( Extension(user): Extension, ) -> Result, (StatusCode, Json)> { 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(user.id) @@ -62,7 +64,7 @@ pub async fn create_listing( // Create the listing directly without company validation 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.is_active) @@ -75,6 +77,7 @@ pub async fn create_listing( .bind(&payload.phone) .bind(&payload.online_ordering) .bind(payload.county_id) + .bind(&payload.town) .bind(user.id) .fetch_one(&*app_state.db) .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 // For now, let's use a simpler approach 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.is_active) @@ -175,6 +178,7 @@ pub async fn update_listing( .bind(&payload.phone) .bind(&payload.online_ordering) .bind(payload.county_id) + .bind(&payload.town) .bind(listing_id) .bind(user.id) .fetch_optional(&*app_state.db) @@ -197,7 +201,7 @@ pub async fn get_listings_by_county( Path(county_id): Path, ) -> Result>, (StatusCode, Json)> { 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) .fetch_all(&*app_state.db) diff --git a/src/listing/structs.rs b/src/listing/structs.rs index 5ce936e..c52b3d5 100644 --- a/src/listing/structs.rs +++ b/src/listing/structs.rs @@ -17,6 +17,7 @@ pub struct Listing { pub phone: Option, pub online_ordering: String, pub county_id: i32, + pub town: Option, pub user_id: i32, pub last_edited: DateTime, } @@ -34,6 +35,7 @@ pub struct CreateListingRequest { pub phone: Option, pub online_ordering: String, pub county_id: i32, + pub town: Option, } #[derive(Debug, Serialize, Deserialize)] @@ -49,4 +51,5 @@ pub struct UpdateListingRequest { pub phone: Option, pub online_ordering: Option, pub county_id: Option, + pub town: Option, }