use serde::{Deserialize, Serialize}; use sqlx::{FromRow, Sqlite, SqlitePool, Transaction}; use super::{ logbook::Logbook, user::{MyNaiveDateTime, User}, }; #[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 { sqlx::query!( " 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=?) ", log.id ) .fetch_all(db) .await .unwrap() .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() } pub async fn create(db: &mut Transaction<'_, Sqlite>, logbook_id: i64, rower_id: i64) { let _ = sqlx::query!( "INSERT INTO rower(logbook_id, rower_id) VALUES (?,?)", logbook_id, rower_id ) .execute(db) .await .unwrap(); } }