First repo code
This commit is contained in:
1 parent
1f3ad22e05
commit
807f398d81
6 files changed
+399
-13
No files matched your search
@@ -0,0 +1,46 @@
|
||||
use crate::data::Id;
|
||||
use crate::data::entities::{Entity, EntityInner, Repository, WordData};
|
||||
use crate::notification::MaybeError;
|
||||
use parking_lot::{Mutex, RwLock};
|
||||
use rusqlite::Connection;
|
||||
use std::marker::PhantomData;
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct WordRepository {
|
||||
inner: Arc<RwLock<WordRepositoryInner>>,
|
||||
}
|
||||
|
||||
pub struct WordRepositoryInner {
|
||||
cache: Vec<WordData>,
|
||||
temporal: Vec<Entity<WordData, WordRepository>>,
|
||||
next_temporal: u32,
|
||||
connection: Arc<Mutex<Connection>>,
|
||||
}
|
||||
|
||||
impl Repository<WordData> for WordRepository {
|
||||
fn get(&self, id: Id) -> Option<WordData> {
|
||||
let inner = self.inner.read();
|
||||
Some(inner.cache[inner.cache.binary_search_by(|w| w.id.cmp(&id)).ok()?].clone())
|
||||
}
|
||||
|
||||
fn create(&mut self, new: WordData) -> Entity<WordData, WordRepository> {
|
||||
let mut inner = self.inner.write();
|
||||
let next_temporal = inner.next_temporal;
|
||||
inner.next_temporal += 1;
|
||||
drop(inner);
|
||||
let entity = Entity {
|
||||
inner: EntityInner {
|
||||
id: next_temporal.into(),
|
||||
real: false,
|
||||
},
|
||||
repo: self.clone(),
|
||||
_phantom: PhantomData,
|
||||
};
|
||||
entity
|
||||
}
|
||||
|
||||
fn save(&mut self, entity: EntityInner) -> MaybeError {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user