rowt/src/model/rower.rs

39 lines
949 B
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:33:44 +02:00
use super::{logbook::Logbook, user::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:33:44 +02:00
sqlx::query_as!(
User,
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-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();
}
}