Skip to main content

systemprompt_files/repository/ai/
mod.rs

1use anyhow::{Context, Result};
2use systemprompt_identifiers::{ContextId, SessionId, TraceId, UserId};
3
4use super::file::FileRepository;
5use crate::models::File;
6
7impl FileRepository {
8    pub async fn list_ai_images(&self, limit: i64, offset: i64) -> Result<Vec<File>> {
9        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        .context("Failed to list AI images")
24    }
25
26    pub async fn list_ai_images_by_user(
27        &self,
28        user_id: &UserId,
29        limit: i64,
30        offset: i64,
31    ) -> Result<Vec<File>> {
32        let user_id_str = user_id.as_str();
33        sqlx::query_as!(
34            File,
35            r#"
36            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
37            FROM files
38            WHERE user_id = $1 AND ai_content = true AND deleted_at IS NULL
39            ORDER BY created_at DESC
40            LIMIT $2 OFFSET $3
41            "#,
42            user_id_str,
43            limit,
44            offset
45        )
46        .fetch_all(self.pool.as_ref())
47        .await
48        .context(format!("Failed to list AI images for user: {user_id}"))
49    }
50
51    pub async fn count_ai_images_by_user(&self, user_id: &UserId) -> Result<i64> {
52        let user_id_str = user_id.as_str();
53        let count = sqlx::query_scalar!(
54            r#"
55            SELECT COUNT(*) as "count!"
56            FROM files
57            WHERE user_id = $1 AND ai_content = true AND deleted_at IS NULL
58            "#,
59            user_id_str
60        )
61        .fetch_one(self.pool.as_ref())
62        .await
63        .context(format!("Failed to count AI images for user: {user_id}"))?;
64
65        Ok(count)
66    }
67
68    pub async fn count_ai_images(&self) -> Result<i64> {
69        let count = sqlx::query_scalar!(
70            r#"
71            SELECT COUNT(*) as "count!"
72            FROM files
73            WHERE ai_content = true AND deleted_at IS NULL
74            "#
75        )
76        .fetch_one(self.pool.as_ref())
77        .await
78        .context("Failed to count AI images")?;
79
80        Ok(count)
81    }
82}