db/herodb/src/models/mcc/calendar.rs
2025-04-22 07:50:03 +04:00

56 lines
1.7 KiB
Rust

use serde::{Deserialize, Serialize};
use crate::models::mcc::event::Event;
use crate::db::model::impl_get_id;
/// Calendar represents a calendar container for events
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Calendar {
pub id: u32, // Unique identifier
pub title: String, // Calendar title
pub description: String, // Calendar details
pub groups: Vec<u32>, // Groups this calendar belongs to (references Circle IDs)
}
impl Calendar {
/// Create a new calendar
pub fn new(id: u32, title: String, description: String) -> Self {
Self {
id,
title,
description,
groups: Vec::new(),
}
}
/// Add a group to this calendar
pub fn add_group(&mut self, group_id: u32) {
if !self.groups.contains(&group_id) {
self.groups.push(group_id);
}
}
/// Remove a group from this calendar
pub fn remove_group(&mut self, group_id: u32) {
self.groups.retain(|&id| id != group_id);
}
/// Filter by groups - returns true if this calendar belongs to any of the specified groups
pub fn filter_by_groups(&self, groups: &[u32]) -> bool {
groups.iter().any(|g| self.groups.contains(g))
}
/// Filter events by this calendar's ID
pub fn filter_events<'a>(&self, events: &'a [Event]) -> Vec<&'a Event> {
events.iter()
.filter(|event| event.calendar_id == self.id)
.collect()
}
/// Get the database prefix for this model type
pub fn db_prefix() -> &'static str {
"calendar"
}
}
// Automatically implement GetId trait for Calendar
impl_get_id!(Calendar);