use axum::{ extract::{Query, State}, Json, }; use serde::Deserialize; use serde_json::{json, Value}; use crate::{ app::AppState, error::AppResult, models::market::{BuyListingRequest, SellCardRequest}, services::{club as club_svc, market as market_svc, profile as profile_svc}, }; #[derive(Deserialize)] pub struct MarketQuery { pub min_overall: Option, pub position: Option, } pub async fn get_market( State(state): State, Query(query): Query, ) -> AppResult> { let all = market_svc::get_active_listings(&state.pool, &state.card_db).await?; let listings: Vec<_> = all .into_iter() .filter(|l| { query .min_overall .map(|min| l.card.overall >= min) .unwrap_or(true) && query .position .as_ref() .map(|p| l.card.position.eq_ignore_ascii_case(p)) .unwrap_or(true) }) .collect(); Ok(Json( json!({ "listings": listings, "total": listings.len() }), )) } pub async fn post_market_buy( State(state): State, Json(req): Json, ) -> AppResult> { let profile = profile_svc::get_active_profile(&state.pool).await?; let club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?; let card = market_svc::buy_listing(&state.pool, &state.card_db, &club.id, &req).await?; Ok(Json( json!({ "purchased_card": card, "message": "Card purchased successfully" }), )) } pub async fn post_market_sell( State(state): State, Json(req): Json, ) -> AppResult> { let profile = profile_svc::get_active_profile(&state.pool).await?; let club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?; let new_balance = market_svc::sell_card(&state.pool, &club.id, &req).await?; Ok(Json( json!({ "new_coin_balance": new_balance, "message": "Card sold to NPC market" }), )) } pub async fn post_market_refresh(State(state): State) -> AppResult> { let count = market_svc::refresh_npc_listings(&state.pool, &state.card_db, &state.event_defs).await?; Ok(Json(json!({ "listings_generated": count }))) }