fix: Use incremental ID

This commit is contained in:
Mahmoud Emad
2025-05-17 13:00:05 +03:00
parent bde5db0e52
commit a676854251
13 changed files with 149 additions and 116 deletions

View File

@@ -59,21 +59,11 @@ pub trait Model:
}
/// Get the unique ID for this model
/// Returns 0 if the ID is None
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: Option<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
@@ -98,8 +88,8 @@ pub trait Index {
/// Base struct that all models should include
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BaseModelData {
/// Unique incremental ID per circle
pub id: Option<u32>,
/// Unique incremental ID - will be auto-generated by OurDB
pub id: u32,
/// Unix epoch timestamp for creation time
pub created_at: i64,
@@ -112,11 +102,12 @@ pub struct BaseModelData {
}
impl BaseModelData {
/// Create a new BaseModelData instance
pub fn new(id: Option<u32>) -> Self {
/// Create a new BaseModelData instance with ID set to 0
/// The ID will be auto-generated by OurDB when the model is saved
pub fn new() -> Self {
let now = chrono::Utc::now().timestamp();
Self {
id,
id: 0, // This will be replaced by OurDB with an auto-generated ID
created_at: now,
modified_at: now,
comments: Vec::new(),
@@ -124,8 +115,8 @@ impl BaseModelData {
}
/// Create a new BaseModelDataBuilder
pub fn builder(id: Option<u32>) -> BaseModelDataBuilder {
BaseModelDataBuilder::new(id)
pub fn builder() -> BaseModelDataBuilder {
BaseModelDataBuilder::new()
}
/// Add a comment to this model
@@ -144,11 +135,15 @@ impl BaseModelData {
pub fn update_modified(&mut self) {
self.modified_at = chrono::Utc::now().timestamp();
}
/// Update the ID of this model
pub fn update_id(&mut self, id: u32) {
self.id = id;
}
}
/// Builder for BaseModelData
pub struct BaseModelDataBuilder {
id: Option<u32>,
created_at: Option<i64>,
modified_at: Option<i64>,
comments: Vec<u32>,
@@ -156,9 +151,8 @@ pub struct BaseModelDataBuilder {
impl BaseModelDataBuilder {
/// Create a new BaseModelDataBuilder
pub fn new(id: Option<u32>) -> Self {
pub fn new() -> Self {
Self {
id,
created_at: None,
modified_at: None,
comments: Vec::new(),
@@ -193,7 +187,7 @@ impl BaseModelDataBuilder {
pub fn build(self) -> BaseModelData {
let now = chrono::Utc::now().timestamp();
BaseModelData {
id: self.id,
id: 0, // This will be replaced by OurDB with an auto-generated ID
created_at: self.created_at.unwrap_or(now),
modified_at: self.modified_at.unwrap_or(now),
comments: self.comments,