events api
This commit is contained in:
parent
db6a5414d9
commit
8d08147329
@ -108,6 +108,55 @@ pub async fn get_all_events(
|
||||
.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(
|
||||
AuthBearer(token): AuthBearer,
|
||||
State(app_state): State<AppState>,
|
||||
|
||||
@ -69,7 +69,8 @@ pub async fn list_points(
|
||||
LEFT JOIN events e
|
||||
ON ea.event_id = e.id
|
||||
WHERE
|
||||
u.grade = $1
|
||||
u.grade = $1 AND
|
||||
u.role = 'student'
|
||||
GROUP BY u.id
|
||||
ORDER BY points DESC
|
||||
;
|
||||
|
||||
@ -56,6 +56,7 @@ async fn main() {
|
||||
.route("/event/preview", get(events::get_events_preview))
|
||||
.route("/event/future", get(events::get_all_events))
|
||||
.route("/event/recent", get(events::get_recent_events))
|
||||
.route("/event/my", get(events::my_events))
|
||||
.route("/report", get(report::get_report))
|
||||
.route("/attending/confirm", put(attending::confirm_attending))
|
||||
.route("/attending/mark", post(attending::mark_attending))
|
||||
|
||||
@ -19,6 +19,7 @@ pub struct WinnerEntry {
|
||||
#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
||||
pub struct PrizeQuery {
|
||||
pub prize_id: Option<i32>,
|
||||
pub grade: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
||||
|
||||
@ -55,6 +55,7 @@ pub async fn get_report(
|
||||
ON u.id = ea.user_id AND ea.confirmed = true
|
||||
LEFT JOIN events e
|
||||
ON ea.event_id = e.id
|
||||
WHERE u.role = 'student'
|
||||
GROUP BY u.id
|
||||
ORDER BY u.grade, u.username, points
|
||||
"#,
|
||||
|
||||
@ -31,6 +31,7 @@ pub async fn select_winners(
|
||||
SELECT
|
||||
u.id,
|
||||
u.username,
|
||||
u.grade,
|
||||
COALESCE(SUM(e.points), 0) AS points,
|
||||
RANDOM() * COALESCE(SUM(e.points), 0) AS random_value
|
||||
FROM
|
||||
@ -50,6 +51,7 @@ pub async fn select_winners(
|
||||
id, points
|
||||
FROM weighted_users
|
||||
WHERE points > p.points_min
|
||||
AND grade = $3
|
||||
LIMIT 1
|
||||
) u
|
||||
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.is_none(),
|
||||
prize_query.grade,
|
||||
)
|
||||
.execute(&app_state.db_pool)
|
||||
.await;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user