32 lines
952 B
Rust
32 lines
952 B
Rust
|
use std::ops::DerefMut;
|
||
|
|
||
|
use rocket::serde::{Deserialize, Serialize};
|
||
|
use sqlx::{FromRow, Sqlite, SqlitePool, Transaction};
|
||
|
|
||
|
#[derive(FromRow, Debug, Serialize, Deserialize, Eq, Hash, PartialEq, Clone)]
|
||
|
pub struct Trailer {
|
||
|
pub id: i64,
|
||
|
pub name: String,
|
||
|
}
|
||
|
|
||
|
impl Trailer {
|
||
|
pub async fn find_by_id(db: &SqlitePool, id: i32) -> Option<Self> {
|
||
|
sqlx::query_as!(Self, "SELECT id, name FROM trailer WHERE id like ?", id)
|
||
|
.fetch_one(db)
|
||
|
.await
|
||
|
.ok()
|
||
|
}
|
||
|
pub async fn find_by_id_tx(db: &mut Transaction<'_, Sqlite>, id: i32) -> Option<Self> {
|
||
|
sqlx::query_as!(Self, "SELECT id, name FROM trailer WHERE id like ?", id)
|
||
|
.fetch_one(db.deref_mut())
|
||
|
.await
|
||
|
.ok()
|
||
|
}
|
||
|
pub async fn all(db: &SqlitePool) -> Vec<Self> {
|
||
|
sqlx::query_as!(Self, "SELECT id, name FROM trailer")
|
||
|
.fetch_all(db)
|
||
|
.await
|
||
|
.unwrap()
|
||
|
}
|
||
|
}
|