Skip to main content

systemprompt_content/repository/content/
mod.rs

1mod mutations;
2mod queries;
3
4use crate::error::ContentError;
5use crate::models::{Content, CreateContentParams, UpdateContentParams};
6use sqlx::PgPool;
7use std::sync::Arc;
8use systemprompt_database::DbPool;
9use systemprompt_identifiers::{CategoryId, ContentId, SourceId};
10
11#[derive(Debug)]
12pub struct ContentRepository {
13    pool: Arc<PgPool>,
14}
15
16impl ContentRepository {
17    pub fn new(db: &DbPool) -> Result<Self, ContentError> {
18        let pool = db
19            .pool_arc()
20            .map_err(|e| ContentError::InvalidRequest(format!("Database pool error: {e}")))?;
21        Ok(Self { pool })
22    }
23
24    pub async fn create(&self, params: &CreateContentParams) -> Result<Content, sqlx::Error> {
25        mutations::create(&self.pool, params).await
26    }
27
28    pub async fn get_by_id(&self, id: &ContentId) -> Result<Option<Content>, sqlx::Error> {
29        queries::get_by_id(&self.pool, id).await
30    }
31
32    pub async fn get_by_slug(&self, slug: &str) -> Result<Option<Content>, sqlx::Error> {
33        queries::get_by_slug(&self.pool, slug).await
34    }
35
36    pub async fn get_by_source_and_slug(
37        &self,
38        source_id: &SourceId,
39        slug: &str,
40    ) -> Result<Option<Content>, sqlx::Error> {
41        queries::get_by_source_and_slug(&self.pool, source_id, slug).await
42    }
43
44    pub async fn list(&self, limit: i64, offset: i64) -> Result<Vec<Content>, sqlx::Error> {
45        queries::list(&self.pool, limit, offset).await
46    }
47
48    pub async fn list_by_source(&self, source_id: &SourceId) -> Result<Vec<Content>, sqlx::Error> {
49        queries::list_by_source(&self.pool, source_id).await
50    }
51
52    pub async fn list_by_source_limited(
53        &self,
54        source_id: &SourceId,
55        limit: i64,
56    ) -> Result<Vec<Content>, sqlx::Error> {
57        queries::list_by_source_limited(&self.pool, source_id, limit).await
58    }
59
60    pub async fn update(&self, params: &UpdateContentParams) -> Result<Content, sqlx::Error> {
61        mutations::update(&self.pool, params).await
62    }
63
64    pub async fn category_exists(&self, category_id: &CategoryId) -> Result<bool, sqlx::Error> {
65        queries::category_exists(&self.pool, category_id).await
66    }
67
68    pub async fn delete(&self, id: &ContentId) -> Result<(), sqlx::Error> {
69        mutations::delete(&self.pool, id).await
70    }
71
72    pub async fn delete_by_source(&self, source_id: &SourceId) -> Result<u64, sqlx::Error> {
73        mutations::delete_by_source(&self.pool, source_id).await
74    }
75
76    pub async fn list_all(&self, limit: i64, offset: i64) -> Result<Vec<Content>, sqlx::Error> {
77        queries::list_all(&self.pool, limit, offset).await
78    }
79
80    pub async fn get_popular_content_ids(
81        &self,
82        source_id: &SourceId,
83        days: i32,
84        limit: i64,
85    ) -> Result<Vec<ContentId>, sqlx::Error> {
86        queries::get_popular_content_ids(&self.pool, source_id, days, limit).await
87    }
88}