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

//use chrono::Utc;
use uuid::Uuid;

use crate::model::{
    dto::plantings::{
        MovePlantingDto, PlantingDto, TransformPlantingDto, UpdateAddDatePlantingDto,
        UpdatePlantingDto, UpdatePlantingNoteDto, UpdateRemoveDatePlantingDto,
    },
    entity::plantings::{NewPlanting, Planting, UpdatePlanting},
};

impl From<(Planting, Option<String>)> for PlantingDto {
    fn from((planting, additional_name): (Planting, Option<String>)) -> Self {
        Self {
            additional_name,
            ..planting.into()
        }
    }
}

impl From<Planting> for PlantingDto {
    fn from(planting: Planting) -> Self {
        Self {
            id: planting.id,
            plant_id: planting.plant_id,
            layer_id: planting.layer_id,
            x: planting.x,
            y: planting.y,
            size_x: planting.size_x,
            size_y: planting.size_y,
            rotation: planting.rotation,
            add_date: planting.add_date,
            remove_date: planting.remove_date,
            seed_id: planting.seed_id,
            is_area: planting.is_area,
            additional_name: None,
            planting_notes: planting.notes,
        }
    }
}

impl From<(PlantingDto, Uuid)> for NewPlanting {
    fn from((dto, user_id): (PlantingDto, Uuid)) -> Self {
        Self {
            id: dto.id,
            plant_id: dto.plant_id,
            layer_id: dto.layer_id,
            created_by: user_id,
            modified_by: user_id,
            x: dto.x,
            y: dto.y,
            size_x: dto.size_x,
            size_y: dto.size_y,
            rotation: dto.rotation,
            remove_date: Option::None,
            add_date: dto.add_date,
            seed_id: dto.seed_id,
            is_area: dto.is_area,
        }
    }
}

impl From<TransformPlantingDto> for UpdatePlanting {
    fn from(dto: TransformPlantingDto) -> Self {
        Self {
            id: dto.id,
            x: Some(dto.x),
            y: Some(dto.y),
            rotation: Some(dto.rotation),
            size_x: Some(dto.size_x),
            size_y: Some(dto.size_y),
            ..Default::default()
        }
    }
}

impl From<MovePlantingDto> for UpdatePlanting {
    fn from(dto: MovePlantingDto) -> Self {
        Self {
            id: dto.id,
            x: Some(dto.x),
            y: Some(dto.y),
            ..Default::default()
        }
    }
}

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

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

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

impl From<UpdatePlantingDto> for Vec<UpdatePlanting> {
    fn from(dto: UpdatePlantingDto) -> Self {
        match dto {
            UpdatePlantingDto::Transform(vec) => vec.into_iter().map(Into::into).collect(),
            UpdatePlantingDto::Move(vec) => vec.into_iter().map(Into::into).collect(),
            UpdatePlantingDto::UpdateAddDate(vec) => vec.into_iter().map(Into::into).collect(),
            UpdatePlantingDto::UpdateRemoveDate(vec) => vec.into_iter().map(Into::into).collect(),
            UpdatePlantingDto::UpdateNote(vec) => vec.into_iter().map(Into::into).collect(),
        }
    }
}