Skip to main content

systemprompt_files/services/content/
mod.rs

1use anyhow::Result;
2use systemprompt_database::DbPool;
3use systemprompt_identifiers::{ContentId, FileId};
4
5use crate::models::{ContentFile, File, FileRole};
6use crate::repository::FileRepository;
7
8#[derive(Debug, Clone)]
9pub struct ContentService {
10    repository: FileRepository,
11}
12
13impl ContentService {
14    pub fn new(db: &DbPool) -> Result<Self> {
15        Ok(Self {
16            repository: FileRepository::new(db)?,
17        })
18    }
19
20    pub const fn from_repository(repository: FileRepository) -> Self {
21        Self { repository }
22    }
23
24    pub const fn repository(&self) -> &FileRepository {
25        &self.repository
26    }
27
28    pub async fn link_to_content(
29        &self,
30        content_id: &ContentId,
31        file_id: &FileId,
32        role: FileRole,
33        display_order: i32,
34    ) -> Result<ContentFile> {
35        self.repository
36            .link_to_content(content_id, file_id, role, display_order)
37            .await
38    }
39
40    pub async fn unlink_from_content(
41        &self,
42        content_id: &ContentId,
43        file_id: &FileId,
44    ) -> Result<()> {
45        self.repository
46            .unlink_from_content(content_id, file_id)
47            .await
48    }
49
50    pub async fn list_files_by_content(
51        &self,
52        content_id: &ContentId,
53    ) -> Result<Vec<(File, ContentFile)>> {
54        self.repository.list_files_by_content(content_id).await
55    }
56
57    pub async fn list_content_by_file(&self, file_id: &FileId) -> Result<Vec<ContentFile>> {
58        self.repository.list_content_by_file(file_id).await
59    }
60
61    pub async fn find_featured_image(&self, content_id: &ContentId) -> Result<Option<File>> {
62        self.repository.find_featured_image(content_id).await
63    }
64
65    pub async fn set_featured(&self, file_id: &FileId, content_id: &ContentId) -> Result<()> {
66        self.repository.set_featured(file_id, content_id).await
67    }
68}