45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
use anyhow::Result;
|
|
use persmgr_derive::TableMeta;
|
|
use sqlx::prelude::FromRow;
|
|
|
|
use crate::db::{CurrPool, tables::TableMeta};
|
|
|
|
#[derive(Debug, Default, Clone, FromRow, TableMeta)]
|
|
#[meta(table = "missions")]
|
|
pub struct Mission {
|
|
pub id: i64,
|
|
pub name: String,
|
|
pub description: String,
|
|
pub starting_at: i64,
|
|
pub estimated_length: i64,
|
|
pub created_at: i64,
|
|
pub modified_at: i64,
|
|
}
|
|
|
|
impl Mission {
|
|
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
|
|
let session = sqlx::query_as!(
|
|
Mission,
|
|
r#"
|
|
INSERT INTO missions (name, description, created_at, modified_at)
|
|
VALUES ($1, $2, $3, $4)
|
|
RETURNING *
|
|
"#,
|
|
self.name,
|
|
self.description,
|
|
self.created_at,
|
|
self.modified_at
|
|
)
|
|
.fetch_one(pool)
|
|
.await?;
|
|
|
|
Ok(session)
|
|
}
|
|
pub async fn get_by_id(pool: &CurrPool, id: i64) -> anyhow::Result<Self> {
|
|
let session = sqlx::query_as!(Mission, "SELECT * FROM missions WHERE id = $1", id)
|
|
.fetch_one(pool)
|
|
.await?;
|
|
Ok(session)
|
|
}
|
|
}
|