use axum::{extract::{Path, State}, http::StatusCode, response::{IntoResponse, Json}}; use serde::Deserialize; use crate::{AppState, routes::errors::RouteError}; #[derive(Deserialize)] pub struct CreateProblemRequest { title: String, description: String, } pub async fn create_problem( State(state): State, Json(request): Json ) -> Result { let Ok(problem) = state.database.insert_problem(&request.title, &request.description) else { return Err(RouteError::Internal("database action failed".into())); }; Ok((StatusCode::CREATED, Json(problem))) } pub async fn get_problems( State(state): State, ) -> Result { let Ok(problems) = state.database.fetch_problems() else { return Err(RouteError::Internal("database action failed".into())); }; Ok((StatusCode::CREATED, Json(problems))) } pub async fn get_problem( State(state): State, Path(problem_id): Path, ) -> Result { match state.database.fetch_problem(problem_id) { Err(_) => Err(RouteError::Internal("database action failed".into())), Ok(None) => Err(RouteError::NotFound("problem".into())), Ok(Some(problem)) => Ok(Json(problem)) } }