articles_rs/databases/
postgres_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
use async_trait::async_trait;
use std::collections::HashMap;
use uuid::Uuid;

/// Repository trait for PostgreSQL database operations
///
/// This trait defines a common interface for database repositories that handle
/// CRUD operations and queries on PostgreSQL tables.
#[async_trait]
pub trait PostgresRepository {
    /// The error type returned by repository operations
    type Error;
    /// The database model type this repository handles
    type Item;

    /// Finds a single item by its UUID
    ///
    /// # Arguments
    /// * `id` - The UUID to search for
    ///
    /// # Returns
    /// The found item wrapped in Some, or None if not found
    async fn find_by_id(&self, id: Uuid) -> Result<Option<Self::Item>, Self::Error>;

    /// Finds a single item by its name/slug
    ///
    /// # Arguments
    /// * `name` - The name or slug to search for
    ///
    /// # Returns
    /// The found item wrapped in Some, or None if not found
    async fn find_by_name(&self, name: String) -> Result<Option<Self::Item>, Self::Error>;

    /// Lists items matching optional filters
    ///
    /// # Arguments
    /// * `filters` - Optional HashMap of column names and values to filter by
    ///
    /// # Returns
    /// Vector of items matching the filters
    async fn list(
        &self,
        filters: Option<HashMap<String, String>>,
    ) -> Result<Vec<Self::Item>, Self::Error>;

    /// Creates a new item in the database
    ///
    /// # Arguments
    /// * `item` - The item to create
    ///
    /// # Returns
    /// The UUID of the created item
    async fn create(&self, item: &Self::Item) -> Result<Uuid, Self::Error>;

    /// Deletes an item by its UUID
    ///
    /// # Arguments
    /// * `id` - The UUID of the item to delete
    async fn delete(&self, id: Uuid) -> Result<(), Self::Error>;

    /// Deletes multiple items matching optional filters
    ///
    /// # Arguments
    /// * `filters` - Optional HashMap of column names and values to filter deletions by
    async fn delete_all(&self, filters: Option<HashMap<String, String>>)
        -> Result<(), Self::Error>;

    /// Updates an existing item
    ///
    /// # Arguments
    /// * `item` - The item with updated values
    async fn update(&self, item: &Self::Item) -> Result<(), Self::Error>;
}