|
|
|
|
@ -4,7 +4,7 @@ use sqlx::PgPool;
|
|
|
|
|
use uuid::Uuid;
|
|
|
|
|
|
|
|
|
|
use crate::errors::AppError;
|
|
|
|
|
use crate::models::theme::Theme;
|
|
|
|
|
use crate::models::theme::{CreateThemeRequest, Theme, UpdateThemeRequest};
|
|
|
|
|
|
|
|
|
|
pub async fn list_for_user(pool: &PgPool, user_id: Uuid) -> Result<Vec<Theme>, AppError> {
|
|
|
|
|
let themes = sqlx::query_as::<_, Theme>(
|
|
|
|
|
@ -27,15 +27,12 @@ pub async fn get_by_id(pool: &PgPool, user_id: Uuid, id: Uuid) -> Result<Option<
|
|
|
|
|
Ok(theme)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Create a new theme from a validated request.
|
|
|
|
|
pub async fn create(
|
|
|
|
|
pool: &PgPool,
|
|
|
|
|
user_id: Uuid,
|
|
|
|
|
name: &str,
|
|
|
|
|
theme: &str,
|
|
|
|
|
categories: &serde_json::Value,
|
|
|
|
|
max_items_per_category: i32,
|
|
|
|
|
max_age_days: i32,
|
|
|
|
|
summary_length: i32,
|
|
|
|
|
req: &CreateThemeRequest,
|
|
|
|
|
categories_json: &serde_json::Value,
|
|
|
|
|
) -> Result<Theme, AppError> {
|
|
|
|
|
let row = sqlx::query_as::<_, Theme>(
|
|
|
|
|
r#"
|
|
|
|
|
@ -45,27 +42,24 @@ pub async fn create(
|
|
|
|
|
"#,
|
|
|
|
|
)
|
|
|
|
|
.bind(user_id)
|
|
|
|
|
.bind(name)
|
|
|
|
|
.bind(theme)
|
|
|
|
|
.bind(categories)
|
|
|
|
|
.bind(max_items_per_category)
|
|
|
|
|
.bind(max_age_days)
|
|
|
|
|
.bind(summary_length)
|
|
|
|
|
.bind(&req.name)
|
|
|
|
|
.bind(&req.theme)
|
|
|
|
|
.bind(categories_json)
|
|
|
|
|
.bind(req.max_items_per_category.unwrap_or(4))
|
|
|
|
|
.bind(req.max_age_days.unwrap_or(7))
|
|
|
|
|
.bind(req.summary_length.unwrap_or(3))
|
|
|
|
|
.fetch_one(pool)
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(row)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Partially update a theme from a validated request.
|
|
|
|
|
pub async fn update(
|
|
|
|
|
pool: &PgPool,
|
|
|
|
|
user_id: Uuid,
|
|
|
|
|
id: Uuid,
|
|
|
|
|
name: Option<&str>,
|
|
|
|
|
theme: Option<&str>,
|
|
|
|
|
categories: Option<&serde_json::Value>,
|
|
|
|
|
max_items_per_category: Option<i32>,
|
|
|
|
|
max_age_days: Option<i32>,
|
|
|
|
|
summary_length: Option<i32>,
|
|
|
|
|
req: &UpdateThemeRequest,
|
|
|
|
|
categories_json: Option<&serde_json::Value>,
|
|
|
|
|
) -> Result<Option<Theme>, AppError> {
|
|
|
|
|
let row = sqlx::query_as::<_, Theme>(
|
|
|
|
|
r#"
|
|
|
|
|
@ -83,12 +77,12 @@ pub async fn update(
|
|
|
|
|
)
|
|
|
|
|
.bind(id)
|
|
|
|
|
.bind(user_id)
|
|
|
|
|
.bind(name)
|
|
|
|
|
.bind(theme)
|
|
|
|
|
.bind(categories)
|
|
|
|
|
.bind(max_items_per_category)
|
|
|
|
|
.bind(max_age_days)
|
|
|
|
|
.bind(summary_length)
|
|
|
|
|
.bind(req.name.as_deref())
|
|
|
|
|
.bind(req.theme.as_deref())
|
|
|
|
|
.bind(categories_json)
|
|
|
|
|
.bind(req.max_items_per_category)
|
|
|
|
|
.bind(req.max_age_days)
|
|
|
|
|
.bind(req.summary_length)
|
|
|
|
|
.fetch_optional(pool)
|
|
|
|
|
.await?;
|
|
|
|
|
Ok(row)
|
|
|
|
|
|