use axum::{ extract::{Path, State}, Json, }; use serde_json::{json, Value}; use crate::{ app::AppState, error::{AppError, AppResult}, models::sbc::{SbcResult, SubmitSbcRequest}, services::{club as club_svc, profile as profile_svc, sbc as sbc_svc}, }; pub async fn get_sbcs(State(state): State) -> AppResult> { Ok(Json(json!({ "sbcs": state.sbc_defs }))) } pub async fn get_sbc( State(state): State, Path(sbc_id): Path, ) -> AppResult> { let sbc = state .sbc_defs .iter() .find(|s| s.id == sbc_id) .ok_or_else(|| AppError::NotFound(format!("SBC '{sbc_id}' not found")))?; Ok(Json(json!({ "sbc": sbc }))) } pub async fn post_sbc_submit( 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 result = sbc_svc::submit_sbc( &state.pool, &state.card_db, &state.sbc_defs, &state.obj_defs, &profile.id, &club.id, &req, ) .await?; if result.passed { let _ = crate::services::achievement::check_and_unlock( &state.pool, &state.achievement_defs, &profile.id, &club.id, ).await; } Ok(Json(result)) }