bridge_common/repo/
pages.rsuse chrono::Utc;
use serde::{Deserialize, Serialize};
use sqlx::{query, query_as, Executor, Postgres};
use crate::types::{
pages::{Page, ShortPage},
Result,
};
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct CreateParams {
pub title: String,
pub text: String,
}
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct UpdateParams {
pub title: String,
pub text: String,
}
pub async fn create<'a, E>(executor: E, company_id: i32, params: CreateParams) -> Result<Page>
where
E: Executor<'a, Database = Postgres>,
{
let current_datetime = Utc::now();
Ok(query_as!(
Page,
r#"
INSERT INTO pages (company_id, title, text, created_at, updated_at)
VALUES ($1, $2, $3, $4, $4)
RETURNING *
"#,
company_id,
params.title,
params.text,
current_datetime
)
.fetch_one(executor)
.await?)
}
pub async fn list<'a, E>(executor: E, company_id: i32) -> Result<Vec<ShortPage>>
where
E: Executor<'a, Database = Postgres>,
{
Ok(query_as!(
ShortPage,
"SELECT id, title, created_at, updated_at FROM pages WHERE company_id = $1",
company_id
)
.fetch_all(executor)
.await?)
}
pub async fn get<'a, E>(executor: E, company_id: i32, id: i32) -> Result<Page>
where
E: Executor<'a, Database = Postgres>,
{
Ok(query_as!(
Page,
"SELECT * FROM pages WHERE company_id = $1 AND id = $2",
company_id,
id
)
.fetch_one(executor)
.await?)
}
pub async fn update<'a, E>(
executor: E,
company_id: i32,
id: i32,
data: UpdateParams,
) -> Result<Page>
where
E: Executor<'a, Database = Postgres>,
{
let current_datetime = Utc::now();
Ok(query_as!(
Page,
r#"
UPDATE pages
SET title = $3, text = $4, updated_at = $5
WHERE company_id = $1 AND id = $2
RETURNING *
"#,
company_id,
id,
data.title,
data.text,
current_datetime
)
.fetch_one(executor)
.await?)
}
pub async fn delete<'a, E>(executor: E, company_id: i32, id: i32) -> Result<()>
where
E: Executor<'a, Database = Postgres>,
{
query!(
"DELETE FROM pages WHERE company_id = $1 AND id = $2",
company_id,
id
)
.execute(executor)
.await?;
Ok(())
}