events api

This commit is contained in:
Mitchell Marino 2023-04-16 19:59:24 -05:00
parent db6a5414d9
commit 8d08147329
6 changed files with 57 additions and 1 deletions

View File

@ -108,6 +108,55 @@ pub async fn get_all_events(
.into_response() .into_response()
} }
pub async fn my_events(
AuthBearer(token): AuthBearer,
State(app_state): State<AppState>,
) -> impl IntoResponse {
let token_data = match handle_token(token, &app_state, Role::Teacher) {
Err(err) => return err,
Ok(token_data) => token_data,
};
let result = query_as!(
Event,
r#"
SELECT
id,
title,
description,
time_start,
time_end,
event_type AS "event_type!: EventType",
points,
place,
price,
created_by
FROM
events
WHERE
created_by = $1
ORDER BY
time_start
LIMIT
20;
"#,
token_data.id,
)
.fetch_all(&app_state.db_pool)
.await;
match result {
Ok(events) => (StatusCode::OK, Json(json!(events))),
Err(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(json!({
"error": format!("Unknown error getting events: {:?}", err)
})),
),
}
.into_response()
}
pub async fn get_recent_events( pub async fn get_recent_events(
AuthBearer(token): AuthBearer, AuthBearer(token): AuthBearer,
State(app_state): State<AppState>, State(app_state): State<AppState>,

View File

@ -69,7 +69,8 @@ pub async fn list_points(
LEFT JOIN events e LEFT JOIN events e
ON ea.event_id = e.id ON ea.event_id = e.id
WHERE WHERE
u.grade = $1 u.grade = $1 AND
u.role = 'student'
GROUP BY u.id GROUP BY u.id
ORDER BY points DESC ORDER BY points DESC
; ;

View File

@ -56,6 +56,7 @@ async fn main() {
.route("/event/preview", get(events::get_events_preview)) .route("/event/preview", get(events::get_events_preview))
.route("/event/future", get(events::get_all_events)) .route("/event/future", get(events::get_all_events))
.route("/event/recent", get(events::get_recent_events)) .route("/event/recent", get(events::get_recent_events))
.route("/event/my", get(events::my_events))
.route("/report", get(report::get_report)) .route("/report", get(report::get_report))
.route("/attending/confirm", put(attending::confirm_attending)) .route("/attending/confirm", put(attending::confirm_attending))
.route("/attending/mark", post(attending::mark_attending)) .route("/attending/mark", post(attending::mark_attending))

View File

@ -19,6 +19,7 @@ pub struct WinnerEntry {
#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct PrizeQuery { pub struct PrizeQuery {
pub prize_id: Option<i32>, pub prize_id: Option<i32>,
pub grade: i32,
} }
#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]

View File

@ -55,6 +55,7 @@ pub async fn get_report(
ON u.id = ea.user_id AND ea.confirmed = true ON u.id = ea.user_id AND ea.confirmed = true
LEFT JOIN events e LEFT JOIN events e
ON ea.event_id = e.id ON ea.event_id = e.id
WHERE u.role = 'student'
GROUP BY u.id GROUP BY u.id
ORDER BY u.grade, u.username, points ORDER BY u.grade, u.username, points
"#, "#,

View File

@ -31,6 +31,7 @@ pub async fn select_winners(
SELECT SELECT
u.id, u.id,
u.username, u.username,
u.grade,
COALESCE(SUM(e.points), 0) AS points, COALESCE(SUM(e.points), 0) AS points,
RANDOM() * COALESCE(SUM(e.points), 0) AS random_value RANDOM() * COALESCE(SUM(e.points), 0) AS random_value
FROM FROM
@ -50,6 +51,7 @@ pub async fn select_winners(
id, points id, points
FROM weighted_users FROM weighted_users
WHERE points > p.points_min WHERE points > p.points_min
AND grade = $3
LIMIT 1 LIMIT 1
) u ) u
WHERE p.id = $1 OR $2 WHERE p.id = $1 OR $2
@ -61,6 +63,7 @@ pub async fn select_winners(
"#, "#,
prize_query.prize_id.unwrap_or(0), prize_query.prize_id.unwrap_or(0),
prize_query.prize_id.is_none(), prize_query.prize_id.is_none(),
prize_query.grade,
) )
.execute(&app_state.db_pool) .execute(&app_state.db_pool)
.await; .await;