summaryrefslogtreecommitdiff
path: root/src/routes/problem.rs
diff options
context:
space:
mode:
authorDaniel Hader <[email protected]>2026-05-12 13:46:39 -0500
committerDaniel Hader <[email protected]>2026-05-12 13:46:39 -0500
commit970638e3c895ba518c7a70f56f385c9dcdce8b5d (patch)
tree301152e34344da3e7fcf0fe9ea8f7dbe642c9802 /src/routes/problem.rs
parent36fad793c3be58b220ae319a45c8cd8afbae09fa (diff)
create and get problem endpoints added
Diffstat (limited to 'src/routes/problem.rs')
-rw-r--r--src/routes/problem.rs34
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)))
+}