start porting model specs into heromodels
This commit is contained in:
184
heromodels/src/models/calendar/calendar.rs
Normal file
184
heromodels/src/models/calendar/calendar.rs
Normal file
@@ -0,0 +1,184 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use heromodels_core::BaseModelData;
|
||||
use heromodels_derive::model;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use rhai_autobind_macros::rhai_model_export;
|
||||
use rhai::{CustomType, TypeBuilder};
|
||||
|
||||
/// Represents the status of an attendee for an event
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum AttendanceStatus {
|
||||
Accepted,
|
||||
Declined,
|
||||
Tentative,
|
||||
NoResponse,
|
||||
}
|
||||
|
||||
/// Represents an attendee of an event
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Attendee {
|
||||
/// ID of the user attending
|
||||
// Assuming user_id might be queryable
|
||||
pub user_id: String, // Using String for user_id similar to potential external IDs
|
||||
/// Attendance status of the user for the event
|
||||
pub status: AttendanceStatus,
|
||||
}
|
||||
|
||||
impl Attendee {
|
||||
pub fn new(user_id: String) -> Self {
|
||||
Self {
|
||||
user_id,
|
||||
status: AttendanceStatus::NoResponse,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn status(mut self, status: AttendanceStatus) -> Self {
|
||||
self.status = status;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents an event in a calendar
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Event {
|
||||
/// Unique identifier for the event (e.g., could be a UUID string or u32 if internally managed)
|
||||
// Events might be looked up by their ID
|
||||
pub id: String,
|
||||
/// Title of the event
|
||||
pub title: String,
|
||||
/// Optional description of the event
|
||||
pub description: Option<String>,
|
||||
/// Start time of the 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>,
|
||||
/// Optional location of the event
|
||||
pub location: Option<String>,
|
||||
}
|
||||
|
||||
impl Event {
|
||||
/// Creates a new event
|
||||
pub fn new(id: String, title: impl ToString, start_time: DateTime<Utc>, end_time: DateTime<Utc>) -> Self {
|
||||
Self {
|
||||
id,
|
||||
title: title.to_string(),
|
||||
description: None,
|
||||
start_time,
|
||||
end_time,
|
||||
attendees: Vec::new(),
|
||||
location: None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the description for the event
|
||||
pub fn description(mut self, description: impl ToString) -> Self {
|
||||
self.description = Some(description.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the location for the event
|
||||
pub fn location(mut self, location: impl ToString) -> Self {
|
||||
self.location = Some(location.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
/// Adds an attendee to the event
|
||||
pub fn add_attendee(mut self, attendee: Attendee) -> Self {
|
||||
// Prevent duplicate attendees by user_id
|
||||
if !self.attendees.iter().any(|a| a.user_id == attendee.user_id) {
|
||||
self.attendees.push(attendee);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Removes an attendee from the event by user_id
|
||||
pub fn remove_attendee(mut self, user_id: &str) -> Self {
|
||||
self.attendees.retain(|a| a.user_id != user_id);
|
||||
self
|
||||
}
|
||||
|
||||
/// Updates the status of an existing attendee
|
||||
pub fn update_attendee_status(mut self, user_id: &str, status: AttendanceStatus) -> Self {
|
||||
if let Some(attendee) = self.attendees.iter_mut().find(|a| a.user_id == user_id) {
|
||||
attendee.status = status;
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Reschedules the event to new start and end times
|
||||
pub fn reschedule(mut self, new_start_time: DateTime<Utc>, new_end_time: DateTime<Utc>) -> Self {
|
||||
// Basic validation: end_time should be after start_time
|
||||
if new_end_time > new_start_time {
|
||||
self.start_time = new_start_time;
|
||||
self.end_time = new_end_time;
|
||||
}
|
||||
// Optionally, add error handling or return a Result type
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a calendar with events
|
||||
#[rhai_model_export(db_type = "std::sync::Arc<crate::db::hero::OurDB>")]
|
||||
#[model]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, CustomType)]
|
||||
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]>.
|
||||
pub events: Vec<Event>,
|
||||
}
|
||||
|
||||
impl Calendar {
|
||||
/// Creates a new calendar
|
||||
pub fn new(id: u32, name: impl ToString) -> Self {
|
||||
Self {
|
||||
base_data: BaseModelData::new(id),
|
||||
name: name.to_string(),
|
||||
description: None,
|
||||
events: Vec::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Sets the description for the calendar
|
||||
pub fn description(mut self, description: impl ToString) -> Self {
|
||||
self.description = Some(description.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
/// Adds an event to the calendar
|
||||
pub fn add_event(mut self, event: Event) -> Self {
|
||||
// Prevent duplicate events by id
|
||||
if !self.events.iter().any(|e| e.id == event.id) {
|
||||
self.events.push(event);
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
/// Removes an event from the calendar by its ID
|
||||
pub fn remove_event(mut self, event_id: &str) -> Self {
|
||||
self.events.retain(|event| event.id != event_id);
|
||||
self
|
||||
}
|
||||
|
||||
/// Finds an event by its ID and allows modification
|
||||
pub fn update_event<F>(mut self, event_id: &str, update_fn: F) -> Self
|
||||
where
|
||||
F: FnOnce(Event) -> Event,
|
||||
{
|
||||
if let Some(index) = self.events.iter().position(|e| e.id == event_id) {
|
||||
let event = self.events.remove(index);
|
||||
self.events.insert(index, update_fn(event));
|
||||
}
|
||||
self
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user