2023-07-23 12:17:57 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use sqlx::{FromRow, SqlitePool};
|
|
|
|
|
|
|
|
#[derive(FromRow, Debug, Serialize, Deserialize, Clone)]
|
2023-07-31 19:38:53 +02:00
|
|
|
pub struct LogType {
|
2023-07-23 12:17:57 +02:00
|
|
|
pub id: i64,
|
|
|
|
name: String,
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:38:53 +02:00
|
|
|
impl LogType {
|
2023-07-23 12:17:57 +02:00
|
|
|
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() {
|
2023-07-31 19:38:53 +02:00
|
|
|
let _ = testdb!();
|
2023-07-23 12:17:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: write tests
|
|
|
|
}
|