add routes for different years

This commit is contained in:
philipp 2023-11-18 12:21:37 +01:00
parent e053ff96cf
commit f001aaf90f
3 changed files with 41 additions and 26 deletions

View File

@ -1,4 +1,4 @@
use chrono::NaiveDateTime; use chrono::{Datelike, NaiveDateTime};
use rocket::FromForm; use rocket::FromForm;
use serde::Serialize; use serde::Serialize;
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction}; use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
@ -220,14 +220,14 @@ ORDER BY departure DESC
} }
pub async fn completed(db: &SqlitePool) -> Vec<LogbookWithBoatAndRowers> { pub async fn completed(db: &SqlitePool) -> Vec<LogbookWithBoatAndRowers> {
let logs = sqlx::query_as!( let year = chrono::Utc::now().year();
Logbook, let logs = sqlx::query_as(
" &format!("
SELECT id, boat_id, shipmaster, steering_person, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype SELECT id, boat_id, shipmaster, steering_person, shipmaster_only_steering, departure, arrival, destination, distance_in_km, comments, logtype
FROM logbook FROM logbook
WHERE arrival is not null WHERE arrival is not null AND arrival LIKE '{}-%'
ORDER BY departure DESC ORDER BY departure DESC
" ", year)
) )
.fetch_all(db) .fetch_all(db)
.await .await

View File

@ -1,4 +1,5 @@
use crate::model::user::User; use crate::model::user::User;
use chrono::Datelike;
use serde::Serialize; use serde::Serialize;
use sqlx::{FromRow, Row, SqlitePool}; use sqlx::{FromRow, Row, SqlitePool};
@ -9,15 +10,20 @@ pub struct Stat {
} }
impl Stat { impl Stat {
pub async fn boats(db: &SqlitePool) -> Vec<Stat> { pub async fn boats(db: &SqlitePool, year: Option<i32>) -> Vec<Stat> {
let year = match year {
Some(year) => year,
None => chrono::Utc::now().year(),
};
//TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server)
sqlx::query( sqlx::query(&format!(
" "
SELECT (SELECT name FROM boat WHERE id=logbook.boat_id) as name, CAST(SUM(distance_in_km) AS INTEGER) AS rowed_km SELECT (SELECT name FROM boat WHERE id=logbook.boat_id) as name, CAST(SUM(distance_in_km) AS INTEGER) AS rowed_km
FROM logbook FROM logbook
WHERE arrival LIKE '{}-%'
GROUP BY boat_id GROUP BY boat_id
ORDER BY rowed_km DESC; ORDER BY rowed_km DESC;
", ",year)
) )
.fetch_all(db) .fetch_all(db)
.await .await
@ -30,19 +36,24 @@ ORDER BY rowed_km DESC;
.collect() .collect()
} }
pub async fn people(db: &SqlitePool) -> Vec<Stat> { pub async fn people(db: &SqlitePool, year: Option<i32>) -> Vec<Stat> {
let year = match year {
Some(year) => year,
None => chrono::Utc::now().year(),
};
//TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server) //TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server)
sqlx::query( sqlx::query(&format!(
" "
SELECT u.name, CAST(SUM(l.distance_in_km) AS INTEGER) AS rowed_km SELECT u.name, CAST(SUM(l.distance_in_km) AS INTEGER) AS rowed_km
FROM user u FROM user u
INNER JOIN rower r ON u.id = r.rower_id INNER JOIN rower r ON u.id = r.rower_id
INNER JOIN logbook l ON r.logbook_id = l.id INNER JOIN logbook l ON r.logbook_id = l.id
WHERE u.is_guest = 0 AND l.distance_in_km IS NOT NULL WHERE u.is_guest = 0 AND l.distance_in_km IS NOT NULL AND l.arrival LIKE '{}-%'
GROUP BY u.name GROUP BY u.name
ORDER BY rowed_km DESC; ORDER BY rowed_km DESC;
", ",
) year
))
.fetch_all(db) .fetch_all(db)
.await .await
.unwrap() .unwrap()

View File

@ -9,9 +9,9 @@ use crate::model::{
use super::log::KioskCookie; use super::log::KioskCookie;
#[get("/boats", rank = 2)] #[get("/boats?<year>", rank = 2)]
async fn index_boat(db: &State<SqlitePool>, user: NonGuestUser) -> Template { async fn index_boat(db: &State<SqlitePool>, user: NonGuestUser, year: Option<i32>) -> Template {
let stat = Stat::boats(db).await; let stat = Stat::boats(db, year).await;
let kiosk = false; let kiosk = false;
Template::render( Template::render(
@ -20,17 +20,21 @@ async fn index_boat(db: &State<SqlitePool>, user: NonGuestUser) -> Template {
) )
} }
#[get("/boats")] #[get("/boats?<year>")]
async fn index_boat_kiosk(db: &State<SqlitePool>, _kiosk: KioskCookie) -> Template { async fn index_boat_kiosk(
let stat = Stat::boats(db).await; db: &State<SqlitePool>,
_kiosk: KioskCookie,
year: Option<i32>,
) -> Template {
let stat = Stat::boats(db, year).await;
let kiosk = true; let kiosk = true;
Template::render("stat.boats", context!(stat, kiosk, show_kiosk_header: true)) Template::render("stat.boats", context!(stat, kiosk, show_kiosk_header: true))
} }
#[get("/", rank = 2)] #[get("/?<year>", rank = 2)]
async fn index(db: &State<SqlitePool>, user: NonGuestUser) -> Template { async fn index(db: &State<SqlitePool>, user: NonGuestUser, year: Option<i32>) -> Template {
let stat = Stat::people(db).await; let stat = Stat::people(db, year).await;
let personal = stat::get_personal(db, &user.user).await; let personal = stat::get_personal(db, &user.user).await;
let kiosk = false; let kiosk = false;
@ -40,9 +44,9 @@ async fn index(db: &State<SqlitePool>, user: NonGuestUser) -> Template {
) )
} }
#[get("/")] #[get("/?<year>")]
async fn index_kiosk(db: &State<SqlitePool>, _kiosk: KioskCookie) -> Template { async fn index_kiosk(db: &State<SqlitePool>, _kiosk: KioskCookie, year: Option<i32>) -> Template {
let stat = Stat::people(db).await; let stat = Stat::people(db, year).await;
let kiosk = true; let kiosk = true;
Template::render( Template::render(