Change Over to new derive

This commit is contained in:
2025-09-15 21:40:31 +03:00
parent 771be89260
commit 6252ec87b9
14 changed files with 97 additions and 179 deletions

View File

@@ -1,9 +1,11 @@
use anyhow::Result;
use persmgr_derive::TableMeta;
use sqlx::prelude::FromRow;
use crate::db::{CurrPool, tables::TableMeta};
#[derive(Debug, Default, Clone, FromRow)]
#[derive(Debug, Default, Clone, FromRow, TableMeta)]
#[meta(table = "trainings")]
pub struct Training {
pub id: i64,
pub name: String,
@@ -12,11 +14,6 @@ pub struct Training {
pub modified_at: i64,
}
impl TableMeta for Training {
type PrimaryKey = i64;
const TABLE: &'static str = "trainings";
}
impl Training {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!(
@@ -42,25 +39,4 @@ impl Training {
.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)
}
}