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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//! All DTOs associated with [`ShadingDto`].

use chrono::NaiveDate;
use postgis_diesel::types::{Point, Polygon};
use serde::{Deserialize, Serialize};
use typeshare::typeshare;
use utoipa::{IntoParams, ToSchema};
use uuid::Uuid;

use crate::model::r#enum::shade::Shade;

/// Represents shade on a map.
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct ShadingDto {
    /// The id of the shading.
    pub id: Uuid,
    /// The layer the shadings is on.
    pub layer_id: Uuid,
    /// The type/strength of shade.
    pub shade: Shade,
    /// The position of the shade on the map.
    ///
    /// E.g. `{"rings": [[{"x": 0.0,"y": 0.0},{"x": 1000.0,"y": 0.0},{"x": 1000.0,"y": 1000.0},{"x": 0.0,"y": 1000.0},{"x": 0.0,"y": 0.0}]],"srid": 4326}`
    #[schema(value_type = Object)]
    pub geometry: Polygon<Point>,
    /// The date the shading was added to the map.
    /// If None, the shading always existed.
    pub add_date: Option<NaiveDate>,
    /// The date the shading was removed from the map.
    /// If None, the shading is still on the map.
    pub remove_date: Option<NaiveDate>,
    /// Markdown notes.
    pub notes: String,
}

/// Used to create a new shading.
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct NewShadingDto {
    /// The id of the shading.
    pub id: Option<Uuid>,
    /// The plant layer the shadings is on.
    pub layer_id: Uuid,
    /// The type/strength of shade.
    pub shade: Shade,
    /// The position of the shade on the map.
    ///
    /// E.g. `{"rings": [[{"x": 0.0,"y": 0.0},{"x": 1000.0,"y": 0.0},{"x": 1000.0,"y": 1000.0},{"x": 0.0,"y": 1000.0},{"x": 0.0,"y": 0.0}]],"srid": 4326}`
    #[schema(value_type = Object)]
    pub geometry: Polygon<Point>,
    /// The date the shading was added to the map.
    /// If None, the shading always existed.
    pub add_date: Option<NaiveDate>,
}

/// Used to differentiate between different update operations on shadings.
///
/// Ordering of enum variants is important.
/// Serde will try to deserialize starting from the top.
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(tag = "type", content = "content")]
pub enum UpdateShadingDto {
    /// Update values of shadings.
    Update(Vec<UpdateValuesShadingDto>),
    /// Change the `add_date` of shadings.
    UpdateAddDate(Vec<UpdateAddDateShadingDto>),
    /// Change the `remove_date` of shadings.
    UpdateRemoveDate(Vec<UpdateRemoveDateShadingDto>),
    /// Change the `notes` of shadings.
    UpdateNotes(Vec<UpdateNotesShadingDto>),
}

/// Used to update the values of an existing shading.
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct UpdateValuesShadingDto {
    /// The id of the shading.
    pub id: Uuid,
    /// The type/strength of shade.
    pub shade: Option<Shade>,
    /// The position of the shade on the map.
    ///
    /// E.g. `{"rings": [[{"x": 0.0,"y": 0.0},{"x": 1000.0,"y": 0.0},{"x": 1000.0,"y": 1000.0},{"x": 0.0,"y": 1000.0},{"x": 0.0,"y": 0.0}]],"srid": 4326}`
    #[schema(value_type = Option<Object>)]
    pub geometry: Option<Polygon<Point>>,
}

/// Used to change the `add_date` of a shading.
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct UpdateAddDateShadingDto {
    /// The id of the shading.
    pub id: Uuid,
    /// The date the shading was added to the map.
    /// If None, the shading always existed.
    pub add_date: Option<NaiveDate>,
}

/// Used to change the `remove_date` of a shading.
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct UpdateRemoveDateShadingDto {
    /// The id of the shading.
    pub id: Uuid,
    /// The date the shading was removed from the map.
    /// If None, the shading is still on the map.
    pub remove_date: Option<NaiveDate>,
}

/// Used to change the `notes` of a shading.
#[typeshare]
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct UpdateNotesShadingDto {
    /// The id of the shading.
    pub id: Uuid,
    /// Markdown notes.
    pub notes: String,
}

/// Used to delete a shading.
/// The id of the shading is passed in the path.
#[typeshare]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema)]
#[serde(rename_all = "camelCase")]
pub struct DeleteShadingDto {
    /// The id of the shading.
    pub id: Uuid,
}

/// Query parameters for searching shadings.
#[typeshare]
#[derive(Debug, Deserialize, IntoParams)]
pub struct ShadingSearchParameters {
    /// The id of the layer the shading is placed on.
    pub layer_id: Option<Uuid>,
    /// Shadings that exist around this date are returned.
    pub relative_to_date: NaiveDate,
}