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
use std::collections::HashMap;
use crate::application::{repository, ReadRepository, Repository};
use crate::domain::{AggregateRoot, Entity};
pub struct InMemoryRepository<T: AggregateRoot> {
entities: std::sync::RwLock<HashMap<<T as Entity>::Id, T>>,
}
impl<T: AggregateRoot> InMemoryRepository<T> {
pub fn new() -> Self {
Self {
entities: std::sync::RwLock::new(HashMap::new()),
}
}
}
impl<T: AggregateRoot> Default for InMemoryRepository<T> {
fn default() -> Self {
Self::new()
}
}
#[async_trait::async_trait]
impl<T: AggregateRoot + Clone> ReadRepository<T> for InMemoryRepository<T>
where
<T as Entity>::Id: std::hash::Hash + Eq,
{
async fn get_by_id(&self, id: <T as Entity>::Id) -> repository::Result<Option<T>> {
let ro_entities = self.entities.read().unwrap();
let entity = ro_entities.get(&id).cloned();
Ok(entity)
}
async fn list(&self, skip: usize, take: usize) -> repository::Result<Vec<T>> {
let ro_entities = self.entities.read().unwrap();
let entities = ro_entities
.values()
.skip(skip)
.take(take)
.cloned()
.collect();
Ok(entities)
}
async fn count(&self) -> repository::Result<usize> {
let ro_entities = self.entities.read().unwrap();
Ok(ro_entities.len())
}
}
#[async_trait::async_trait]
impl<T: AggregateRoot + Clone> Repository<T> for InMemoryRepository<T>
where
<T as Entity>::Id: std::hash::Hash + Eq,
{
async fn add(&self, entity: T) -> repository::Result<T> {
let mut wo_entities = self.entities.write().unwrap();
wo_entities.insert(entity.id(), entity.clone());
Ok(entity)
}
async fn update(&self, entity: T) -> repository::Result<T> {
self.add(entity).await
}
async fn delete(&self, entity: T) -> repository::Result<()> {
let mut wo_entities = self.entities.write().unwrap();
wo_entities.remove(&entity.id());
Ok(())
}
}