backend/model/dto/
plantings_impl.rs1use uuid::Uuid;
5
6use crate::model::{
7 dto::plantings::{
8 MovePlantingDto, PlantingDto, TransformPlantingDto, UpdateAddDatePlantingDto,
9 UpdatePlantingDto, UpdatePlantingNoteDto, UpdateRemoveDatePlantingDto,
10 },
11 entity::plantings::{NewPlanting, Planting, UpdatePlanting},
12};
13
14impl From<(Planting, Option<String>)> for PlantingDto {
15 fn from((planting, additional_name): (Planting, Option<String>)) -> Self {
16 Self {
17 additional_name,
18 ..planting.into()
19 }
20 }
21}
22
23impl From<Planting> for PlantingDto {
24 fn from(planting: Planting) -> Self {
25 Self {
26 id: planting.id,
27 plant_id: planting.plant_id.try_into().unwrap_or(-1),
28 layer_id: planting.layer_id,
29 created_at: Some(planting.created_at),
30 modified_at: Some(planting.modified_at),
31 created_by: Some(planting.created_by),
32 modified_by: Some(planting.modified_by),
33 x: planting.x,
34 y: planting.y,
35 size_x: planting.size_x,
36 size_y: planting.size_y,
37 height: planting.height,
38 rotation: planting.rotation,
39 add_date: planting.add_date,
40 remove_date: planting.remove_date,
41 seed_id: planting.seed_id.and_then(|v| i32::try_from(v).ok()),
42 is_area: planting.is_area,
43 additional_name: None,
44 notes: planting.notes,
45 }
46 }
47}
48
49impl From<(PlantingDto, Uuid)> for NewPlanting {
50 fn from((dto, user_id): (PlantingDto, Uuid)) -> Self {
51 Self {
52 id: dto.id,
53 plant_id: dto.plant_id.into(),
54 layer_id: dto.layer_id,
55 created_by: user_id,
56 modified_by: user_id,
57 x: dto.x,
58 y: dto.y,
59 size_x: dto.size_x,
60 size_y: dto.size_y,
61 height: dto.height,
62 rotation: dto.rotation,
63 remove_date: Option::None,
64 add_date: dto.add_date,
65 seed_id: dto.seed_id.map(Into::into),
66 is_area: dto.is_area,
67 notes: dto.notes,
68 }
69 }
70}
71
72impl From<TransformPlantingDto> for UpdatePlanting {
73 fn from(dto: TransformPlantingDto) -> Self {
74 Self {
75 id: dto.id,
76 x: Some(dto.x),
77 y: Some(dto.y),
78 rotation: Some(dto.rotation),
79 size_x: Some(dto.size_x),
80 size_y: Some(dto.size_y),
81 height: Some(dto.height),
82 ..Default::default()
83 }
84 }
85}
86
87impl From<MovePlantingDto> for UpdatePlanting {
88 fn from(dto: MovePlantingDto) -> Self {
89 Self {
90 id: dto.id,
91 x: Some(dto.x),
92 y: Some(dto.y),
93 ..Default::default()
94 }
95 }
96}
97
98impl From<UpdateAddDatePlantingDto> for UpdatePlanting {
99 fn from(dto: UpdateAddDatePlantingDto) -> Self {
100 Self {
101 id: dto.id,
102 add_date: Some(dto.add_date),
103 ..Default::default()
104 }
105 }
106}
107
108impl From<UpdateRemoveDatePlantingDto> for UpdatePlanting {
109 fn from(dto: UpdateRemoveDatePlantingDto) -> Self {
110 Self {
111 id: dto.id,
112 remove_date: Some(dto.remove_date),
113 ..Default::default()
114 }
115 }
116}
117
118impl From<UpdatePlantingNoteDto> for UpdatePlanting {
119 fn from(dto: UpdatePlantingNoteDto) -> Self {
120 Self {
121 id: dto.id,
122 notes: Some(dto.notes),
123 ..Default::default()
124 }
125 }
126}
127
128impl From<UpdatePlantingDto> for Vec<UpdatePlanting> {
129 fn from(dto: UpdatePlantingDto) -> Self {
130 match dto {
131 UpdatePlantingDto::Transform(vec) => vec.into_iter().map(Into::into).collect(),
132 UpdatePlantingDto::Move(vec) => vec.into_iter().map(Into::into).collect(),
133 UpdatePlantingDto::UpdateAddDate(vec) => vec.into_iter().map(Into::into).collect(),
134 UpdatePlantingDto::UpdateRemoveDate(vec) => vec.into_iter().map(Into::into).collect(),
135 UpdatePlantingDto::UpdateNote(vec) => vec.into_iter().map(Into::into).collect(),
136 }
137 }
138}