rowt/src/model/rower.rs

53 lines
1.3 KiB
Rust
Raw Normal View History

2023-07-24 13:01:39 +02:00
use serde::{Deserialize, Serialize};
2023-07-25 13:22:11 +02:00
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
2023-07-24 13:01:39 +02:00
2023-07-31 16:25:07 +02:00
use super::{
logbook::Logbook,
user::{MyNaiveDateTime, User},
};
2023-07-24 13:01:39 +02:00
#[derive(FromRow, Debug, Serialize, Deserialize)]
pub struct Rower {
pub logbook_id: i64,
pub rower_id: i64,
}
impl Rower {
pub async fn for_log(db: &SqlitePool, log: &Logbook) -> Vec<User> {
2023-07-31 16:25:07 +02:00
sqlx::query!(
2023-07-24 13:01:39 +02:00
"
2023-07-31 16:25:07 +02:00
SELECT id, name, pw, is_cox, is_admin, is_guest, deleted, last_access
FROM user
WHERE id in (SELECT rower_id FROM rower WHERE logbook_id=?)
",
2023-07-24 13:01:39 +02:00
log.id
)
.fetch_all(db)
.await
.unwrap()
2023-07-31 16:25:07 +02:00
.into_iter()
.map(|row| User {
id: row.id,
name: row.name,
pw: row.pw,
is_cox: row.is_cox,
is_admin: row.is_admin,
is_guest: row.is_guest,
deleted: row.deleted,
last_access: row.last_access.map(MyNaiveDateTime),
})
.collect()
2023-07-24 13:01:39 +02:00
}
2023-07-25 13:22:11 +02:00
pub async fn create(db: &mut Transaction<'_, Sqlite>, logbook_id: i64, rower_id: i64) {
2023-07-24 13:01:39 +02:00
let _ = sqlx::query!(
"INSERT INTO rower(logbook_id, rower_id) VALUES (?,?)",
logbook_id,
rower_id
)
.execute(db)
.await
.unwrap();
}
}