From 6252ec87b9d576cb5ce0a2295ba41d67488d2eee Mon Sep 17 00:00:00 2001 From: MCorange Date: Mon, 15 Sep 2025 21:40:31 +0300 Subject: [PATCH] Change Over to new derive --- src/db/tables/assignables/awards.rs | 30 ++---------- src/db/tables/assignables/missions.rs | 30 ++---------- src/db/tables/assignables/qualifications.rs | 30 ++---------- src/db/tables/assignables/ranks.rs | 30 ++---------- src/db/tables/assignables/trainings.rs | 30 ++---------- src/db/tables/groups.rs | 53 +++++++++++++++++++++ src/db/tables/mod.rs | 1 + src/db/tables/records/awards.rs | 9 ++-- src/db/tables/records/missions.rs | 9 ++-- src/db/tables/records/qualifications.rs | 9 ++-- src/db/tables/records/ranks.rs | 9 ++-- src/db/tables/records/trainings.rs | 9 ++-- src/db/tables/sessions.rs | 10 ++-- src/db/tables/user.rs | 17 +++---- 14 files changed, 97 insertions(+), 179 deletions(-) create mode 100644 src/db/tables/groups.rs diff --git a/src/db/tables/assignables/awards.rs b/src/db/tables/assignables/awards.rs index e2100cd..e62c43c 100644 --- a/src/db/tables/assignables/awards.rs +++ b/src/db/tables/assignables/awards.rs @@ -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 = "awards")] pub struct Award { pub id: i64, pub name: String, @@ -12,11 +14,6 @@ pub struct Award { pub modified_at: i64, } -impl TableMeta for Award { - type PrimaryKey = i64; - const TABLE: &'static str = "awards"; -} - impl Award { pub async fn insert_new(&self, pool: &CurrPool) -> Result { let session = sqlx::query_as!( @@ -42,25 +39,4 @@ impl Award { .await?; Ok(session) } - pub async fn update(&self, pool: &CurrPool) -> anyhow::Result { - let curr_time = time::OffsetDateTime::now_utc().unix_timestamp(); - let session = sqlx::query_as!( - Award, - r#" - UPDATE awards 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) - } } diff --git a/src/db/tables/assignables/missions.rs b/src/db/tables/assignables/missions.rs index 55e5602..7583d3f 100644 --- a/src/db/tables/assignables/missions.rs +++ b/src/db/tables/assignables/missions.rs @@ -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 = "missions")] pub struct Mission { pub id: i64, pub name: String, @@ -14,11 +16,6 @@ pub struct Mission { pub modified_at: i64, } -impl TableMeta for Mission { - type PrimaryKey = i64; - const TABLE: &'static str = "missions"; -} - impl Mission { pub async fn insert_new(&self, pool: &CurrPool) -> Result { let session = sqlx::query_as!( @@ -44,25 +41,4 @@ impl Mission { .await?; Ok(session) } - pub async fn update(&self, pool: &CurrPool) -> anyhow::Result { - let curr_time = time::OffsetDateTime::now_utc().unix_timestamp(); - let session = sqlx::query_as!( - Mission, - r#" - UPDATE missions 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) - } } diff --git a/src/db/tables/assignables/qualifications.rs b/src/db/tables/assignables/qualifications.rs index 3bfe265..d99e909 100644 --- a/src/db/tables/assignables/qualifications.rs +++ b/src/db/tables/assignables/qualifications.rs @@ -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 = "qualifications")] pub struct Qualification { pub id: i64, pub name: String, @@ -12,11 +14,6 @@ pub struct Qualification { pub modified_at: i64, } -impl TableMeta for Qualification { - type PrimaryKey = i64; - const TABLE: &'static str = "qualifications"; -} - impl Qualification { pub async fn insert_new(&self, pool: &CurrPool) -> Result { let session = sqlx::query_as!( @@ -46,25 +43,4 @@ impl Qualification { .await?; Ok(session) } - pub async fn update(&self, pool: &CurrPool) -> anyhow::Result { - let curr_time = time::OffsetDateTime::now_utc().unix_timestamp(); - let session = sqlx::query_as!( - Qualification, - r#" - UPDATE qualifications 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) - } } diff --git a/src/db/tables/assignables/ranks.rs b/src/db/tables/assignables/ranks.rs index a76c0dd..37754eb 100644 --- a/src/db/tables/assignables/ranks.rs +++ b/src/db/tables/assignables/ranks.rs @@ -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 = "ranks")] pub struct Rank { pub id: i64, pub name: String, @@ -12,11 +14,6 @@ pub struct Rank { pub modified_at: i64, } -impl TableMeta for Rank { - type PrimaryKey = i64; - const TABLE: &'static str = "ranks"; -} - impl Rank { pub async fn insert_new(&self, pool: &CurrPool) -> Result { let session = sqlx::query_as!( @@ -42,25 +39,4 @@ impl Rank { .await?; Ok(session) } - pub async fn update(&self, pool: &CurrPool) -> anyhow::Result { - let curr_time = time::OffsetDateTime::now_utc().unix_timestamp(); - let session = sqlx::query_as!( - Rank, - r#" - UPDATE ranks 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) - } } diff --git a/src/db/tables/assignables/trainings.rs b/src/db/tables/assignables/trainings.rs index 821c0ef..598cc07 100644 --- a/src/db/tables/assignables/trainings.rs +++ b/src/db/tables/assignables/trainings.rs @@ -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 { let session = sqlx::query_as!( @@ -42,25 +39,4 @@ impl Training { .await?; Ok(session) } - pub async fn update(&self, pool: &CurrPool) -> anyhow::Result { - 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) - } } diff --git a/src/db/tables/groups.rs b/src/db/tables/groups.rs new file mode 100644 index 0000000..0224e80 --- /dev/null +++ b/src/db/tables/groups.rs @@ -0,0 +1,53 @@ +use anyhow::Result; +use persmgr_derive::TableMeta; +use sqlx::prelude::FromRow; + +use crate::db::{ + CurrPool, + tables::{ForeignKey, TableMeta, assignables::missions::Mission, user::User}, +}; + +#[derive(Debug, Default, Clone, FromRow, TableMeta)] +#[meta(table = "roster_groups")] +pub struct Groups { + pub id: i64, + pub name: String, + pub manager_id: ForeignKey, + pub sort_order: i64, +} + +impl Groups { + pub async fn insert_new(&self, pool: &CurrPool) -> Result { + let session = sqlx::query_as!( + Groups, + r#" + INSERT INTO roster_groups (name, manager_id, sort_order) + VALUES ($1, $2, $3) + RETURNING * + "#, + self.name, + *self.manager_id, + self.sort_order, + ) + .fetch_one(pool) + .await?; + + Ok(session) + } + pub async fn get_by_id(pool: &CurrPool, id: i64) -> anyhow::Result { + let session = sqlx::query_as!(Groups, "SELECT * FROM roster_groups WHERE id = $1", id) + .fetch_one(pool) + .await?; + Ok(session) + } + pub async fn get_by_manager_id(pool: &CurrPool, id: i64) -> anyhow::Result> { + let session = sqlx::query_as!( + Groups, + "SELECT * FROM roster_groups WHERE manager_id = $1", + id + ) + .fetch_all(pool) + .await?; + Ok(session) + } +} diff --git a/src/db/tables/mod.rs b/src/db/tables/mod.rs index 0124726..b049f84 100644 --- a/src/db/tables/mod.rs +++ b/src/db/tables/mod.rs @@ -8,6 +8,7 @@ use sqlx::{Decode, Encode, QueryBuilder, Type, postgres::PgRow}; pub mod assignables; pub mod attendance; +pub mod groups; pub mod records; pub mod sessions; pub mod user; diff --git a/src/db/tables/records/awards.rs b/src/db/tables/records/awards.rs index 106a075..9a9c318 100644 --- a/src/db/tables/records/awards.rs +++ b/src/db/tables/records/awards.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use persmgr_derive::TableMeta; use sqlx::prelude::FromRow; use crate::db::{ @@ -6,7 +7,8 @@ use crate::db::{ tables::{ForeignKey, TableMeta, assignables::awards::Award, user::User}, }; -#[derive(Debug, Default, Clone, FromRow)] +#[derive(Debug, Default, Clone, FromRow, TableMeta)] +#[meta(table = "records_awards")] pub struct AwardRecord { pub id: i64, pub user_id: ForeignKey, @@ -15,11 +17,6 @@ pub struct AwardRecord { pub created_at: i64, } -impl TableMeta for AwardRecord { - type PrimaryKey = i64; - const TABLE: &'static str = "records_awards"; -} - impl AwardRecord { pub async fn insert_new(&self, pool: &CurrPool) -> Result { let session = sqlx::query_as!( diff --git a/src/db/tables/records/missions.rs b/src/db/tables/records/missions.rs index cb92643..6d4f3be 100644 --- a/src/db/tables/records/missions.rs +++ b/src/db/tables/records/missions.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use persmgr_derive::TableMeta; use sqlx::prelude::FromRow; use crate::db::{ @@ -6,7 +7,8 @@ use crate::db::{ tables::{ForeignKey, TableMeta, assignables::missions::Mission, user::User}, }; -#[derive(Debug, Default, Clone, FromRow)] +#[derive(Debug, Default, Clone, FromRow, TableMeta)] +#[meta(table = "records_missions")] pub struct MissionRecord { pub id: i64, pub user_id: ForeignKey, @@ -15,11 +17,6 @@ pub struct MissionRecord { pub created_at: i64, } -impl TableMeta for MissionRecord { - type PrimaryKey = i64; - const TABLE: &'static str = "records_missions"; -} - impl MissionRecord { pub async fn insert_new(&self, pool: &CurrPool) -> Result { let session = sqlx::query_as!( diff --git a/src/db/tables/records/qualifications.rs b/src/db/tables/records/qualifications.rs index 574bc2f..0cd5a1d 100644 --- a/src/db/tables/records/qualifications.rs +++ b/src/db/tables/records/qualifications.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use persmgr_derive::TableMeta; use sqlx::prelude::FromRow; use crate::db::{ @@ -6,7 +7,8 @@ use crate::db::{ tables::{ForeignKey, TableMeta, assignables::qualifications::Qualification, user::User}, }; -#[derive(Debug, Default, Clone, FromRow)] +#[derive(Debug, Default, Clone, FromRow, TableMeta)] +#[meta(table = "records_qualifications")] pub struct QualificationRecord { pub id: i64, pub user_id: ForeignKey, @@ -15,11 +17,6 @@ pub struct QualificationRecord { pub created_at: i64, } -impl TableMeta for QualificationRecord { - type PrimaryKey = i64; - const TABLE: &'static str = "records_qualifications"; -} - impl QualificationRecord { pub async fn insert_new(&self, pool: &CurrPool) -> Result { let session = sqlx::query_as!( diff --git a/src/db/tables/records/ranks.rs b/src/db/tables/records/ranks.rs index d449078..523ab27 100644 --- a/src/db/tables/records/ranks.rs +++ b/src/db/tables/records/ranks.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use persmgr_derive::TableMeta; use sqlx::prelude::FromRow; use crate::db::{ @@ -6,7 +7,8 @@ use crate::db::{ tables::{ForeignKey, TableMeta, assignables::ranks::Rank, user::User}, }; -#[derive(Debug, Default, Clone, FromRow)] +#[derive(Debug, Default, Clone, FromRow, TableMeta)] +#[meta(table = "records_ranks")] pub struct RankRecord { pub id: i64, pub user_id: ForeignKey, @@ -15,11 +17,6 @@ pub struct RankRecord { pub created_at: i64, } -impl TableMeta for RankRecord { - type PrimaryKey = i64; - const TABLE: &'static str = "records_ranks"; -} - impl RankRecord { pub async fn insert_new(&self, pool: &CurrPool) -> Result { let session = sqlx::query_as!( diff --git a/src/db/tables/records/trainings.rs b/src/db/tables/records/trainings.rs index 9f6973b..ea50041 100644 --- a/src/db/tables/records/trainings.rs +++ b/src/db/tables/records/trainings.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use persmgr_derive::TableMeta; use sqlx::prelude::FromRow; use crate::db::{ @@ -6,7 +7,8 @@ use crate::db::{ tables::{ForeignKey, TableMeta, assignables::trainings::Training, user::User}, }; -#[derive(Debug, Default, Clone, FromRow)] +#[derive(Debug, Default, Clone, FromRow, TableMeta)] +#[meta(table = "records_trainings")] pub struct TrainingRecord { pub id: i64, pub user_id: ForeignKey, @@ -15,11 +17,6 @@ pub struct TrainingRecord { pub created_at: i64, } -impl TableMeta for TrainingRecord { - type PrimaryKey = i64; - const TABLE: &'static str = "records_trainings"; -} - impl TrainingRecord { pub async fn insert_new(&self, pool: &CurrPool) -> Result { let session = sqlx::query_as!( diff --git a/src/db/tables/sessions.rs b/src/db/tables/sessions.rs index 5be6fbd..2606b1d 100644 --- a/src/db/tables/sessions.rs +++ b/src/db/tables/sessions.rs @@ -1,20 +1,18 @@ 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 = "sessions")] pub struct Session { + pub id: i64, pub user_id: i64, pub session_key: String, pub expires: i64, } -impl TableMeta for Session { - type PrimaryKey = i64; - const TABLE: &'static str = "sessions"; -} - impl Session { pub async fn insert_new(&self, pool: &CurrPool) -> Result { let session = sqlx::query_as!( diff --git a/src/db/tables/user.rs b/src/db/tables/user.rs index d80e416..7ebaf69 100644 --- a/src/db/tables/user.rs +++ b/src/db/tables/user.rs @@ -1,8 +1,13 @@ use anyhow::bail; +use persmgr_derive::TableMeta; use sqlx::prelude::FromRow; -use crate::db::{CurrPool, tables::TableMeta}; -#[derive(Debug, Default, Clone, FromRow)] +use crate::db::{ + CurrPool, + tables::{ForeignKey, TableMeta, assignables::ranks::Rank, groups::Groups}, +}; +#[derive(Debug, Default, Clone, FromRow, TableMeta)] +#[meta(table = "users")] pub struct User { pub id: i64, pub email: String, @@ -11,12 +16,8 @@ pub struct User { pub pw_hash: String, pub pw_salt: String, pub pfp_id: i64, - pub rank_id: i64, -} - -impl TableMeta for User { - type PrimaryKey = i64; - const TABLE: &'static str = "users"; + pub rank_id: ForeignKey, + pub group_id: ForeignKey, } impl User {