use serde::{Deserialize, Serialize};
use sqlx::{FromRow, SqlitePool};

#[derive(FromRow, Debug, Serialize, Deserialize, Clone)]
pub struct LogType {
    pub id: i64,
    name: String,
}

impl LogType {
    pub async fn find_by_id(db: &SqlitePool, id: i64) -> Option<Self> {
        sqlx::query_as!(
            Self,
            "
SELECT id, name
FROM logbook_type
WHERE id like ?
        ",
            id
        )
        .fetch_one(db)
        .await
        .ok()
    }

    pub async fn all(db: &SqlitePool) -> Vec<Self> {
        sqlx::query_as!(
            Self,
            "
SELECT id, name
FROM logbook_type 
        "
        )
        .fetch_all(db)
        .await
        .unwrap() //TODO: fixme
    }
}

#[cfg(test)]
mod test {
    use crate::testdb;

    use sqlx::SqlitePool;

    #[sqlx::test]
    fn test_find_true() {
        let _ = testdb!();
    }

    //TODO: write tests
}
