summaryrefslogtreecommitdiff
path: root/src/routes
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/problem.rs34
-rw-r--r--src/routes/user.rs7
2 files changed, 26 insertions, 15 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)))
+}
diff --git a/src/routes/user.rs b/src/routes/user.rs
index 42db46f..f64cb39 100644
--- a/src/routes/user.rs
+++ b/src/routes/user.rs
@@ -2,7 +2,6 @@ use axum::extract::{Json, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use serde::Deserialize;
-use serde_json::json;
use crate::AppState;
use crate::auth::hash_password;
@@ -40,10 +39,6 @@ pub async fn create_user(
return Err(RouteError::Internal("failed to create user".into()));
};
- return Ok((StatusCode::CREATED, Json(json!({
- "id": user.id(),
- "email": user.email(),
- "username": user.username(),
- }))));
+ return Ok((StatusCode::CREATED, Json(user)));
}