new get/post prize endpoints
This commit is contained in:
parent
f8fc5d3dce
commit
234ae29e96
@ -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,
|
||||
|
||||
@ -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,
|
||||
|
||||
@ -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<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()
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user