articles_rs/databases/
article_repository.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use sqlx::{
    postgres::{PgPool, PgRow},
    Error as SqlxError, FromRow, Row,
};
use std::collections::HashMap;
use uuid::Uuid;

use crate::postgres_repository::PostgresRepository;

pub struct DbArticle {
    pub id: Option<Uuid>,
    pub title: String,
    pub hero_image: String,
    pub slug: String,
    pub description: String,
    pub author: String,
    pub status: String,
    pub date: DateTime<Utc>,
    pub content: String,
}

impl DbArticle {
    pub fn new(title: &str, slug: &str, description: &str, author: &str) -> Self {
        let now = Utc::now();
        DbArticle {
            id: None,
            title: title.to_string(),
            hero_image: "".to_string(),
            slug: slug.to_string(),
            description: description.to_string(),
            author: author.to_string(),
            status: "NEW".to_string(),
            date: now,
            content: String::new(),
        }
    }
}

impl<'r> FromRow<'r, PgRow> for DbArticle {
    fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
        let row_id = row.try_get("id").ok();
        let hero: String = row.try_get("hero_image").unwrap_or_else(|_| String::new());
        Ok(DbArticle {
            id: row_id,
            title: row.try_get("title")?,
            hero_image: hero,
            slug: row.try_get("slug")?,
            description: row.try_get("description")?,
            author: row.try_get("author")?,
            status: row.try_get("status")?,
            date: row.try_get::<DateTime<Utc>, _>("date").unwrap_or_else(|_| {
                let naive_date: chrono::NaiveDateTime = row.try_get("date").unwrap();
                DateTime::<Utc>::from_naive_utc_and_offset(naive_date, Utc)
            }),
            content: row.try_get("content")?,
        })
    }
}

pub struct ArticleRepository {
    pool: PgPool,
}

impl ArticleRepository {
    pub fn new(pool: PgPool) -> Self {
        Self { pool }
    }
}

#[async_trait]
impl PostgresRepository for ArticleRepository {
    type Error = SqlxError;
    type Item = DbArticle;

    async fn create(&self, article: &Self::Item) -> Result<Uuid, Self::Error> {
        let row = sqlx::query(
            r#"
                INSERT INTO articles (title, slug, description, author, status, date, content)
                VALUES ($1, $2, $3, $4, $5, $6, $7)
                RETURNING id
                "#,
        )
        .bind(&article.title)
        .bind(&article.slug)
        .bind(&article.description)
        .bind(&article.author)
        .bind(&article.status)
        .bind(article.date)
        .bind(&article.content)
        .fetch_one(&self.pool)
        .await?;

        let id: Uuid = row.try_get("id")?;
        Ok(id)
    }

    async fn update(&self, article: &Self::Item) -> Result<(), Self::Error> {
        let affected_rows = sqlx::query(
            r#"
                UPDATE articles
                SET title = $1, slug = $2, author = $3, status = $4, date = $5, content = $6, description = $7
                WHERE id = $8
                "#,
        )
        .bind(&article.title)
        .bind(&article.slug)
        .bind(&article.author)
        .bind(&article.status)
        .bind(article.date)
        .bind(&article.content)
        .bind(&article.description)
        .bind(article.id)
        .execute(&self.pool)
        .await?
        .rows_affected();

        if affected_rows == 0 {
            return Err(SqlxError::RowNotFound);
        }

        Ok(())
    }

    async fn delete(&self, id: Uuid) -> Result<(), Self::Error> {
        let affected_rows = sqlx::query(
            r#"
                DELETE FROM articles
                WHERE id = $1
                "#,
        )
        .bind(id)
        .execute(&self.pool)
        .await?
        .rows_affected();

        if affected_rows == 0 {
            return Err(SqlxError::RowNotFound);
        }

        Ok(())
    }

    async fn delete_all(
        &self,
        _filters: Option<HashMap<String, String>>,
    ) -> Result<(), Self::Error> {
        return Err(SqlxError::RowNotFound);
    }

    async fn find_by_id(&self, id: Uuid) -> Result<Option<Self::Item>, Self::Error> {
        let row_result = sqlx::query(
            "SELECT articles.id, title, slug, description, author, status, date, content,
            (SELECT image_path FROM article_images WHERE article_images.article_id = articles.id LIMIT 1) as hero_image
            FROM articles WHERE articles.id = $1",
        )
        .bind(id)
        .fetch_optional(&self.pool)
        .await?;

        match row_result {
            Some(row) => {
                let article = DbArticle::from_row(&row)?;
                Ok(Some(article))
            }
            None => Ok(None),
        }
    }

    async fn find_by_name(&self, slug: String) -> Result<Option<Self::Item>, Self::Error> {
        let sql = "SELECT articles.id, title, slug, description, author, status, date, content,
                            (SELECT image_path FROM article_images WHERE article_images.article_id = articles.id LIMIT 1) as hero_image
                            FROM articles WHERE articles.slug = $1";
        let row_result = sqlx::query(sql)
            .bind(slug.clone())
            .fetch_optional(&self.pool)
            .await?;

        match row_result {
            Some(row) => {
                let article = DbArticle::from_row(&row)?;
                Ok(Some(article))
            }
            None => Ok(None),
        }
    }

    async fn list(
        &self,
        filters: Option<HashMap<String, String>>,
    ) -> Result<Vec<Self::Item>, Self::Error> {
        let mut query = String::from(
            "SELECT articles.id, title, slug, description, author, status, date, content,
                                        (SELECT image_path FROM article_images WHERE article_images.article_id = articles.id LIMIT 1) as hero_image
                                        FROM articles",
        );
        let mut params: Vec<String> = Vec::new();
        let mut param_count = 1;

        if let Some(filter_map) = filters {
            if !filter_map.is_empty() {
                query.push_str(" WHERE ");
                for (column, value) in filter_map {
                    if param_count > 1 {
                        query.push_str(" AND ");
                    }
                    query.push_str(&format!("{} = ${}", column, param_count));
                    params.push(value);
                    param_count += 1;
                }
            }
        }

        query.push_str(" ORDER BY date DESC");
        let mut query_builder = sqlx::query(&query);
        for param in params {
            // Try parsing as UUID first, if it fails, bind as a string
            if let Ok(uuid) = sqlx::types::Uuid::parse_str(&param) {
                query_builder = query_builder.bind(uuid);
            } else {
                query_builder = query_builder.bind(param);
            }
        }

        let articles = query_builder
            .fetch_all(&self.pool)
            .await?
            .into_iter()
            .map(|row| DbArticle::from_row(&row))
            .collect::<Result<Vec<_>, _>>()?;

        Ok(articles)
    }
}