Skip to main content

systemprompt_files/repository/ai/
mod.rs

1use systemprompt_identifiers::{ContextId, SessionId, TraceId, UserId};
2
3use super::file::FileRepository;
4use crate::error::FilesResult;
5use crate::models::File;
6
7impl FileRepository {
8    pub async fn list_ai_images(&self, limit: i64, offset: i64) -> FilesResult<Vec<File>> {
9        let result = sqlx::query_as!(
10            File,
11            r#"
12            SELECT id, path, public_url, mime_type, size_bytes, ai_content, metadata, user_id as "user_id: UserId", session_id as "session_id: SessionId", trace_id as "trace_id: TraceId", context_id as "context_id: ContextId", created_at, updated_at, deleted_at
13            FROM files
14            WHERE ai_content = true AND deleted_at IS NULL
15            ORDER BY created_at DESC
16            LIMIT $1 OFFSET $2
17            "#,
18            limit,
19            offset
20        )
21        .fetch_all(self.pool.as_ref())
22        .await?;
23
24        Ok(result)
25    }
26
27    pub async fn list_ai_images_by_user(
28        &self,
29        user_id: &UserId,
30        limit: i64,
31        offset: i64,
32    ) -> FilesResult<Vec<File>> {
33        let user_id_str = user_id.as_str();
34        let result = sqlx::query_as!(
35            File,
36            r#"
37            SELECT id, path, public_url, mime_type, size_bytes, ai_content, metadata, user_id as "user_id: UserId", session_id as "session_id: SessionId", trace_id as "trace_id: TraceId", context_id as "context_id: ContextId", created_at, updated_at, deleted_at
38            FROM files
39            WHERE user_id = $1 AND ai_content = true AND deleted_at IS NULL
40            ORDER BY created_at DESC
41            LIMIT $2 OFFSET $3
42            "#,
43            user_id_str,
44            limit,
45            offset
46        )
47        .fetch_all(self.pool.as_ref())
48        .await?;
49
50        Ok(result)
51    }
52
53    pub async fn count_ai_images_by_user(&self, user_id: &UserId) -> FilesResult<i64> {
54        let user_id_str = user_id.as_str();
55        let count = sqlx::query_scalar!(
56            r#"
57            SELECT COUNT(*) as "count!"
58            FROM files
59            WHERE user_id = $1 AND ai_content = true AND deleted_at IS NULL
60            "#,
61            user_id_str
62        )
63        .fetch_one(self.pool.as_ref())
64        .await?;
65
66        Ok(count)
67    }
68
69    pub async fn count_ai_images(&self) -> FilesResult<i64> {
70        let count = sqlx::query_scalar!(
71            r#"
72            SELECT COUNT(*) as "count!"
73            FROM files
74            WHERE ai_content = true AND deleted_at IS NULL
75            "#
76        )
77        .fetch_one(self.pool.as_ref())
78        .await?;
79
80        Ok(count)
81    }
82}