feat: Enhance calendar example with Event and Calendar features

- Add EventStatus enum to represent event states (Draft, Published, Cancelled).
- Extend Event model with color, category, reminder, timezone, created_by, all_day and is_recurring fields.
- Add color and description fields to Calendar model.
- Enhance calendar example to demonstrate new features, including event status changes, and detailed calendar information.
- Improve example output for clarity and comprehensiveness.
- Add owner_id and is_public fields to Calendar model for enhanced management.
2025-05-28 13:09:05,630 - INFO - Commit message copied to clipboard.
This commit is contained in:
Mahmoud-Emad 2025-05-28 14:01:25 +03:00
parent 331915c6cb
commit 8e2354df43
3 changed files with 377 additions and 31 deletions

View File

@ -1,7 +1,7 @@
use chrono::{Duration, Utc};
use heromodels::db::{Collection, Db};
use heromodels::models::User;
use heromodels::models::calendar::{AttendanceStatus, Attendee, Calendar, Event};
use heromodels::models::calendar::{AttendanceStatus, Attendee, Calendar, Event, EventStatus};
use heromodels_core::Model;
fn main() {
@ -83,7 +83,7 @@ fn main() {
);
// --- Create Events with Attendees ---
println!("\n--- Creating Events with Attendees ---");
println!("\n--- Creating Events with Enhanced Features ---");
let now = Utc::now();
let event1 = Event::new(
@ -93,6 +93,12 @@ fn main() {
)
.description("Weekly sync-up meeting to discuss project progress.")
.location("Conference Room A")
.color("#FF5722") // Red-orange color
.created_by(user1_id)
.status(EventStatus::Published)
.category("Work")
.reminder_minutes(15)
.timezone("UTC")
.add_attendee(attendee1_id)
.add_attendee(attendee2_id);
@ -103,6 +109,12 @@ fn main() {
)
.description("Brainstorming session for new project features.")
.location("Innovation Lab")
.color("#4CAF50") // Green color
.created_by(user2_id)
.status(EventStatus::Draft)
.category("Planning")
.reminder_minutes(30)
.is_recurring(true)
.add_attendee(attendee1_id)
.add_attendee(attendee3_id);
@ -112,9 +124,27 @@ fn main() {
now + Duration::days(2) + Duration::hours(1),
)
.description("Quarterly review with key client.")
.color("#9C27B0") // Purple color
.created_by(user3_id)
.status(EventStatus::Published)
.category("Client")
.reminder_minutes(60)
.add_attendee(attendee2_id);
println!("Created events:");
// Create an all-day event
let event4 = Event::new(
"Company Holiday",
now + Duration::days(7),
now + Duration::days(7) + Duration::hours(24),
)
.description("National holiday - office closed.")
.color("#FFC107") // Amber color
.all_day(true)
.created_by(user1_id)
.status(EventStatus::Published)
.category("Holiday");
println!("Created events with enhanced features:");
println!(
"- Event 1: '{}' at {} with {} attendees",
event1.title,
@ -128,6 +158,22 @@ fn main() {
.as_ref()
.unwrap_or(&"Not specified".to_string())
);
println!(
" Color: {}",
event1.color.as_ref().unwrap_or(&"Default".to_string())
);
println!(
" Category: {}",
event1.category.as_ref().unwrap_or(&"None".to_string())
);
println!(" Status: {:?}", event1.status);
println!(" Created by: User ID {}", event1.created_by.unwrap_or(0));
println!(
" Reminder: {} minutes before",
event1.reminder_minutes.unwrap_or(0)
);
println!(" All-day: {}", event1.all_day);
println!(" Recurring: {}", event1.is_recurring);
println!(" Attendee IDs: {:?}", event1.attendees);
println!(
@ -143,6 +189,22 @@ fn main() {
.as_ref()
.unwrap_or(&"Not specified".to_string())
);
println!(
" Color: {}",
event2.color.as_ref().unwrap_or(&"Default".to_string())
);
println!(
" Category: {}",
event2.category.as_ref().unwrap_or(&"None".to_string())
);
println!(" Status: {:?}", event2.status);
println!(" Created by: User ID {}", event2.created_by.unwrap_or(0));
println!(
" Reminder: {} minutes before",
event2.reminder_minutes.unwrap_or(0)
);
println!(" All-day: {}", event2.all_day);
println!(" Recurring: {}", event2.is_recurring);
println!(" Attendee IDs: {:?}", event2.attendees);
println!(
@ -151,8 +213,48 @@ fn main() {
event3.start_time.format("%Y-%m-%d %H:%M"),
event3.attendees.len()
);
println!(
" Location: {}",
event3
.location
.as_ref()
.unwrap_or(&"Not specified".to_string())
);
println!(
" Color: {}",
event3.color.as_ref().unwrap_or(&"Default".to_string())
);
println!(
" Category: {}",
event3.category.as_ref().unwrap_or(&"None".to_string())
);
println!(" Status: {:?}", event3.status);
println!(" Created by: User ID {}", event3.created_by.unwrap_or(0));
println!(
" Reminder: {} minutes before",
event3.reminder_minutes.unwrap_or(0)
);
println!(" All-day: {}", event3.all_day);
println!(" Recurring: {}", event3.is_recurring);
println!(" Attendee IDs: {:?}", event3.attendees);
println!(
"- Event 4: '{}' at {} (All-day: {})",
event4.title,
event4.start_time.format("%Y-%m-%d"),
event4.all_day
);
println!(
" Color: {}",
event4.color.as_ref().unwrap_or(&"Default".to_string())
);
println!(
" Category: {}",
event4.category.as_ref().unwrap_or(&"None".to_string())
);
println!(" Status: {:?}", event4.status);
println!(" Created by: User ID {}", event4.created_by.unwrap_or(0));
// --- Demonstrate Event Manipulation ---
println!("\n--- Demonstrating Event Manipulation ---");
@ -181,6 +283,54 @@ fn main() {
attendee3_id, updated_event1.title, updated_event1.attendees
);
// --- Demonstrate Event Status Changes ---
println!("\n--- Demonstrating Event Status Changes ---");
// Change event status from Draft to Published
let mut updated_event2 = event2.clone();
updated_event2 = updated_event2.status(EventStatus::Published);
println!(
"Changed '{}' status from Draft to Published",
updated_event2.title
);
// Cancel an event
let mut cancelled_event = event3.clone();
cancelled_event = cancelled_event.status(EventStatus::Cancelled);
println!("Cancelled event: '{}'", cancelled_event.title);
// Update event with new features
let enhanced_event = Event::new(
"Enhanced Meeting",
now + Duration::days(5),
now + Duration::days(5) + Duration::hours(2),
)
.description("Meeting with all new features demonstrated.")
.location("Virtual - Zoom")
.color("#673AB7") // Deep purple
.created_by(user1_id)
.status(EventStatus::Published)
.category("Demo")
.reminder_minutes(45)
.timezone("America/New_York")
.is_recurring(true)
.add_attendee(attendee1_id)
.add_attendee(attendee2_id)
.add_attendee(attendee3_id);
println!("Created enhanced event with all features:");
println!(" Title: {}", enhanced_event.title);
println!(" Status: {:?}", enhanced_event.status);
println!(" Category: {}", enhanced_event.category.as_ref().unwrap());
println!(" Color: {}", enhanced_event.color.as_ref().unwrap());
println!(" Timezone: {}", enhanced_event.timezone.as_ref().unwrap());
println!(" Recurring: {}", enhanced_event.is_recurring);
println!(
" Reminder: {} minutes",
enhanced_event.reminder_minutes.unwrap()
);
println!(" Attendees: {} people", enhanced_event.attendees.len());
// --- Store Events in Database ---
// Now that Event is a proper database model, we need to store events first
println!("\n--- Storing Events in Database ---");
@ -191,34 +341,94 @@ fn main() {
let (event1_id, stored_event1) = event_collection.set(&event1).expect("can set event1");
let (event2_id, stored_event2) = event_collection.set(&event2).expect("can set event2");
let (event3_id, stored_event3) = event_collection.set(&event3).expect("can set event3");
let (event4_id, stored_event4) = event_collection.set(&event4).expect("can set event4");
println!("Stored events in database:");
println!("- Event ID {}: '{}'", event1_id, stored_event1.title);
println!("- Event ID {}: '{}'", event2_id, stored_event2.title);
println!("- Event ID {}: '{}'", event3_id, stored_event3.title);
println!(
"- Event ID {}: '{}' (Status: {:?})",
event1_id, stored_event1.title, stored_event1.status
);
println!(
"- Event ID {}: '{}' (Status: {:?})",
event2_id, stored_event2.title, stored_event2.status
);
println!(
"- Event ID {}: '{}' (Status: {:?})",
event3_id, stored_event3.title, stored_event3.status
);
println!(
"- Event ID {}: '{}' (All-day: {})",
event4_id, stored_event4.title, stored_event4.all_day
);
// --- Create Calendars ---
// Now calendars store the actual database IDs of the events
println!("\n--- Creating Calendars ---");
println!("\n--- Creating Enhanced Calendars ---");
// Create a calendar with auto-generated ID and the stored event IDs
// Create a work calendar with enhanced features
let calendar1 = Calendar::new(None, "Work Calendar")
.description("Calendar for all work-related events.")
.owner_id(user1_id)
.is_public(false)
.color("#2196F3") // Blue color
.add_event(event1_id as i64)
.add_event(event2_id as i64);
// Create another calendar with auto-generated ID
let calendar2 = Calendar::new(None, "Personal Calendar").add_event(event3_id as i64);
// Create a personal calendar
let calendar2 = Calendar::new(None, "Personal Calendar")
.description("Personal events and appointments.")
.owner_id(user2_id)
.is_public(false)
.color("#E91E63") // Pink color
.add_event(event3_id as i64);
println!("Created calendars with event IDs:");
// Create a company-wide calendar
let calendar3 = Calendar::new(None, "Company Events")
.description("Company-wide events and holidays.")
.owner_id(user1_id)
.is_public(true)
.color("#FF9800") // Orange color
.add_event(event4_id as i64);
println!("Created calendars with enhanced features:");
println!(
"- Calendar 1: '{}' with events: {:?}",
calendar1.name, calendar1.events
"- Calendar 1: '{}' with {} events",
calendar1.name,
calendar1.events.len()
);
println!(" Owner: User ID {}", calendar1.owner_id.unwrap_or(0));
println!(" Public: {}", calendar1.is_public);
println!(
"- Calendar 2: '{}' with events: {:?}",
calendar2.name, calendar2.events
" Color: {}",
calendar1.color.as_ref().unwrap_or(&"Default".to_string())
);
println!(" Events: {:?}", calendar1.events);
println!(
"- Calendar 2: '{}' with {} events",
calendar2.name,
calendar2.events.len()
);
println!(" Owner: User ID {}", calendar2.owner_id.unwrap_or(0));
println!(" Public: {}", calendar2.is_public);
println!(
" Color: {}",
calendar2.color.as_ref().unwrap_or(&"Default".to_string())
);
println!(" Events: {:?}", calendar2.events);
println!(
"- Calendar 3: '{}' with {} events",
calendar3.name,
calendar3.events.len()
);
println!(" Owner: User ID {}", calendar3.owner_id.unwrap_or(0));
println!(" Public: {}", calendar3.is_public);
println!(
" Color: {}",
calendar3.color.as_ref().unwrap_or(&"Default".to_string())
);
println!(" Events: {:?}", calendar3.events);
// --- Store Calendars in DB ---
let cal_collection = db
@ -227,16 +437,28 @@ fn main() {
let (_, calendar1) = cal_collection.set(&calendar1).expect("can set calendar1");
let (_, calendar2) = cal_collection.set(&calendar2).expect("can set calendar2");
let (_, calendar3) = cal_collection.set(&calendar3).expect("can set calendar3");
println!(
"Created calendar1 (ID: {}): Name - '{}'",
"Created calendar1 (ID: {}): Name - '{}' (Owner: {}, Public: {})",
calendar1.get_id(),
calendar1.name
calendar1.name,
calendar1.owner_id.unwrap_or(0),
calendar1.is_public
);
println!(
"Created calendar2 (ID: {}): Name - '{}'",
"Created calendar2 (ID: {}): Name - '{}' (Owner: {}, Public: {})",
calendar2.get_id(),
calendar2.name
calendar2.name,
calendar2.owner_id.unwrap_or(0),
calendar2.is_public
);
println!(
"Created calendar3 (ID: {}): Name - '{}' (Owner: {}, Public: {})",
calendar3.get_id(),
calendar3.name,
calendar3.owner_id.unwrap_or(0),
calendar3.is_public
);
// --- Retrieve a Calendar by ID ---
@ -382,23 +604,36 @@ fn main() {
}
// --- List All Calendars ---
println!("\n--- Listing All Calendars ---");
println!("\n--- Listing All Enhanced Calendars ---");
let all_calendars = cal_collection.get_all().expect("can list all calendars");
for calendar in &all_calendars {
println!(
"- Calendar ID: {}, Name: '{}', Events: {:?}",
"- Calendar ID: {}, Name: '{}', Owner: {}, Public: {}, Color: {}",
calendar.get_id(),
calendar.name,
calendar.events
calendar.owner_id.unwrap_or(0),
calendar.is_public,
calendar.color.as_ref().unwrap_or(&"Default".to_string())
);
println!(
" Description: {}",
calendar.description.as_ref().unwrap_or(&"None".to_string())
);
println!(" Events: {:?}", calendar.events);
// Show which events are in this calendar
// Show which events are in this calendar with their details
for &event_id in &calendar.events {
if let Some(event) = event_collection
.get_by_id(event_id as u32)
.expect("can try to get event")
{
println!(" * Event: '{}'", event.title);
println!(
" * Event: '{}' (Status: {:?}, Category: {}, All-day: {})",
event.title,
event.status,
event.category.as_ref().unwrap_or(&"None".to_string()),
event.all_day
);
}
}
}

View File

@ -14,6 +14,14 @@ pub enum AttendanceStatus {
NoResponse = 3,
}
/// Represents the status of an event
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum EventStatus {
Draft = 0,
Published = 1,
Cancelled = 2,
}
/// Represents an attendee of an event
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
@ -74,6 +82,22 @@ pub struct Event {
pub attendees: Vec<u32>,
/// Optional location of the event
pub location: Option<String>,
/// Color for the event (hex color code)
pub color: Option<String>,
/// Whether this is an all-day event
pub all_day: bool,
/// ID of the user who created the event
pub created_by: Option<u32>,
/// Status of the event
pub status: EventStatus,
/// Whether this is a recurring event
pub is_recurring: bool,
/// Optional timezone for display purposes
pub timezone: Option<String>,
/// Optional category/tag for the event
pub category: Option<String>,
/// Optional reminder settings (minutes before event)
pub reminder_minutes: Option<i32>,
}
impl Event {
@ -87,6 +111,14 @@ impl Event {
end_time,
attendees: Vec::new(),
location: None,
color: Some("#4285F4".to_string()), // Default blue color
all_day: false,
created_by: None,
status: EventStatus::Published,
is_recurring: false,
timezone: None,
category: None,
reminder_minutes: None,
}
}
@ -110,6 +142,14 @@ impl Event {
end_time,
attendees: Vec::new(),
location: None,
color: Some("#4285F4".to_string()), // Default blue color
all_day: false,
created_by: None,
status: EventStatus::Published,
is_recurring: false,
timezone: None,
category: None,
reminder_minutes: None,
}
}
@ -131,6 +171,54 @@ impl Event {
self
}
/// Sets the color for the event
pub fn color(mut self, color: impl ToString) -> Self {
self.color = Some(color.to_string());
self
}
/// Sets whether this is an all-day event
pub fn all_day(mut self, all_day: bool) -> Self {
self.all_day = all_day;
self
}
/// Sets the creator of the event
pub fn created_by(mut self, user_id: u32) -> Self {
self.created_by = Some(user_id);
self
}
/// Sets the status of the event
pub fn status(mut self, status: EventStatus) -> Self {
self.status = status;
self
}
/// Sets whether this is a recurring event
pub fn is_recurring(mut self, is_recurring: bool) -> Self {
self.is_recurring = is_recurring;
self
}
/// Sets the timezone for the event
pub fn timezone(mut self, timezone: impl ToString) -> Self {
self.timezone = Some(timezone.to_string());
self
}
/// Sets the category for the event
pub fn category(mut self, category: impl ToString) -> Self {
self.category = Some(category.to_string());
self
}
/// Sets reminder minutes before the event
pub fn reminder_minutes(mut self, minutes: i32) -> Self {
self.reminder_minutes = Some(minutes);
self
}
/// Adds an attendee ID to the event
pub fn add_attendee(mut self, attendee_id: u32) -> Self {
// Prevent duplicate attendees by ID
@ -169,16 +257,18 @@ impl Event {
pub struct Calendar {
/// Base model data
pub base_data: BaseModelData,
/// Name of the calendar
pub name: String,
/// Optional description of the calendar
pub description: Option<String>,
/// List of events in the calendar
// For now, events are embedded. If they become separate models, this would be Vec<[IDType]>.
/// List of event IDs in the calendar
pub events: Vec<i64>,
/// ID of the user who owns this calendar
pub owner_id: Option<u32>,
/// Whether this calendar is public
pub is_public: bool,
/// Color theme for the calendar (hex color code)
pub color: Option<String>,
}
impl Calendar {
@ -198,6 +288,9 @@ impl Calendar {
name: name.to_string(),
description: None,
events: Vec::new(),
owner_id: None,
is_public: false,
color: Some("#4285F4".to_string()), // Default blue color
}
}
@ -213,6 +306,24 @@ impl Calendar {
self
}
/// Sets the owner of the calendar
pub fn owner_id(mut self, user_id: u32) -> Self {
self.owner_id = Some(user_id);
self
}
/// Sets whether the calendar is public
pub fn is_public(mut self, is_public: bool) -> Self {
self.is_public = is_public;
self
}
/// Sets the color for the calendar
pub fn color(mut self, color: impl ToString) -> Self {
self.color = Some(color.to_string());
self
}
/// Adds an event to the calendar
pub fn add_event(mut self, event_id: i64) -> Self {
// Prevent duplicate events by id

View File

@ -2,6 +2,6 @@
pub mod calendar;
pub mod rhai;
// Re-export Calendar, Event, Attendee, and AttendanceStatus from the inner calendar module (calendar.rs) within src/models/calendar/mod.rs
pub use self::calendar::{Calendar, Event, Attendee, AttendanceStatus};
// Re-export Calendar, Event, Attendee, AttendanceStatus, and EventStatus from the inner calendar module (calendar.rs) within src/models/calendar/mod.rs
pub use self::calendar::{AttendanceStatus, Attendee, Calendar, Event, EventStatus};
pub use rhai::register_rhai_engine_functions as register_calendar_rhai_module;