mod mutations;
mod queries;
mod stats;
use crate::models::{Content, CreateContentParams, UpdateContentParams};
use sqlx::PgPool;
use std::sync::Arc;
use systemprompt_database::DbPool;
use systemprompt_identifiers::{CategoryId, ContentId, LocaleCode, SourceId};
#[derive(Debug, Clone)]
pub struct ContentRepository {
pool: Arc<PgPool>,
write_pool: Arc<PgPool>,
}
impl ContentRepository {
pub fn new(db: &DbPool) -> Self {
let pool = db.pool();
let write_pool = db.write_pool();
Self { pool, write_pool }
}
pub async fn create(&self, params: &CreateContentParams) -> Result<Content, sqlx::Error> {
mutations::create(&self.write_pool, params).await
}
pub async fn find_by_id(&self, id: &ContentId) -> Result<Option<Content>, sqlx::Error> {
queries::find_by_id(&self.pool, id).await
}
pub async fn find_by_slug(
&self,
slug: &str,
locale: &LocaleCode,
) -> Result<Option<Content>, sqlx::Error> {
queries::find_by_slug(&self.pool, slug, locale).await
}
pub async fn find_by_source_and_slug(
&self,
source_id: &SourceId,
slug: &str,
locale: &LocaleCode,
) -> Result<Option<Content>, sqlx::Error> {
queries::find_by_source_and_slug(&self.pool, source_id, slug, locale).await
}
pub async fn list(&self, limit: i64, offset: i64) -> Result<Vec<Content>, sqlx::Error> {
queries::list(&self.pool, limit, offset).await
}
pub async fn list_by_source(
&self,
source_id: &SourceId,
locale: &LocaleCode,
) -> Result<Vec<Content>, sqlx::Error> {
queries::list_by_source(&self.pool, source_id, locale).await
}
pub async fn list_by_source_limited(
&self,
source_id: &SourceId,
locale: &LocaleCode,
limit: i64,
) -> Result<Vec<Content>, sqlx::Error> {
queries::list_by_source_limited(&self.pool, source_id, locale, limit).await
}
pub async fn list_sources_by_slug(
&self,
slug: &str,
locale: &LocaleCode,
) -> Result<Vec<SourceId>, sqlx::Error> {
queries::list_sources_by_slug(&self.pool, slug, locale).await
}
pub async fn list_slugs_with_locales_by_source(
&self,
source_id: &SourceId,
) -> Result<Vec<(String, LocaleCode)>, sqlx::Error> {
queries::list_slugs_with_locales_by_source(&self.pool, source_id).await
}
pub async fn update(&self, params: &UpdateContentParams) -> Result<Content, sqlx::Error> {
mutations::update(&self.write_pool, params).await
}
pub async fn category_exists(&self, category_id: &CategoryId) -> Result<bool, sqlx::Error> {
queries::category_exists(&self.pool, category_id).await
}
pub async fn delete(&self, id: &ContentId) -> Result<(), sqlx::Error> {
mutations::delete(&self.write_pool, id).await
}
pub async fn delete_by_source(&self, source_id: &SourceId) -> Result<u64, sqlx::Error> {
mutations::delete_by_source(&self.write_pool, source_id).await
}
pub async fn list_all(&self, limit: i64, offset: i64) -> Result<Vec<Content>, sqlx::Error> {
queries::list_all(&self.pool, limit, offset).await
}
}