Change Over to new derive

This commit is contained in:
Gvidas Juknevičius 2025-09-15 21:40:31 +03:00
parent 771be89260
commit 6252ec87b9
Signed by: MCorange
GPG Key ID: 5BE6B533CB76FE86
14 changed files with 97 additions and 179 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

53
src/db/tables/groups.rs Normal file
View File

@ -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<User>,
pub sort_order: i64,
}
impl Groups {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
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<Self> {
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<Vec<Self>> {
let session = sqlx::query_as!(
Groups,
"SELECT * FROM roster_groups WHERE manager_id = $1",
id
)
.fetch_all(pool)
.await?;
Ok(session)
}
}

View File

@ -8,6 +8,7 @@ use sqlx::{Decode, Encode, QueryBuilder, Type, postgres::PgRow};
pub mod assignables; pub mod assignables;
pub mod attendance; pub mod attendance;
pub mod groups;
pub mod records; pub mod records;
pub mod sessions; pub mod sessions;
pub mod user; pub mod user;

View File

@ -1,4 +1,5 @@
use anyhow::Result; use anyhow::Result;
use persmgr_derive::TableMeta;
use sqlx::prelude::FromRow; use sqlx::prelude::FromRow;
use crate::db::{ use crate::db::{
@ -6,7 +7,8 @@ use crate::db::{
tables::{ForeignKey, TableMeta, assignables::awards::Award, user::User}, 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 struct AwardRecord {
pub id: i64, pub id: i64,
pub user_id: ForeignKey<User>, pub user_id: ForeignKey<User>,
@ -15,11 +17,6 @@ pub struct AwardRecord {
pub created_at: i64, pub created_at: i64,
} }
impl TableMeta for AwardRecord {
type PrimaryKey = i64;
const TABLE: &'static str = "records_awards";
}
impl AwardRecord { impl AwardRecord {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> { pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!( let session = sqlx::query_as!(

View File

@ -1,4 +1,5 @@
use anyhow::Result; use anyhow::Result;
use persmgr_derive::TableMeta;
use sqlx::prelude::FromRow; use sqlx::prelude::FromRow;
use crate::db::{ use crate::db::{
@ -6,7 +7,8 @@ use crate::db::{
tables::{ForeignKey, TableMeta, assignables::missions::Mission, user::User}, 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 struct MissionRecord {
pub id: i64, pub id: i64,
pub user_id: ForeignKey<User>, pub user_id: ForeignKey<User>,
@ -15,11 +17,6 @@ pub struct MissionRecord {
pub created_at: i64, pub created_at: i64,
} }
impl TableMeta for MissionRecord {
type PrimaryKey = i64;
const TABLE: &'static str = "records_missions";
}
impl MissionRecord { impl MissionRecord {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> { pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!( let session = sqlx::query_as!(

View File

@ -1,4 +1,5 @@
use anyhow::Result; use anyhow::Result;
use persmgr_derive::TableMeta;
use sqlx::prelude::FromRow; use sqlx::prelude::FromRow;
use crate::db::{ use crate::db::{
@ -6,7 +7,8 @@ use crate::db::{
tables::{ForeignKey, TableMeta, assignables::qualifications::Qualification, user::User}, 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 struct QualificationRecord {
pub id: i64, pub id: i64,
pub user_id: ForeignKey<User>, pub user_id: ForeignKey<User>,
@ -15,11 +17,6 @@ pub struct QualificationRecord {
pub created_at: i64, pub created_at: i64,
} }
impl TableMeta for QualificationRecord {
type PrimaryKey = i64;
const TABLE: &'static str = "records_qualifications";
}
impl QualificationRecord { impl QualificationRecord {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> { pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!( let session = sqlx::query_as!(

View File

@ -1,4 +1,5 @@
use anyhow::Result; use anyhow::Result;
use persmgr_derive::TableMeta;
use sqlx::prelude::FromRow; use sqlx::prelude::FromRow;
use crate::db::{ use crate::db::{
@ -6,7 +7,8 @@ use crate::db::{
tables::{ForeignKey, TableMeta, assignables::ranks::Rank, user::User}, 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 struct RankRecord {
pub id: i64, pub id: i64,
pub user_id: ForeignKey<User>, pub user_id: ForeignKey<User>,
@ -15,11 +17,6 @@ pub struct RankRecord {
pub created_at: i64, pub created_at: i64,
} }
impl TableMeta for RankRecord {
type PrimaryKey = i64;
const TABLE: &'static str = "records_ranks";
}
impl RankRecord { impl RankRecord {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> { pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!( let session = sqlx::query_as!(

View File

@ -1,4 +1,5 @@
use anyhow::Result; use anyhow::Result;
use persmgr_derive::TableMeta;
use sqlx::prelude::FromRow; use sqlx::prelude::FromRow;
use crate::db::{ use crate::db::{
@ -6,7 +7,8 @@ use crate::db::{
tables::{ForeignKey, TableMeta, assignables::trainings::Training, user::User}, 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 struct TrainingRecord {
pub id: i64, pub id: i64,
pub user_id: ForeignKey<User>, pub user_id: ForeignKey<User>,
@ -15,11 +17,6 @@ pub struct TrainingRecord {
pub created_at: i64, pub created_at: i64,
} }
impl TableMeta for TrainingRecord {
type PrimaryKey = i64;
const TABLE: &'static str = "records_trainings";
}
impl TrainingRecord { impl TrainingRecord {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> { pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!( let session = sqlx::query_as!(

View File

@ -1,20 +1,18 @@
use anyhow::Result; use anyhow::Result;
use persmgr_derive::TableMeta;
use sqlx::prelude::FromRow; use sqlx::prelude::FromRow;
use crate::db::{CurrPool, tables::TableMeta}; use crate::db::{CurrPool, tables::TableMeta};
#[derive(Debug, Default, Clone, FromRow)] #[derive(Debug, Default, Clone, FromRow, TableMeta)]
#[meta(table = "sessions")]
pub struct Session { pub struct Session {
pub id: i64,
pub user_id: i64, pub user_id: i64,
pub session_key: String, pub session_key: String,
pub expires: i64, pub expires: i64,
} }
impl TableMeta for Session {
type PrimaryKey = i64;
const TABLE: &'static str = "sessions";
}
impl Session { impl Session {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> { pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!( let session = sqlx::query_as!(

View File

@ -1,8 +1,13 @@
use anyhow::bail; use anyhow::bail;
use persmgr_derive::TableMeta;
use sqlx::prelude::FromRow; use sqlx::prelude::FromRow;
use crate::db::{CurrPool, tables::TableMeta}; use crate::db::{
#[derive(Debug, Default, Clone, FromRow)] CurrPool,
tables::{ForeignKey, TableMeta, assignables::ranks::Rank, groups::Groups},
};
#[derive(Debug, Default, Clone, FromRow, TableMeta)]
#[meta(table = "users")]
pub struct User { pub struct User {
pub id: i64, pub id: i64,
pub email: String, pub email: String,
@ -11,12 +16,8 @@ pub struct User {
pub pw_hash: String, pub pw_hash: String,
pub pw_salt: String, pub pw_salt: String,
pub pfp_id: i64, pub pfp_id: i64,
pub rank_id: i64, pub rank_id: ForeignKey<Rank>,
} pub group_id: ForeignKey<Groups>,
impl TableMeta for User {
type PrimaryKey = i64;
const TABLE: &'static str = "users";
} }
impl User { impl User {