diff options
Diffstat (limited to 'src/routes/problem.rs')
| -rw-r--r-- | src/routes/problem.rs | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/src/routes/problem.rs b/src/routes/problem.rs index caeb808..9b2eba3 100644 --- a/src/routes/problem.rs +++ b/src/routes/problem.rs @@ -1,17 +1,33 @@ -use axum::response::Json; -use serde::Serialize; +use axum::{extract::State, http::StatusCode, response::{IntoResponse, Json}}; +use serde::Deserialize; -#[derive(Serialize)] -pub struct Problem { +use crate::{AppState, routes::errors::RouteError}; + +#[derive(Deserialize)] +pub struct CreateProblemRequest { title: String, description: String, } -pub async fn get_problems() -> Json<Vec<Problem>> { - let problem = Problem { - title: "test problem".into(), - description: "the description of a test problem".into(), +pub async fn create_problem( + State(state): State<AppState>, + Json(request): Json<CreateProblemRequest> +) -> Result<impl IntoResponse, RouteError> { + + let Ok(problem) = state.database.insert_problem(&request.title, &request.description) else { + return Err(RouteError::Internal("database action failed".into())); }; - Json(vec![problem]) + + Ok((StatusCode::CREATED, Json(problem))) } +pub async fn get_problems( + State(state): State<AppState>, +) -> Result<impl IntoResponse, RouteError> { + + let Ok(problems) = state.database.fetch_problems() else { + return Err(RouteError::Internal("database action failed".into())); + }; + + Ok((StatusCode::CREATED, Json(problems))) +} |
