From 4e78fd83349c95711cdee5acc56f248f81ebd25c Mon Sep 17 00:00:00 2001 From: Mikkel Thestrup Date: Mon, 26 Jan 2026 21:27:59 +0100 Subject: feat(domain): add core domain models for calendar application Implement domain-driven design layer with entities, value objects, and repository traits for calendar management system. Domain Entities: - Calendar: manages calendar lifecycle and archival state - Event: handles single occurrence events with cancellation support - RecurringEvent: supports recurring events with exception handling - RecurrenceException: manages modifications to specific occurrences Value Objects: - TimeRange: enforces valid start/end time constraints - EventColor: type-safe color representation - Frequency: recurrence frequency enumeration (daily/weekly/monthly/yearly) - RecurrenceRule: encapsulates recurrence pattern logic - CalendarId & EventId: essentially just a wrapper Repository Traits: - CalendarRepository: calendar persistence interface - EventRepository: event querying and persistence with overlap detection - RecurrenceRepository: recurring event management Key Design Decisions: - Use Uuid for entity IDs (type safety, performance) - Encapsulate business logic within entities - Immutable value objects with validation - Repository pattern for infrastructure abstraction - Clear separation of concerns between domain and persistence layers All entities include timestamp tracking and follow builder pattern for construction with validation at domain boundaries. --- src/domain/event.rs | 116 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/domain/event.rs (limited to 'src/domain/event.rs') diff --git a/src/domain/event.rs b/src/domain/event.rs new file mode 100644 index 0000000..93216a4 --- /dev/null +++ b/src/domain/event.rs @@ -0,0 +1,116 @@ +use chrono::{DateTime, Utc}; +use getset::Getters; + +use crate::domain::value_objects::{CalendarId, EventColor, EventId, TimeRange}; + +#[derive(Debug, Clone, Getters)] +pub struct Event { + #[getset(get = "pub")] + id: EventId, + #[getset(get = "pub")] + calendar_id: CalendarId, + #[getset(get = "pub")] + title: String, + #[getset(get = "pub")] + description: Option, + #[getset(get = "pub")] + time_range: TimeRange, + #[getset(get = "pub")] + color: EventColor, + #[getset(get = "pub")] + is_all_day: bool, + #[getset(get = "pub")] + is_cancelled: bool, + #[getset(get = "pub")] + created_at: DateTime, + #[getset(get = "pub")] + updated_at: DateTime, +} + +impl Event { + pub fn new( + calendar_id: CalendarId, + title: String, + description: Option, + time_range: TimeRange, + color: EventColor, + is_all_day: bool, + ) -> Self { + let now = Utc::now(); + Self { + id: EventId::new(), + calendar_id, + title, + description, + time_range, + color, + is_all_day, + is_cancelled: false, + created_at: now, + updated_at: now, + } + } + + pub fn with_id( + id: EventId, + calendar_id: CalendarId, + title: String, + description: Option, + time_range: TimeRange, + color: EventColor, + is_all_day: bool, + is_cancelled: bool, + created_at: DateTime, + updated_at: DateTime, + ) -> Self { + Self { + id, + calendar_id, + title, + description, + time_range, + color, + is_all_day, + is_cancelled, + created_at, + updated_at, + } + } + + pub fn cancel(&mut self) { + self.is_cancelled = true; + self.updated_at = Utc::now(); + } + + pub fn restore(&mut self) { + self.is_cancelled = false; + self.updated_at = Utc::now(); + } + + pub fn update_title(&mut self, title: String) { + self.title = title; + self.updated_at = Utc::now(); + } + + pub fn update_description(&mut self, description: Option) { + self.description = description; + self.updated_at = Utc::now(); + } + + pub fn update_time_range(&mut self, time_range: TimeRange) { + self.time_range = time_range; + self.updated_at = Utc::now(); + } + + pub fn update_color(&mut self, color: EventColor) { + self.color = color; + self.updated_at = Utc::now(); + } + + pub fn overlaps_with(&self, other: &Event) -> bool { + !self.is_cancelled + && !other.is_cancelled + && self.calendar_id == other.calendar_id + && self.time_range.overlaps(other.time_range()) + } +} -- cgit v1.2.3-70-g09d2