Skip to main content

systemprompt_files/services/file/
mod.rs

1use anyhow::Result;
2use systemprompt_database::DbPool;
3use systemprompt_identifiers::{FileId, UserId};
4
5use crate::models::{File, FileMetadata};
6use crate::repository::{FileRepository, FileStats, InsertFileRequest};
7
8#[derive(Debug, Clone)]
9pub struct FileService {
10    repository: FileRepository,
11}
12
13impl FileService {
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 insert(&self, request: InsertFileRequest) -> Result<FileId> {
29        self.repository.insert(request).await
30    }
31
32    pub async fn insert_file(&self, file: &File) -> Result<FileId> {
33        self.repository.insert_file(file).await
34    }
35
36    pub async fn find_by_id(&self, id: &FileId) -> Result<Option<File>> {
37        self.repository.find_by_id(id).await
38    }
39
40    pub async fn find_by_path(&self, path: &str) -> Result<Option<File>> {
41        self.repository.find_by_path(path).await
42    }
43
44    pub async fn list_by_user(
45        &self,
46        user_id: &UserId,
47        limit: i64,
48        offset: i64,
49    ) -> Result<Vec<File>> {
50        self.repository.list_by_user(user_id, limit, offset).await
51    }
52
53    pub async fn list_all(&self, limit: i64, offset: i64) -> Result<Vec<File>> {
54        self.repository.list_all(limit, offset).await
55    }
56
57    pub async fn delete(&self, id: &FileId) -> Result<()> {
58        self.repository.delete(id).await
59    }
60
61    pub async fn update_metadata(&self, id: &FileId, metadata: &FileMetadata) -> Result<()> {
62        self.repository.update_metadata(id, metadata).await
63    }
64
65    pub async fn get_stats(&self) -> Result<FileStats> {
66        self.repository.get_stats().await
67    }
68
69    pub async fn search_by_path(&self, query: &str, limit: i64) -> Result<Vec<File>> {
70        self.repository.search_by_path(query, limit).await
71    }
72}