2023-07-24 20:56:46 +02:00
|
|
|
use serde::Serialize;
|
2023-07-24 21:16:43 +02:00
|
|
|
use sqlx::{Row, FromRow, 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:16:43 +02:00
|
|
|
sqlx::query(
|
2023-07-24 20:56:46 +02:00
|
|
|
"SELECT u.name AS name, COALESCE(SUM(distance_in_km), 0) as rowed_km
|
|
|
|
FROM user u
|
|
|
|
INNER JOIN (
|
|
|
|
SELECT shipmaster AS user_id, distance_in_km
|
|
|
|
FROM logbook
|
|
|
|
UNION
|
|
|
|
SELECT r.rower_id AS user_id, l.distance_in_km
|
|
|
|
FROM logbook l
|
|
|
|
INNER JOIN rower r ON r.logbook_id = l.id
|
|
|
|
) AS subquery ON u.id = subquery.user_id
|
|
|
|
GROUP BY u.id ORDER BY rowed_km DESC;"
|
|
|
|
)
|
|
|
|
.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()
|
|
|
|
}
|
|
|
|
}
|