articles_rs/articles/
repo.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
use async_trait::async_trait;
use sqlx::{postgres::PgPool, Error as SqlxError, FromRow, Row};
use std::collections::HashMap;
use uuid::Uuid;

use crate::postgres_repository::PostgresRepository;

use super::Article;

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 = Article;

    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 find_by_id(&self, id: Uuid) -> Result<Option<Self::Item>, Self::Error> {
        let row_result = sqlx::query(
            "SELECT id, title, slug, description, author, status, date, content FROM articles WHERE id = $1",
        )
        .bind(id)
        .fetch_optional(&self.pool)
        .await?;

        match row_result {
            Some(row) => {
                let article = Article::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 id, title, slug, description, author, status, date::timestamptz, content 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| Article::from_row(&row))
            .collect::<Result<Vec<_>, _>>()?;

        Ok(articles)
    }
}