new get/post prize endpoints

This commit is contained in:
Mitchell Marino 2023-04-16 22:31:15 -05:00
parent f8fc5d3dce
commit 234ae29e96
3 changed files with 94 additions and 1 deletions

View File

@ -68,6 +68,7 @@ async fn main() {
.route("/winners/recent", get(winner::get_recent_winners)) .route("/winners/recent", get(winner::get_recent_winners))
.route("/winners/unclaimed", get(winner::get_unclaimed_winners)) .route("/winners/unclaimed", get(winner::get_unclaimed_winners))
.route("/winners/mark", put(winner::mark_claimed)) .route("/winners/mark", put(winner::mark_claimed))
.route("/prizes", get(winner::get_prizes).post(winner::new_prize))
.with_state(AppState { .with_state(AppState {
db_pool, db_pool,
jwt_encode, jwt_encode,

View File

@ -22,6 +22,23 @@ pub struct PrizeQuery {
pub grade: i32, 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)] #[derive(Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct AttendingEntry { pub struct AttendingEntry {
pub user_id: i32, pub user_id: i32,

View File

@ -9,7 +9,7 @@ use axum_auth::AuthBearer;
use serde_json::json; use serde_json::json;
use sqlx::{query, query_as}; use sqlx::{query, query_as};
use crate::models::{WinnerEntry, WinnerQuery}; use crate::models::{NewPrize, PrizeEntry, WinnerEntry, WinnerQuery};
use crate::{ use crate::{
jwt::handle_token, jwt::handle_token,
models::{PrizeQuery, Role}, models::{PrizeQuery, Role},
@ -223,3 +223,78 @@ pub async fn mark_claimed(
} }
.into_response() .into_response()
} }
pub async fn get_prizes(
AuthBearer(token): AuthBearer,
State(app_state): State<AppState>,
) -> 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<AppState>,
Json(new_prize): Json<NewPrize>,
) -> 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()
}