dofus_framework/ddd/infrastructure/
in_memory_repository.rs

1use crate::ddd::application::repository::Repository;
2use crate::ddd::domain::entity::{Entity, Identifiable};
3use std::collections::HashMap;
4
5pub struct InMemoryRepository<K: Identifiable, E: Entity<K>> {
6    entities: HashMap<K, E>,
7}
8
9impl<K: Identifiable, E: Entity<K>> Repository<K, E> for InMemoryRepository<K, E> {
10    fn save(&mut self, entity: E) {
11        self.entities.insert(entity.id(), entity);
12    }
13
14    fn get_by_id(&self, key: &K) -> Option<&E> {
15        self.entities.get(key)
16    }
17
18    fn get_all(&self) -> Vec<&E> {
19        self.entities.values().collect()
20    }
21
22    fn delete(&mut self, key: &K) -> Option<E> {
23        self.entities.remove(key)
24    }
25}