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
//! Contains the implementations related to [`ShadingDto`].

use uuid::Uuid;

use crate::model::{
    dto::shadings::{
        NewShadingDto, ShadingDto, UpdateAddDateShadingDto, UpdateNotesShadingDto,
        UpdateRemoveDateShadingDto, UpdateShadingDto, UpdateValuesShadingDto,
    },
    entity::shadings::{Shading, UpdateShading},
};

impl From<Shading> for ShadingDto {
    fn from(entity: Shading) -> Self {
        Self {
            id: entity.id,
            layer_id: entity.layer_id,
            shade: entity.shade,
            geometry: entity.geometry,
            add_date: entity.add_date,
            remove_date: entity.remove_date,
            notes: entity.notes,
        }
    }
}

impl From<NewShadingDto> for Shading {
    fn from(dto: NewShadingDto) -> Self {
        Self {
            id: dto.id.unwrap_or_else(Uuid::new_v4),
            layer_id: dto.layer_id,
            shade: dto.shade,
            geometry: dto.geometry,
            add_date: dto.add_date,
            remove_date: None,
            notes: String::new(),
        }
    }
}

impl From<UpdateShadingDto> for Vec<UpdateShading> {
    fn from(dto: UpdateShadingDto) -> Self {
        match dto {
            UpdateShadingDto::Update(vec) => vec.into_iter().map(Into::into).collect(),
            UpdateShadingDto::UpdateAddDate(vec) => vec.into_iter().map(Into::into).collect(),
            UpdateShadingDto::UpdateRemoveDate(vec) => vec.into_iter().map(Into::into).collect(),
            UpdateShadingDto::UpdateNotes(vec) => vec.into_iter().map(Into::into).collect(),
        }
    }
}

impl From<UpdateValuesShadingDto> for UpdateShading {
    fn from(dto: UpdateValuesShadingDto) -> Self {
        Self {
            id: dto.id,
            shade: dto.shade,
            geometry: dto.geometry,
            ..Default::default()
        }
    }
}

impl From<UpdateAddDateShadingDto> for UpdateShading {
    fn from(dto: UpdateAddDateShadingDto) -> Self {
        Self {
            id: dto.id,
            add_date: Some(dto.add_date),
            ..Default::default()
        }
    }
}

impl From<UpdateRemoveDateShadingDto> for UpdateShading {
    fn from(dto: UpdateRemoveDateShadingDto) -> Self {
        Self {
            id: dto.id,
            remove_date: Some(dto.remove_date),
            ..Default::default()
        }
    }
}

impl From<UpdateNotesShadingDto> for UpdateShading {
    fn from(dto: UpdateNotesShadingDto) -> Self {
        Self {
            id: dto.id,
            notes: Some(dto.notes),
            ..Default::default()
        }
    }
}