Add more db shite
This commit is contained in:
60
src/db/tables/assignables/trainings.rs
Normal file
60
src/db/tables/assignables/trainings.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use anyhow::Result;
|
||||
|
||||
use crate::db::CurrPool;
|
||||
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct Training {
|
||||
pub id: i64,
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub created_at: i64,
|
||||
pub modified_at: i64,
|
||||
}
|
||||
|
||||
impl Training {
|
||||
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
|
||||
let session = sqlx::query_as!(
|
||||
Training,
|
||||
r#"
|
||||
INSERT INTO trainings (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!(Training, "SELECT * FROM trainings WHERE id = $1", id)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
Ok(session)
|
||||
}
|
||||
pub async fn update(&self, pool: &CurrPool) -> anyhow::Result<Self> {
|
||||
let curr_time = time::OffsetDateTime::now_utc().unix_timestamp();
|
||||
let session = sqlx::query_as!(
|
||||
Training,
|
||||
r#"
|
||||
UPDATE trainings SET
|
||||
name = $2,
|
||||
description = $3,
|
||||
modified_at = $4
|
||||
WHERE id = $1
|
||||
RETURNING *
|
||||
"#,
|
||||
self.id,
|
||||
self.name,
|
||||
self.description,
|
||||
curr_time
|
||||
)
|
||||
.fetch_one(pool)
|
||||
.await?;
|
||||
Ok(session)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user