working pages

This commit is contained in:
2025-12-29 21:38:19 -05:00
parent 56ebb8eba2
commit 57b48890ff
3 changed files with 13 additions and 5 deletions

View File

@@ -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<User>,
) -> Result<Json<Vec<Listing>>, (StatusCode, Json<serde_json::Value>)> {
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<User>,
) -> Result<Json<Listing>, (StatusCode, Json<serde_json::Value>)> {
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<i32>,
) -> Result<Json<Vec<Listing>>, (StatusCode, Json<serde_json::Value>)> {
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)

View File

@@ -17,6 +17,7 @@ pub struct Listing {
pub phone: Option<String>,
pub online_ordering: String,
pub county_id: i32,
pub town: Option<String>,
pub user_id: i32,
pub last_edited: DateTime<Utc>,
}
@@ -34,6 +35,7 @@ pub struct CreateListingRequest {
pub phone: Option<String>,
pub online_ordering: String,
pub county_id: i32,
pub town: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
@@ -49,4 +51,5 @@ pub struct UpdateListingRequest {
pub phone: Option<String>,
pub online_ordering: Option<String>,
pub county_id: Option<i32>,
pub town: Option<String>,
}