builder update
This commit is contained in:
parent
9784c3cac6
commit
d830e677ba
8 changed files with 461 additions and 99 deletions
|
@ -3,7 +3,7 @@ use std::{collections::HashMap, ops::Deref};
|
|||
|
||||
/// A struct to keep track of historical changes to a value.
|
||||
/// This struct represents a value that has a current state and a history of previous states.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct Historic<T> {
|
||||
/// The current value
|
||||
pub current: T,
|
||||
|
@ -24,6 +24,13 @@ impl<T: Clone> Historic<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Default + Clone> Historic<T> {
|
||||
/// Create a new tracked value initialized with Default
|
||||
pub fn new_default() -> Historic<T> {
|
||||
Self::new(T::default())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone> Historic<T> {
|
||||
/// Update the value. The change will be recorded.
|
||||
/// Will record a change even if the value is the same as the current one.
|
||||
|
|
|
@ -18,8 +18,6 @@ pub mod reference;
|
|||
pub mod update;
|
||||
pub mod valid;
|
||||
|
||||
// todo : use mongodb projection to only get fields you actually use, maybe PartialModel shadow struct?
|
||||
|
||||
/// Error type when updating a model
|
||||
#[derive(Debug)]
|
||||
pub enum UpdateError {
|
||||
|
@ -35,6 +33,7 @@ pub trait Model:
|
|||
Sized + Referencable + Validate + serde::Serialize + for<'a> serde::Deserialize<'a>
|
||||
{
|
||||
type Partial: DeserializeOwned;
|
||||
type ChangeBuilder;
|
||||
|
||||
/// Insert the `Model` into the database
|
||||
fn insert(
|
||||
|
@ -293,6 +292,18 @@ pub trait Model:
|
|||
}
|
||||
}
|
||||
|
||||
fn change_builder(self) -> Self::ChangeBuilder;
|
||||
|
||||
/// Update values of `Model` using a builder pattern
|
||||
fn change(self) -> Self::ChangeBuilder {
|
||||
#[cfg(feature = "cache")]
|
||||
{
|
||||
mongod::cache_write!().invalidate(Self::collection_name(), self.id());
|
||||
}
|
||||
|
||||
self.change_builder()
|
||||
}
|
||||
|
||||
/// Update values of `Model` into database
|
||||
fn update(
|
||||
&mut self,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue