final db models wip

This commit is contained in:
timurgordon
2025-06-03 21:50:08 +03:00
parent 2a2d69dafb
commit abbed9a1a1
43 changed files with 2958 additions and 1410 deletions

View File

@@ -1,21 +1,46 @@
use chrono::{DateTime, Utc};
use heromodels_core::BaseModelData;
use heromodels_derive::model;
use rhai_autobind_macros::rhai_model_export;
// Temporarily removed to fix compilation issues
// 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)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
pub enum AttendanceStatus {
Accepted,
Declined,
Tentative,
#[default]
NoResponse,
}
impl AttendanceStatus {
/// Convert a string to an AttendanceStatus
pub fn from_string(s: &str) -> Result<Self, String> {
match s {
"Accepted" => Ok(AttendanceStatus::Accepted),
"Declined" => Ok(AttendanceStatus::Declined),
"Tentative" => Ok(AttendanceStatus::Tentative),
"NoResponse" => Ok(AttendanceStatus::NoResponse),
_ => Err(format!("Invalid attendance status: '{}'", s)),
}
}
/// Convert an AttendanceStatus to a string
pub fn to_string(&self) -> String {
match self {
AttendanceStatus::Accepted => "Accepted".to_string(),
AttendanceStatus::Declined => "Declined".to_string(),
AttendanceStatus::Tentative => "Tentative".to_string(),
AttendanceStatus::NoResponse => "NoResponse".to_string(),
}
}
}
/// Represents an attendee of an event
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType, Default)]
pub struct Attendee {
/// ID of the user attending
// Assuming user_id might be queryable
@@ -39,34 +64,51 @@ 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
#[index]
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>,
/// Start time of the event (Unix timestamp)
pub start_time: i64,
/// End time of the event (Unix timestamp)
pub end_time: i64,
/// 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(title: impl ToString, start_time: DateTime<Utc>, end_time: DateTime<Utc>) -> Self {
impl Default for Event {
fn default() -> Self {
let now = chrono::Utc::now().timestamp();
Self {
base_data: BaseModelData::new(),
title: title.to_string(),
title: String::new(),
description: None,
start_time,
end_time,
start_time: now,
end_time: now + 3600, // Add 1 hour in seconds
attendees: Vec::new(),
location: None,
}
}
}
impl Event {
/// Creates a new event
pub fn new() -> Self {
let now = chrono::Utc::now().timestamp();
Self {
base_data: BaseModelData::new(),
title: String::new(),
description: None,
start_time: now,
end_time: now + 3600, // Add 1 hour in seconds
attendees: Vec::new(),
location: None,
}
@@ -116,8 +158,8 @@ impl Event {
/// Reschedules the event to new start and end times
pub fn reschedule(
mut self,
new_start_time: DateTime<Utc>,
new_end_time: DateTime<Utc>,
new_start_time: i64,
new_end_time: i64,
) -> Self {
// Basic validation: end_time should be after start_time
if new_end_time > new_start_time {
@@ -130,17 +172,18 @@ impl Event {
}
/// Represents a calendar with events
#[rhai_model_export(
db_type = "std::sync::Arc<crate::db::hero::OurDB>",
)]
// Temporarily removed rhai_model_export macro to fix compilation issues
// #[rhai_model_export(
// db_type = "std::sync::Arc<crate::db::hero::OurDB>",
// )]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[model]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, CustomType)]
pub struct Calendar {
/// Base model data
#[serde(flatten)]
pub base_data: BaseModelData,
/// Name of the calendar
#[index]
pub name: String,
/// Optional description of the calendar