From 51fac3f3b6f73b649ba8109b37d8ff311b905cd4 Mon Sep 17 00:00:00 2001 From: Daniel Hader Date: Tue, 2 Jun 2026 20:37:40 -0500 Subject: problem statement on submission page and submission logic tweaks --- src/routes/auth.rs | 6 ++++-- src/routes/errors.rs | 4 ++++ src/routes/problem.rs | 13 ++++++++++++- src/routes/submission.rs | 31 +++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 3 deletions(-) create mode 100644 src/routes/submission.rs (limited to 'src/routes') diff --git a/src/routes/auth.rs b/src/routes/auth.rs index ab7a393..8e8d99b 100644 --- a/src/routes/auth.rs +++ b/src/routes/auth.rs @@ -18,9 +18,10 @@ use crate::routes::errors::RouteError; #[derive(Serialize, Deserialize)] pub struct Claims { - pub sub: String, + pub sub: i64, pub exp: usize, pub iat: usize, + pub email: String, pub username: String, pub is_admin: bool, } @@ -90,9 +91,10 @@ pub async fn login( .as_secs() as usize; let claims = Claims { - sub: user.email().to_string(), + sub: user.id(), iat: now, exp: now + 60 * 60 * 24, + email: user.email().to_string(), username: user.username().to_string(), is_admin: false }; diff --git a/src/routes/errors.rs b/src/routes/errors.rs index 78b4e3a..f6a0901 100644 --- a/src/routes/errors.rs +++ b/src/routes/errors.rs @@ -7,6 +7,7 @@ pub enum RouteError { UserCreateUsernameExists(String), UnregisteredEmail(String), AuthorizationFailure(), + NotFound(String), } impl IntoResponse for RouteError { @@ -26,6 +27,9 @@ impl IntoResponse for RouteError { }, RouteError::AuthorizationFailure() => { (StatusCode::UNAUTHORIZED, format!("failed to authorize")) + }, + RouteError::NotFound(resource) => { + (StatusCode::NOT_FOUND, format!("{resource} not found")) } }; diff --git a/src/routes/problem.rs b/src/routes/problem.rs index 9b2eba3..69d1b66 100644 --- a/src/routes/problem.rs +++ b/src/routes/problem.rs @@ -1,4 +1,4 @@ -use axum::{extract::State, http::StatusCode, response::{IntoResponse, Json}}; +use axum::{extract::{Path, State}, http::StatusCode, response::{IntoResponse, Json}}; use serde::Deserialize; use crate::{AppState, routes::errors::RouteError}; @@ -31,3 +31,14 @@ pub async fn get_problems( 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)) + } +} diff --git a/src/routes/submission.rs b/src/routes/submission.rs new file mode 100644 index 0000000..50bb53d --- /dev/null +++ b/src/routes/submission.rs @@ -0,0 +1,31 @@ +use axum::{Json, extract::State, http::StatusCode, response::IntoResponse}; +use serde::Deserialize; + +use crate::{AppState, routes::{auth::AuthUser, errors::RouteError}}; + +#[derive(Deserialize)] +pub(crate) struct CreateSubmissionRequest { + problem_id: i64, + language: String, + details: String, + code: String, +} + +pub async fn create_submission( + State(state): State, + AuthUser(claims): AuthUser, + Json(request): Json +) -> Result { + let user_id = claims.sub; + + match state.database.insert_submission( + user_id, + request.problem_id, + &request.language, + &request.details, + &request.code + ) { + Ok(submission) => Ok((StatusCode::CREATED, Json(submission))), + Err(_) => Err(RouteError::Internal(format!("unable to insert submission"))) + } +} -- cgit v1.2.3