aboutsummaryrefslogtreecommitdiff
path: root/src/infrastructure/persistence/mappers.rs
blob: 4260038e5c3c9cbf6138e83cd5422650c795811c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
use chrono::{DateTime, Utc};
use crate::domain::{
    calendar::Calendar,
    event::Event,
    recurrence::{
        ExceptionModification,
        RecurrenceException,
        RecurrenceRule,
        RecurringEvent,
    },
    value_objects::{
        CalendarId,
        EventColor,
        EventId,
        Frequency,
        TimeRange,
    },
};
use super::models::{
    CalendarModel,
    EventModel,
    RecurrenceModel,
    RecurrenceExceptionModel
};
use std::{collections::HashMap, str::FromStr};

pub struct CalendarMapper;

impl CalendarMapper {
    pub fn to_domain(model: CalendarModel) -> Result<Calendar, String> {
        let id = CalendarId::from_str(&model.id)
            .map_err(|e| format!("Invalid calendar ID: {}", e))?;

        let created_at = DateTime::parse_from_rfc3339(&model.created_at)
            .map_err(|e| format!("Invalid created_at: {}", e))?
            .with_timezone(&Utc);

        let updated_at = DateTime::parse_from_rfc3339(&model.updated_at)
            .map_err(|e| format!("Invalid updated_at: {}", e))?
            .with_timezone(&Utc);

        Ok(Calendar::with_id(
            id,
            model.name,
            model.description,
            model.is_archived != 0,
            created_at,
            updated_at,
        ))
    }

    pub fn to_model(calendar: &Calendar) -> CalendarModel {
        CalendarModel {
            id: calendar.id().to_string(),
            name: calendar.name().to_string(),
            description: calendar.description().clone(),
            is_archived: if *calendar.is_archived() { 1 } else { 0 },
            created_at: calendar.created_at().to_rfc3339(),
            updated_at: calendar.updated_at().to_rfc3339(),
        }
    }
}

pub struct EventMapper;

impl EventMapper {
    pub fn to_domain(model: EventModel) -> Result<Event, String> {
        let id = EventId::from_str(&model.id)
            .map_err(|e| format!("Invalid event ID: {}", e))?;

        let calendar_id = CalendarId::from_str(&model.calendar_id)
            .map_err(|e| format!("Invalid calendar ID: {}", e))?;

        let starts_at = DateTime::parse_from_rfc3339(&model.starts_at)
            .map_err(|e| format!("Invalid starts_at: {}", e))?
            .with_timezone(&Utc);

        let ends_at = DateTime::parse_from_rfc3339(&model.ends_at)
            .map_err(|e| format!("Invalid ends_at: {}", e))?
            .with_timezone(&Utc);

        let created_at = DateTime::parse_from_rfc3339(&model.created_at)
            .map_err(|e| format!("Invalid created_at: {}", e))?
            .with_timezone(&Utc);

        let updated_at = DateTime::parse_from_rfc3339(&model.updated_at)
            .map_err(|e| format!("Invalid updated_at: {}", e))?
            .with_timezone(&Utc);

        let time_range = TimeRange::new(starts_at, ends_at)
            .map_err(|e| e.to_string())?;

        let color = EventColor::from(model.color as u8);

        Ok(Event::with_id(
            id,
            calendar_id,
            model.title,
            model.description,
            time_range,
            color,
            model.is_all_day != 0,
            model.is_cancelled != 0,
            created_at,
            updated_at,
        ))
    }
    
    pub fn to_model(event: &Event) -> EventModel {
        EventModel {
            id: event.id().to_string(),
            calendar_id: event.calendar_id().to_string(),
            title: event.title().to_string(),
            description: event.description().clone(),
            starts_at: event.time_range().starts_at().to_rfc3339(),
            ends_at: event.time_range().ends_at().to_rfc3339(),
            color: u8::from(*event.color()) as i64,
            is_all_day: if *event.is_all_day() { 1 } else { 0 },
            is_cancelled: if *event.is_cancelled() { 1 } else { 0 },
            created_at: event.created_at().to_rfc3339(),
            updated_at: event.updated_at().to_rfc3339(),
        }
    }
}

pub struct RecurrenceMapper;

