implement more models and rhai examples

This commit is contained in:
timurgordon
2025-05-22 03:57:03 +03:00
parent aa8ef90f9f
commit 56ec505874
79 changed files with 4546 additions and 182 deletions

View File

@@ -1,9 +1,9 @@
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};
use serde::{Deserialize, Serialize};
/// Represents the status of an attendee for an event
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
@@ -15,19 +15,19 @@ pub enum AttendanceStatus {
}
/// Represents an attendee of an event
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
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
pub contact_id: u32,
/// Attendance status of the user for the event
pub status: AttendanceStatus,
}
impl Attendee {
pub fn new(user_id: String) -> Self {
pub fn new(contact_id: u32) -> Self {
Self {
user_id,
contact_id,
status: AttendanceStatus::NoResponse,
}
}
@@ -39,11 +39,11 @@ impl Attendee {
}
/// Represents an event in a calendar
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
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,
/// Base model data
#[serde(flatten)]
pub base_data: BaseModelData,
/// Title of the event
pub title: String,
/// Optional description of the event
@@ -60,18 +60,24 @@ pub struct Event {
impl Event {
/// Creates a new event
pub fn new(id: String, title: impl ToString, start_time: DateTime<Utc>, end_time: DateTime<Utc>) -> Self {
pub fn new(id: i64) -> Self {
Self {
id,
title: title.to_string(),
base_data: BaseModelData::new(id as u32),
title: String::new(),
description: None,
start_time,
end_time,
start_time: Utc::now(),
end_time: Utc::now(),
attendees: Vec::new(),
location: None,
}
}
/// Sets the title for the event
pub fn title(mut self, title: impl ToString) -> Self {
self.title = title.to_string();
self
}
/// Sets the description for the event
pub fn description(mut self, description: impl ToString) -> Self {
self.description = Some(description.to_string());
@@ -86,22 +92,22 @@ impl Event {
/// 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) {
// Prevent duplicate attendees by contact_id
if !self.attendees.iter().any(|a| a.contact_id == attendee.contact_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);
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, user_id: &str, status: AttendanceStatus) -> Self {
if let Some(attendee) = self.attendees.iter_mut().find(|a| a.user_id == user_id) {
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;
}
self
@@ -120,11 +126,14 @@ 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, CustomType)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
pub struct Calendar {
/// Base model data
#[serde(flatten)]
pub base_data: BaseModelData,
/// Name of the calendar
@@ -135,20 +144,26 @@ pub struct Calendar {
/// 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>,
pub events: Vec<i64>,
}
impl Calendar {
/// Creates a new calendar
pub fn new(id: u32, name: impl ToString) -> Self {
pub fn new(id: u32) -> Self {
Self {
base_data: BaseModelData::new(id),
name: name.to_string(),
base_data: BaseModelData::new(id as u32),
name: String::new(),
description: None,
events: Vec::new(),
}
}
/// Sets the name for the calendar
pub fn name(mut self, name: impl ToString) -> Self {
self.name = name.to_string();
self
}
/// Sets the description for the calendar
pub fn description(mut self, description: impl ToString) -> Self {
self.description = Some(description.to_string());
@@ -156,29 +171,17 @@ impl Calendar {
}
/// Adds an event to the calendar
pub fn add_event(mut self, event: Event) -> Self {
pub fn add_event(mut self, event_id: i64) -> Self {
// Prevent duplicate events by id
if !self.events.iter().any(|e| e.id == event.id) {
self.events.push(event);
if !self.events.iter().any(|e_id| *e_id == event_id) {
self.events.push(event_id);
}
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));
}
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
}
}