Add more db shite

This commit is contained in:
2025-09-13 15:52:49 +03:00
parent 111bcedf2c
commit 7d88ac6db8
45 changed files with 971 additions and 5 deletions

View File

@@ -0,0 +1,73 @@
use anyhow::Result;
use crate::db::CurrPool;
#[derive(Debug, Default, Clone)]
pub struct MissionRecord {
pub id: i64,
pub user_id: i64,
pub mission_id: i64,
pub author_id: i64,
pub created_at: i64,
}
impl MissionRecord {
pub async fn insert_new(&self, pool: &CurrPool) -> Result<Self> {
let session = sqlx::query_as!(
MissionRecord,
r#"
INSERT INTO records_missions (user_id, mission_id, author_id, created_at)
VALUES ($1, $2, $3, $4)
RETURNING *
"#,
self.user_id,
self.mission_id,
self.author_id,
self.created_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!(
MissionRecord,
"SELECT * FROM records_missions WHERE id = $1",
id
)
.fetch_one(pool)
.await?;
Ok(session)
}
pub async fn get_by_user_id(pool: &CurrPool, id: i64) -> anyhow::Result<Vec<Self>> {
let session = sqlx::query_as!(
MissionRecord,
"SELECT * FROM records_missions WHERE user_id = $1",
id
)
.fetch_all(pool)
.await?;
Ok(session)
}
pub async fn get_by_author_id(pool: &CurrPool, id: i64) -> anyhow::Result<Vec<Self>> {
let session = sqlx::query_as!(
MissionRecord,
"SELECT * FROM records_missions WHERE author_id = $1",
id
)
.fetch_all(pool)
.await?;
Ok(session)
}
pub async fn get_by_mission_id(pool: &CurrPool, id: i64) -> anyhow::Result<Vec<Self>> {
let session = sqlx::query_as!(
MissionRecord,
"SELECT * FROM records_missions WHERE mission_id = $1",
id
)
.fetch_all(pool)
.await?;
Ok(session)
}
}