From ad4377b52c2e08e0149b7bc8e1115782ca775a13 Mon Sep 17 00:00:00 2001 From: Mitchell Marino Date: Sun, 16 Apr 2023 16:08:47 -0500 Subject: [PATCH] small tweaks --- src/models.rs | 16 ++++++++++++++- src/report.rs | 23 +++++++++++----------- src/winner.rs | 2 +- templates/report.html | 46 +++++++++++++++++++++++++++++++++++++------ 4 files changed, 68 insertions(+), 19 deletions(-) diff --git a/src/models.rs b/src/models.rs index afc9dc6..e8e8dd7 100644 --- a/src/models.rs +++ b/src/models.rs @@ -130,7 +130,7 @@ pub struct Signin { /// The model for the query parameters of the report request. #[derive(Copy, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] pub struct ReportQuery { - pub grade: Option, + pub detailed_event_view: bool, } /// The model for the confirm attending request. @@ -164,6 +164,20 @@ pub struct LeaderBoardEntry { pub username: String, pub points: Option, } + +#[derive( + sqlx::Type, Clone, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, +)] +pub struct ReportEntry { + pub username: String, + pub grade: i32, + pub points: Option, + pub event_titles: Vec, + pub event_points: Vec, + pub event_discriptions: Vec, + pub event_types: Vec, +} + /// Module for (de)serializing [OffsetDateTime] to conform with the JWT spec (RFC 7519 section 2, "Numeric Date") mod serde_numeric_date { use serde::{self, Deserialize, Deserializer, Serializer}; diff --git a/src/report.rs b/src/report.rs index ad4e97b..b94c4f2 100644 --- a/src/report.rs +++ b/src/report.rs @@ -1,6 +1,6 @@ use crate::{ jwt::handle_token, - models::{LeaderBoardEntry, ReportQuery, Role}, + models::{ReportEntry, ReportQuery, Role}, AppState, }; use axum::{ @@ -40,24 +40,24 @@ pub async fn get_report( }; let result = query_as!( - LeaderBoardEntry, + ReportEntry, r#" SELECT u.username, - COALESCE(SUM(e.points), 0) AS points - FROM - users u + u.grade, + COALESCE(SUM(e.points), 0) AS points, + array_agg(e.title) AS "event_titles!", + array_agg(e.description) AS "event_discriptions!", + array_agg(e.points) AS "event_points!", + array_agg(e.event_type) AS "event_types!: Vec" + FROM users u LEFT JOIN event_attendees ea ON u.id = ea.user_id AND ea.confirmed = true LEFT JOIN events e ON ea.event_id = e.id - WHERE - u.grade = $1 OR $2 GROUP BY u.id - ORDER BY points + ORDER BY u.grade, u.username, points "#, - report_query.grade.unwrap_or(0), - report_query.grade.is_none(), ) .fetch_all(&app_state.db_pool) .await; @@ -76,9 +76,10 @@ pub async fn get_report( }; let mut context = Context::new(); - context.insert("grade", &report_query.grade); context.insert("date", &Local::now().format("%Y-%m-%d").to_string()); context.insert("users", &records); + context.insert("detailed_event_view", &report_query.detailed_event_view); + let report = match TEMPLATES.render("report.html", &context) { Ok(report) => report, Err(err) => { diff --git a/src/winner.rs b/src/winner.rs index 77f0062..6d2a8a9 100644 --- a/src/winner.rs +++ b/src/winner.rs @@ -199,7 +199,7 @@ pub async fn mark_claimed( match result { Ok(record) => { - if (record.rows_affected() == 0) { + if record.rows_affected() == 0 { ( StatusCode::BAD_REQUEST, Json(json!({ "error": format!("winner_id not found") })), diff --git a/templates/report.html b/templates/report.html index 7150f83..6a3ea35 100644 --- a/templates/report.html +++ b/templates/report.html @@ -1,7 +1,7 @@ - Student Point Report Grade {{ grade }} + Student Point Report