aboutsummaryrefslogtreecommitdiff
path: root/src/domain/calendar.rs
blob: 9e6a7fee146a996d7dd93e7d4ae4c8ae5b7d4153 (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
use std::collections::BTreeMap;

use chrono::{DateTime, NaiveDate, Utc};
use getset::Getters;
use uuid::Uuid;

use crate::domain::Event;

#[derive(Clone, Debug, Getters)]
#[getset(get = "pub")]
pub struct Calendar {
    id: Uuid,
    name: String,
    description: Option<String>,
    events: BTreeMap<NaiveDate, Vec<Event>>,
    is_archived: bool,
    created_at: DateTime<Utc>,
    updated_at: DateTime<Utc>,
}

pub struct CalendarBuilder {
    name: String,
    description: Option<String>,
    events: BTreeMap<NaiveDate, Vec<Event>>,
    is_archived: bool,
}

impl CalendarBuilder {
    pub fn new(name: String) -> Result<Self, String> {
        if name.is_empty() {
            Err("Name cannot be empty".to_string())
        } else {
            Ok(Self {
                name,
                description: None,
                events: BTreeMap::new(),
                is_archived: false
            })
        }
    }

    pub fn description(mut self, description: Option<String>) -> Self {
        self.description = description;
        self
    }

    pub fn events(mut self, events: BTreeMap<NaiveDate, Vec<Event>>) -> Self {
        self.events = events;
        self
    }
    
    pub fn is_archived(mut self, is_archived: bool) -> Self {
        self.is_archived = is_archived;
        self
    }

    pub fn build(self) -> Calendar {
        let now = Utc::now();
        Calendar {
            id: Uuid::new_v4(),
            name: self.name,
            description: self.description,
            events: self.events,
            is_archived: self.is_archived,
            created_at: now,
            updated_at: now
        }
    }
}

impl Calendar {
    pub fn add_events(&mut self, events: Vec<Event>) {
        for event in events {
            let date = event.start().date_naive();

            self.events
                .entry(date)
                .or_insert_with(Vec::new)
                .push(event);
        }

        self.touch()
    }

    pub fn update_name(&mut self, name: String) -> Result<(), String> {
        if name.is_empty() {
            Err("Name cannot be empty".to_string())
        } else {
            self.name = name;
            self.touch();
            Ok(())
        }
    }

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

    pub fn update_archived(&mut self, is_archived: bool) {
        self.is_archived = is_archived;
        self.touch()
    }

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