aboutsummaryrefslogtreecommitdiff
path: root/src/domain/event.rs
blob: 0772020c22addd268fdf11d366268d1cd9ba18aa (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
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<String>,
    #[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<Utc>,
    #[getset(get = "pub")]
    updated_at: DateTime<Utc>,
}

impl Event {
    pub fn new(
        calendar_id: CalendarId,
        title: String,
        description: Option<String>,
        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<String>,
        time_range: TimeRange,
        color: EventColor,
        is_all_day: bool,
        is_cancelled: bool,
        created_at: DateTime<Utc>,
        updated_at: DateTime<Utc>,
    ) -> 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.touch();
    }

    pub fn restore(&mut self) {
        self.is_cancelled = false;
        self.touch();
    }

    pub fn update_title(&mut self, title: String) {
        self.title = title;
        self.touch();
    }

    pub fn update_description(&mut self, description: Option<String>) {
        self.description = description;
        self.touch();
    }

    pub fn update_time_range(&mut self, time_range: TimeRange) {
        self.time_range = time_range;
        self.touch();
    }

    pub fn update_color(&mut self, color: EventColor) {
        self.color = color;
        self.touch();
    }

    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())
    }

    pub fn touch(&mut self) {
        self.updated_at = Utc::now();
    }
}