From 970638e3c895ba518c7a70f56f385c9dcdce8b5d Mon Sep 17 00:00:00 2001 From: Daniel Hader Date: Tue, 12 May 2026 13:46:39 -0500 Subject: create and get problem endpoints added --- src/routes/problem.rs | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'src/routes/problem.rs') 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> { - let problem = Problem { - title: "test problem".into(), - description: "the description of a test problem".into(), +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())); }; - Json(vec![problem]) + + 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))) +} -- cgit v1.2.3