backbone_core/
repository.rs1use async_trait::async_trait;
4use anyhow::Result;
5use uuid::Uuid;
6use std::collections::HashMap;
7
8#[derive(Debug, Clone, Default)]
15pub struct DomainPaginationParams {
16 pub page: u32,
17 pub per_page: u32,
18}
19
20impl DomainPaginationParams {
21 pub fn new(page: u32, per_page: u32) -> Self {
22 Self { page, per_page }
23 }
24
25 pub fn offset(&self) -> u64 {
26 ((self.page.saturating_sub(1)) * self.per_page) as u64
27 }
28
29 pub fn limit(&self) -> u64 {
30 self.per_page as u64
31 }
32}
33
34#[derive(Debug, Clone)]
39pub struct DomainPaginatedResult<E> {
40 pub data: Vec<E>,
41 pub total: u64,
42 pub page: u32,
43 pub per_page: u32,
44}
45
46#[async_trait]
48pub trait Repository<T> {
49 async fn create(&self, entity: &T) -> Result<T>;
50 async fn find_by_id(&self, id: &Uuid) -> Result<Option<T>>;
51 async fn update(&self, entity: &T) -> Result<T>;
52 async fn delete(&self, id: &Uuid) -> Result<bool>;
53 async fn list(&self, page: u32, limit: u32) -> Result<Vec<T>>;
54}
55
56#[async_trait]
58pub trait SearchableRepository<T>: Repository<T> {
59 async fn search(&self, criteria: HashMap<String, String>, page: u32, limit: u32) -> Result<Vec<T>>;
60 async fn count(&self, criteria: HashMap<String, String>) -> Result<u64>;
61}
62
63#[async_trait]
65pub trait SoftDeletableRepository<T>: Repository<T> {
66 async fn soft_delete(&self, id: &Uuid) -> Result<bool>;
67 async fn restore(&self, id: &Uuid) -> Result<bool>;
68 async fn list_deleted(&self, page: u32, limit: u32) -> Result<Vec<T>>;
69 async fn permanent_delete_all(&self) -> Result<u64>;
70}
71
72#[async_trait]
74pub trait PaginatedRepository<T>: Repository<T> {
75 async fn paginate(&self, page: u32, limit: u32) -> Result<(Vec<T>, u64)>;
76}
77
78#[async_trait]
80pub trait BulkRepository<T>: Repository<T> {
81 async fn bulk_create(&self, entities: Vec<T>) -> Result<Vec<T>>;
82 async fn bulk_update(&self, ids: &[Uuid], updates: HashMap<String, String>) -> Result<usize>;
83 async fn bulk_delete(&self, ids: &[Uuid]) -> Result<u64>;
84}
85
86#[async_trait]
88pub trait CrudRepository<T>:
89 Repository<T> +
90 SearchableRepository<T> +
91 SoftDeletableRepository<T> +
92 PaginatedRepository<T> +
93 BulkRepository<T> +
94 Send + Sync
95{
96}