persmgr/src/db/tables/assignables/ranks.rs

67 lines
1.7 KiB
Rust

use anyhow::Result;
use sqlx::prelude::FromRow;
use crate::db::{CurrPool, tables::TableMeta};
#[derive(Debug, Default, Clone, FromRow)]
pub struct Rank {
pub id: i64,
pub name: String,
pub description: String,
pub created_at: i64,
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<Self> {
let session = sqlx::query_as!(
Rank,
r#"
INSERT INTO ranks (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!(Rank, "SELECT * FROM ranks 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!(
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)
}
}