2023-10-01 14:35:04 +02:00
|
|
|
use crate::model::user::User;
|
2023-07-24 20:56:46 +02:00
|
|
|
use serde::Serialize;
|
2023-07-24 21:17:51 +02:00
|
|
|
use sqlx::{FromRow, Row, SqlitePool};
|
2023-07-24 20:56:46 +02:00
|
|
|
|
|
|
|
#[derive(FromRow, Serialize, Clone)]
|
|
|
|
pub struct Stat {
|
|
|
|
name: String,
|
|
|
|
rowed_km: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Stat {
|
|
|
|
pub async fn get_rowed_km(db: &SqlitePool) -> Vec<Stat> {
|
2023-07-24 21:17:51 +02:00
|
|
|
//TODO: switch to query! macro again (once upgraded to sqlite 3.42 on server)
|
2023-07-24 21:16:43 +02:00
|
|
|
sqlx::query(
|
2023-08-05 15:58:17 +02:00
|
|
|
"
|
|
|
|
SELECT u.name, SUM(sub.distance_in_km) AS rowed_km
|
|
|
|
FROM user u
|
|
|
|
INNER JOIN (
|
|
|
|
SELECT r.rower_id AS user_id, l.distance_in_km
|
|
|
|
FROM logbook l
|
|
|
|
INNER JOIN rower r ON l.id = r.logbook_id
|
|
|
|
WHERE l.distance_in_km IS NOT NULL
|
|
|
|
|
|
|
|
UNION ALL
|
|
|
|
|
|
|
|
SELECT l.shipmaster AS user_id, l.distance_in_km
|
|
|
|
FROM logbook l
|
|
|
|
WHERE l.distance_in_km IS NOT NULL
|
|
|
|
) sub ON u.id = sub.user_id
|
|
|
|
GROUP BY u.name
|
|
|
|
ORDER BY rowed_km DESC;
|
|
|
|
",
|
2023-07-24 20:56:46 +02:00
|
|
|
)
|
|
|
|
.fetch_all(db)
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.into_iter()
|
|
|
|
.map(|row| Stat {
|
2023-07-24 21:16:43 +02:00
|
|
|
name: row.get("name"),
|
|
|
|
rowed_km: row.get("rowed_km"),
|
2023-07-24 20:56:46 +02:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
2023-10-01 14:35:04 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
pub struct PersonalStat {
|
|
|
|
date: String,
|
|
|
|
km: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_personal(db: &SqlitePool, user: &User) -> Vec<PersonalStat> {
|
|
|
|
vec![
|
|
|
|
PersonalStat {
|
|
|
|
date: String::from("2023-01-01"),
|
|
|
|
km: 5,
|
|
|
|
},
|
|
|
|
PersonalStat {
|
|
|
|
date: String::from("2023-02-01"),
|
|
|
|
km: 24,
|
|
|
|
},
|
|
|
|
PersonalStat {
|
|
|
|
date: String::from("2023-08-30"),
|
|
|
|
km: 1340,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|