Expand index trait to also include key type
Signed-off-by: Lee Smet <lee.smet@hotmail.com>
This commit is contained in:
@@ -6,7 +6,7 @@ use std::fmt::Debug;
|
||||
pub struct IndexKey {
|
||||
/// The name of the index key
|
||||
pub name: &'static str,
|
||||
|
||||
|
||||
/// The value of the index key for a specific model instance
|
||||
pub value: String,
|
||||
}
|
||||
@@ -25,13 +25,13 @@ impl IndexKeyBuilder {
|
||||
value: String::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Set the value for this index key
|
||||
pub fn value(mut self, value: impl ToString) -> Self {
|
||||
self.value = value.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
/// Build the IndexKey
|
||||
pub fn build(self) -> IndexKey {
|
||||
IndexKey {
|
||||
@@ -42,10 +42,14 @@ impl IndexKeyBuilder {
|
||||
}
|
||||
|
||||
/// Unified trait for all models
|
||||
pub trait Model: Debug + Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static {
|
||||
pub trait Model:
|
||||
Debug + Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static
|
||||
{
|
||||
/// Get the database prefix for this model type
|
||||
fn db_prefix() -> &'static str where Self: Sized;
|
||||
|
||||
fn db_prefix() -> &'static str
|
||||
where
|
||||
Self: Sized;
|
||||
|
||||
/// Returns a list of index keys for this model instance
|
||||
/// These keys will be used to create additional indexes in the TST
|
||||
/// The default implementation returns an empty vector
|
||||
@@ -53,21 +57,27 @@ pub trait Model: Debug + Clone + Serialize + for<'de> Deserialize<'de> + Send +
|
||||
fn db_keys(&self) -> Vec<IndexKey> {
|
||||
Vec::new()
|
||||
}
|
||||
|
||||
|
||||
/// Get the unique ID for this model
|
||||
fn get_id(&self) -> u32;
|
||||
|
||||
|
||||
/// Get a mutable reference to the base_data field
|
||||
fn base_data_mut(&mut self) -> &mut BaseModelData;
|
||||
|
||||
|
||||
/// Set the ID for this model
|
||||
fn id(mut self, id: u32) -> Self where Self: Sized {
|
||||
fn id(mut self, id: u32) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.base_data_mut().id = id;
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
/// Build the model, updating the modified timestamp
|
||||
fn build(mut self) -> Self where Self: Sized {
|
||||
fn build(mut self) -> Self
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
self.base_data_mut().update_modified();
|
||||
self
|
||||
}
|
||||
@@ -78,6 +88,8 @@ pub trait Index {
|
||||
/// The model for which this is an index in the database
|
||||
type Model: Model;
|
||||
|
||||
type Key: ToString + ?Sized;
|
||||
|
||||
/// The key of this index
|
||||
fn key() -> &'static str;
|
||||
}
|
||||
@@ -87,13 +99,13 @@ pub trait Index {
|
||||
pub struct BaseModelData {
|
||||
/// Unique incremental ID per circle
|
||||
pub id: u32,
|
||||
|
||||
|
||||
/// Unix epoch timestamp for creation time
|
||||
pub created_at: i64,
|
||||
|
||||
|
||||
/// Unix epoch timestamp for last modification time
|
||||
pub modified_at: i64,
|
||||
|
||||
|
||||
/// List of comment IDs referencing Comment objects
|
||||
pub comments: Vec<u32>,
|
||||
}
|
||||
@@ -109,24 +121,24 @@ impl BaseModelData {
|
||||
comments: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Create a new BaseModelDataBuilder
|
||||
pub fn builder(id: u32) -> BaseModelDataBuilder {
|
||||
BaseModelDataBuilder::new(id)
|
||||
}
|
||||
|
||||
|
||||
/// Add a comment to this model
|
||||
pub fn add_comment(&mut self, comment_id: u32) {
|
||||
self.comments.push(comment_id);
|
||||
self.modified_at = chrono::Utc::now().timestamp();
|
||||
}
|
||||
|
||||
|
||||
/// Remove a comment from this model
|
||||
pub fn remove_comment(&mut self, comment_id: u32) {
|
||||
self.comments.retain(|&id| id != comment_id);
|
||||
self.update_modified();
|
||||
}
|
||||
|
||||
|
||||
/// Update the modified timestamp
|
||||
pub fn update_modified(&mut self) {
|
||||
self.modified_at = chrono::Utc::now().timestamp();
|
||||
@@ -151,31 +163,31 @@ impl BaseModelDataBuilder {
|
||||
comments: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Set the created_at timestamp
|
||||
pub fn created_at(mut self, timestamp: i64) -> Self {
|
||||
self.created_at = Some(timestamp);
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
/// Set the modified_at timestamp
|
||||
pub fn modified_at(mut self, timestamp: i64) -> Self {
|
||||
self.modified_at = Some(timestamp);
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
/// Add a comment ID
|
||||
pub fn add_comment(mut self, comment_id: u32) -> Self {
|
||||
self.comments.push(comment_id);
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
/// Add multiple comment IDs
|
||||
pub fn add_comments(mut self, comment_ids: Vec<u32>) -> Self {
|
||||
self.comments.extend(comment_ids);
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
/// Build the BaseModelData
|
||||
pub fn build(self) -> BaseModelData {
|
||||
let now = chrono::Utc::now().timestamp();
|
||||
@@ -197,14 +209,15 @@ macro_rules! impl_model {
|
||||
fn db_prefix() -> &'static str {
|
||||
$prefix
|
||||
}
|
||||
|
||||
|
||||
fn get_id(&self) -> u32 {
|
||||
self.base_data.id
|
||||
}
|
||||
|
||||
|
||||
fn base_data_mut(&mut self) -> &mut $crate::core::model::BaseModelData {
|
||||
&mut self.base_data
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user