feat: Enhance calendar example with user and attendee management

- Add user creation and management to the calendar example.
- Integrate user IDs into attendees for improved data integrity.
- Improve event manipulation by adding and removing attendees by ID.
- Enhance calendar example to demonstrate event and calendar retrieval.
- Enhance the calendar example with database storage for events.
- Modify the calendar example to manage events by ID instead of title.
This commit is contained in:
Mahmoud-Emad
2025-05-28 12:52:32 +03:00
parent d8e3d48caa
commit 331915c6cb
3 changed files with 518 additions and 166 deletions

View File

@@ -1,32 +1,50 @@
use chrono::{DateTime, Utc};
use heromodels_core::BaseModelData;
use heromodels_derive::model;
use rhai_autobind_macros::rhai_model_export;
use rhai::{CustomType, TypeBuilder};
use rhai_autobind_macros::rhai_model_export;
use serde::{Deserialize, Serialize};
/// Represents the status of an attendee for an event
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum AttendanceStatus {
Accepted,
Declined,
Tentative,
NoResponse,
Accepted = 0,
Declined = 1,
Tentative = 2,
NoResponse = 3,
}
/// Represents an attendee of an event
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Attendee {
/// Base model data
pub base_data: BaseModelData,
/// ID of the user attending
// Assuming user_id might be queryable
pub contact_id: u32,
/// Attendance status of the user for the event
pub status: AttendanceStatus,
}
impl Attendee {
/// Creates a new attendee with auto-generated ID
pub fn new(contact_id: u32) -> Self {
Self {
base_data: BaseModelData::new(), // ID will be auto-generated by OurDB
contact_id,
status: AttendanceStatus::NoResponse,
}
}
/// Creates a new attendee with optional ID (use None for auto-generated ID)
pub fn new_with_id(id: Option<u32>, contact_id: u32) -> Self {
let mut base_data = BaseModelData::new();
if let Some(id) = id {
base_data.update_id(id);
}
Self {
base_data,
contact_id,
status: AttendanceStatus::NoResponse,
}
@@ -39,10 +57,10 @@ impl Attendee {
}
/// Represents an event in a calendar
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
pub struct Event {
/// Base model data
#[serde(flatten)]
pub base_data: BaseModelData,
/// Title of the event
pub title: String,
@@ -52,17 +70,40 @@ pub struct Event {
pub start_time: DateTime<Utc>,
/// End time of the event
pub end_time: DateTime<Utc>,
/// List of attendees for the event
pub attendees: Vec<Attendee>,
/// List of attendee IDs for the event
pub attendees: Vec<u32>,
/// Optional location of the event
pub location: Option<String>,
}
impl Event {
/// Creates a new event
/// Creates a new event with auto-generated ID
pub fn new(title: impl ToString, start_time: DateTime<Utc>, end_time: DateTime<Utc>) -> Self {
Self {
base_data: BaseModelData::new(),
base_data: BaseModelData::new(), // ID will be auto-generated by OurDB
title: title.to_string(),
description: None,
start_time,
end_time,
attendees: Vec::new(),
location: None,
}
}
/// Creates a new event with optional ID (use None for auto-generated ID)
pub fn new_with_id(
id: Option<u32>,
title: impl ToString,
start_time: DateTime<Utc>,
end_time: DateTime<Utc>,
) -> Self {
let mut base_data = BaseModelData::new();
if let Some(id) = id {
base_data.update_id(id);
}
Self {
base_data,
title: title.to_string(),
description: None,
start_time,
@@ -90,26 +131,18 @@ impl Event {
self
}
/// Adds an attendee to the event
pub fn add_attendee(mut self, attendee: Attendee) -> Self {
// Prevent duplicate attendees by contact_id
if !self.attendees.iter().any(|a| a.contact_id == attendee.contact_id) {
self.attendees.push(attendee);
/// Adds an attendee ID to the event
pub fn add_attendee(mut self, attendee_id: u32) -> Self {
// Prevent duplicate attendees by ID
if !self.attendees.iter().any(|&a_id| a_id == attendee_id) {
self.attendees.push(attendee_id);
}
self
}
/// Removes an attendee from the event by user_id
pub fn remove_attendee(mut self, contact_id: u32) -> Self {
self.attendees.retain(|a| a.contact_id != contact_id);
self
}
/// Updates the status of an existing attendee
pub fn update_attendee_status(mut self, contact_id: u32, status: AttendanceStatus) -> Self {
if let Some(attendee) = self.attendees.iter_mut().find(|a| a.contact_id == contact_id) {
attendee.status = status;
}
/// Removes an attendee from the event by attendee ID
pub fn remove_attendee(mut self, attendee_id: u32) -> Self {
self.attendees.retain(|&a_id| a_id != attendee_id);
self
}
@@ -130,14 +163,11 @@ impl Event {
}
/// Represents a calendar with events
#[rhai_model_export(
db_type = "std::sync::Arc<crate::db::hero::OurDB>",
)]
#[rhai_model_export(db_type = "std::sync::Arc<crate::db::hero::OurDB>")]
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
pub struct Calendar {
/// Base model data
#[serde(flatten)]
pub base_data: BaseModelData,
/// Name of the calendar
@@ -194,7 +224,8 @@ impl Calendar {
/// Removes an event from the calendar by its ID
pub fn remove_event(mut self, event_id_to_remove: i64) -> Self {
self.events.retain(|&event_id_in_vec| event_id_in_vec != event_id_to_remove);
self.events
.retain(|&event_id_in_vec| event_id_in_vec != event_id_to_remove);
self
}
}