From 234ae29e96450f4599c166d6e8277631650833af Mon Sep 17 00:00:00 2001 From: Mitchell Marino Date: Sun, 16 Apr 2023 22:31:15 -0500 Subject: [PATCH] new get/post prize endpoints --- src/main.rs | 1 + src/models.rs | 17 ++++++++++++ src/winner.rs | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 071b03d..6a85872 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,6 +68,7 @@ async fn main() { .route("/winners/recent", get(winner::get_recent_winners)) .route("/winners/unclaimed", get(winner::get_unclaimed_winners)) .route("/winners/mark", put(winner::mark_claimed)) + .route("/prizes", get(winner::get_prizes).post(winner::new_prize)) .with_state(AppState { db_pool, jwt_encode, diff --git a/src/models.rs b/src/models.rs index 1c47c1f..0ec999b 100644 --- a/src/models.rs +++ b/src/models.rs @@ -22,6 +22,23 @@ pub struct PrizeQuery { pub grade: i32, } +#[derive(Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +pub struct PrizeEntry { + pub id: i32, + pub name: String, + pub points_min: i32, + pub description: String, + pub directions_to_claim: String, +} + +#[derive(Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +pub struct NewPrize { + pub name: String, + pub points_min: i32, + pub description: String, + pub directions_to_claim: String, +} + #[derive(Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] pub struct AttendingEntry { pub user_id: i32, diff --git a/src/winner.rs b/src/winner.rs index 822e526..4103d65 100644 --- a/src/winner.rs +++ b/src/winner.rs @@ -9,7 +9,7 @@ use axum_auth::AuthBearer; use serde_json::json; use sqlx::{query, query_as}; -use crate::models::{WinnerEntry, WinnerQuery}; +use crate::models::{NewPrize, PrizeEntry, WinnerEntry, WinnerQuery}; use crate::{ jwt::handle_token, models::{PrizeQuery, Role}, @@ -223,3 +223,78 @@ pub async fn mark_claimed( } .into_response() } + +pub async fn get_prizes( + AuthBearer(token): AuthBearer, + State(app_state): State, +) -> Response { + if let Err(err) = handle_token(token, &app_state, Role::Student) { + return err; + }; + + let result = query_as!( + PrizeEntry, + r#" + SELECT + * + FROM prizes + "#, + ) + .fetch_all(&app_state.db_pool) + .await; + + match result { + Ok(prizes) => (StatusCode::OK, Json(json!(prizes))), + Err(err) => ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(json!({ + "error": format!("Unknown error getting winners: {:?}", err) + })), + ), + } + .into_response() +} + +pub async fn new_prize( + AuthBearer(token): AuthBearer, + State(app_state): State, + Json(new_prize): Json, +) -> Response { + if let Err(err) = handle_token(token, &app_state, Role::Student) { + return err; + }; + + let result = query_as!( + PrizeEntry, + r#" + INSERT INTO prizes (name, points_min, description, directions_to_claim) + VALUES ($1, $2, $3, $4) + "#, + new_prize.name, + new_prize.points_min, + new_prize.description, + new_prize.directions_to_claim, + ) + .execute(&app_state.db_pool) + .await; + + match result { + Ok(result) => { + if result.rows_affected() == 1 { + (StatusCode::OK, Json(json!({}))) + } else { + ( + StatusCode::BAD_REQUEST, + Json(json!({"error": "Failed to insert prize."})), + ) + } + } + Err(err) => ( + StatusCode::INTERNAL_SERVER_ERROR, + Json(json!({ + "error": format!("Unknown error getting winners: {:?}", err) + })), + ), + } + .into_response() +}