impl RecurrenceMapper {
    pub fn to_domain(
        model: RecurrenceModel,
        exceptions: Vec<RecurrenceExceptionModel>,
    ) -> Result<RecurringEvent, String> {
        let id = EventId::from_str(&model.id)
            .map_err(|e| format!("Invalid event ID: {}", e))?;

        let calendar_id = CalendarId::from_str(&model.calendar_id)
            .map_err(|e| format!("Invalid calendar ID: {}", e))?;

        let starts_at = DateTime::parse_from_rfc3339(&model.starts_at)
            .map_err(|e| format!("Invalid starts_at: {}", e))?
            .with_timezone(&Utc);

        let ends_at = DateTime::parse_from_rfc3339(&model.ends_at)
            .map_err(|e| format!("Invalid ends_at: {}", e))?
            .with_timezone(&Utc);

        let created_at = DateTime::parse_from_rfc3339(&model.created_at)
            .map_err(|e| format!("Invalid created_at: {}", e))?
            .with_timezone(&Utc);

        let updated_at = DateTime::parse_from_rfc3339(&model.updated_at)
            .map_err(|e| format!("Invalid updated_at: {}", e))?
            .with_timezone(&Utc);

        let base_time_range = TimeRange::new(starts_at, ends_at)
            .map_err(|e| e.to_string())?;

        let color = EventColor::from(model.color as u8);

        let frequency = Frequency::from_str(&model.frequency)
            .map_err(|e| e.to_string())?;

        let until = if let Some(until_str) = model.until {
            Some(DateTime::parse_from_rfc3339(&until_str)
                .map_err(|e| format!("Invalid until: {}", e))?
                .with_timezone(&Utc))
        } else {
            None
        };

        let recurrence_rule = RecurrenceRule::new(
            frequency,
            model.interval as u32,
            until,
        ).map_err(|e| e.to_string())?;

        let exception_map = exceptions
            .into_iter()
            .map(Self::exception_to_domain)
            .collect::<Result<Vec<_>, _>>()?
            .into_iter()
            .map(|ex| (*ex.original_starts_at(), ex))
            .collect::<HashMap<DateTime<Utc>, RecurrenceException>>();

        Ok(RecurringEvent::with_id(
            id,
            calendar_id,
            model.title,
            model.description,
            base_time_range,
            recurrence_rule,
            exception_map,
            color,
            model.is_all_day != 0,
            model.is_cancelled != 0,
            created_at,
            updated_at,
        ))
    }

    pub fn to_model(event: &RecurringEvent) -> RecurrenceModel {
        RecurrenceModel {
            id: event.id().to_string(),
            calendar_id: event.calendar_id().to_string(),
            title: event.title().to_string(),
            description: event.description().clone(),
            starts_at: event.time_range().starts_at().to_rfc3339(),
            ends_at: event.time_range().ends_at().to_rfc3339(),
            frequency: event.rule().frequency().to_string(),
            interval: *event.rule().interval() as i64,
            until: event.rule().until().map(|dt| dt.to_rfc3339()),
            color: u8::from(*event.color()) as i64,
            is_all_day: if *event.is_all_day() { 1 } else { 0 },
            is_cancelled: if *event.is_cancelled() { 1 } else { 0 },
            created_at: event.created_at().to_rfc3339(),
            updated_at: event.updated_at().to_rfc3339(),
        }
    }

    fn exception_to_domain(
        model: RecurrenceExceptionModel,
    ) -> Result<RecurrenceException, String> {
        let original_starts_at = DateTime::parse_from_rfc3339(
            &model.original_starts_at
        )
            .map_err(|e| format!("Invalid original_starts_at: {}", e))?
            .with_timezone(&Utc);

        if model.is_cancelled != 0 {
            Ok(RecurrenceException::cancelled(original_starts_at))
        } else if let (Some(new_starts), Some(new_ends)) = 
            (model.new_starts_at, model.new_ends_at) {
            let starts = DateTime::parse_from_rfc3339(&new_starts)
                .map_err(|e| format!("Invalid new_starts_at: {}", e))?
                .with_timezone(&Utc);

            let ends = DateTime::parse_from_rfc3339(&new_ends)
                .map_err(|e| format!("Invalid new_ends_at: {}", e))?
                .with_timezone(&Utc);

            let new_time_range = TimeRange::new(starts, ends)
                .map_err(|e| e.to_string())?;

            Ok(RecurrenceException::rescheduled(
                original_starts_at,
                new_time_range,
            ))
        } else {
            Err(
                "Exception must be either cancelled or have new time range"
                .to_string()
            )
        }
    }

    pub fn exception_to_model(
        exception: &RecurrenceException,
        recurrence_id: &EventId,
    ) -> RecurrenceExceptionModel {
        let (new_starts_at, new_ends_at, is_cancelled) = 
            match exception.modification() {
                ExceptionModification::Cancelled => (None, None, 1),
                ExceptionModification::Rescheduled { new_time_range } => (
                    Some(new_time_range.starts_at().to_rfc3339()),
                    Some(new_time_range.ends_at().to_rfc3339()),
                    0,
                ),
            };

        RecurrenceExceptionModel {
            recurrence_id: recurrence_id.to_string(),
            original_starts_at: exception.original_starts_at().to_rfc3339(),
            new_starts_at,
            new_ends_at,
            is_cancelled,
        }
    }
}