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
use std::fmt::Debug;
pub trait Entity<IdType>: Clone + Debug {
fn get_id(&self) -> IdType;
}
pub trait Create<I, E: Entity<I>> {
type Error;
fn save(&mut self, entity: &E) -> Result<(), Self::Error>;
}
pub trait Read<I, E: Entity<I>> {
type Error;
fn find_by_id(&self, id: &I) -> Result<E, Self::Error>;
}
pub trait ReadWithPaginationAndSort<I, E: Entity<I>> {
type Error;
fn find_all_with_page(&self, page: &Page) -> Result<Vec<E>, Self::Error>;
fn find_all_with_page_and_sort(&self, page: &Page, sort: &Sort) -> Result<Vec<E>, Self::Error>;
}
pub trait Update<I, E: Entity<I>> {
type Error;
fn update(&mut self, entity: &E) -> Result<(), Self::Error>;
}
pub trait Delete<I, E: Entity<I>> {
type Error;
fn remove_by_id(&mut self, id: &I) -> Result<(), Self::Error>;
fn remove(&mut self, entity: &E) -> Result<(), Self::Error>;
}
pub trait Crud<I, E: Entity<I>>:
Create<I, E> + Read<I, E> + ReadWithPaginationAndSort<I, E> + Update<I, E> + Delete<I, E>
{
}
pub struct Page {
pub number: u32,
pub size: u32,
}
impl Page {
pub fn new(number: u32, size: u32) -> Self {
Self { number, size }
}
pub fn offset(&self) -> u32 {
self.number * self.size
}
}
#[derive(PartialEq)]
pub enum Sort {
ASCENDING,
DESCENDING,
}