oxios_kernel/kernel_handle/
calendar_api.rs1use std::sync::Arc;
8
9use chrono::{DateTime, Utc};
10
11use oxios_calendar::{
12 AlarmEvent, CalendarEngine, CreateResult, Event, EventDraft, EventPatch, FreeBusySlot,
13 UpdateResult,
14};
15
16use crate::event_bus::{EventBus, KernelEvent};
17
18pub struct CalendarApi {
24 pub engine: Arc<CalendarEngine>,
26 event_bus: Option<EventBus>,
28}
29
30impl CalendarApi {
31 pub fn new(engine: Arc<CalendarEngine>) -> Self {
33 Self {
34 engine,
35 event_bus: None,
36 }
37 }
38
39 pub fn with_event_bus(engine: Arc<CalendarEngine>, event_bus: EventBus) -> Self {
41 Self {
42 engine,
43 event_bus: Some(event_bus),
44 }
45 }
46
47 pub async fn create(&self, draft: EventDraft) -> anyhow::Result<CreateResult> {
49 let title = draft.title.clone();
50 let start = draft.start.to_rfc3339();
51 let end = draft.end.to_rfc3339();
52 let result = self.engine.create(draft).await?;
53
54 if let Some(bus) = &self.event_bus {
55 let _ = bus.publish(KernelEvent::CalendarEventCreated {
56 uid: result.uid.clone(),
57 title,
58 start,
59 end,
60 });
61 }
62
63 Ok(result)
64 }
65
66 pub async fn update(&self, uid: &str, patch: EventPatch) -> anyhow::Result<UpdateResult> {
68 let result = self.engine.update(uid, patch).await?;
69
70 if let Some(bus) = &self.event_bus {
71 let title = self
73 .engine
74 .get(uid)
75 .await
76 .map(|e| e.title.clone())
77 .unwrap_or_default();
78
79 let _ = bus.publish(KernelEvent::CalendarEventUpdated {
80 uid: result.uid.clone(),
81 title,
82 });
83 }
84
85 Ok(result)
86 }
87
88 pub async fn delete(&self, uid: &str) -> anyhow::Result<()> {
90 let title = self
92 .engine
93 .get(uid)
94 .await
95 .map(|e| e.title.clone())
96 .unwrap_or_default();
97
98 self.engine.delete(uid).await?;
99
100 if let Some(bus) = &self.event_bus {
101 let _ = bus.publish(KernelEvent::CalendarEventDeleted {
102 uid: uid.to_string(),
103 title,
104 });
105 }
106
107 Ok(())
108 }
109
110 pub async fn get(&self, uid: &str) -> anyhow::Result<Event> {
112 self.engine.get(uid).await
113 }
114
115 pub async fn list(&self, from: DateTime<Utc>, to: DateTime<Utc>) -> anyhow::Result<Vec<Event>> {
117 self.engine.list(from, to).await
118 }
119
120 pub async fn search(&self, query: &str) -> anyhow::Result<Vec<Event>> {
122 self.engine.search(query).await
123 }
124
125 pub async fn freebusy(
127 &self,
128 from: DateTime<Utc>,
129 to: DateTime<Utc>,
130 ) -> anyhow::Result<Vec<FreeBusySlot>> {
131 self.engine.freebusy(from, to).await
132 }
133
134 pub fn find_pending_alarms(&self, from: DateTime<Utc>, to: DateTime<Utc>) -> Vec<AlarmEvent> {
136 self.engine.find_pending_alarms(from, to)
137 }
138}