backend/model/dto/
areas.rs

1//! All DTOs associated with [`AreaDto`].
2// Areas represent shade, hydrology or soil texture.
3
4use chrono::NaiveDate;
5use postgis_diesel::types::{Point, Polygon};
6use serde::{Deserialize, Serialize};
7use typeshare::typeshare;
8use utoipa::{IntoParams, ToSchema};
9use uuid::Uuid;
10
11use crate::model::r#enum::{
12    shade::Shade, soil_texture::SoilTextureEnum, water_requirement::WaterRequirementEnum,
13};
14
15#[derive(Debug, Clone, Serialize, Deserialize, ToSchema, Eq, PartialEq)]
16#[serde(rename_all = "camelCase")]
17#[typeshare]
18#[serde(tag = "areaKind", content = "area")]
19pub enum AreaType {
20    Shade(Shade),
21    Hydrology(WaterRequirementEnum),
22    SoilTexture(SoilTextureEnum),
23}
24
25#[typeshare]
26#[derive(Debug, Clone, Copy, Serialize, Deserialize, ToSchema, Eq, PartialEq)]
27#[serde(rename_all = "camelCase")]
28pub enum AreaKind {
29    Shade,
30    Hydrology,
31    SoilTexture,
32}
33
34/// Represents area on a map. Areas are for "brushing layers".
35#[typeshare]
36#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
37#[serde(rename_all = "camelCase")]
38pub struct AreaDto {
39    /// The id of the area.
40    pub id: Uuid,
41    /// The layer the area is on.
42    pub layer_id: Uuid,
43    /// The type/strength of area.
44    pub area_type: AreaType,
45    /// The position of the area on the map.
46    ///
47    /// 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}`
48    #[schema(value_type = Object)]
49    pub geometry: Polygon<Point>,
50    /// The date the area was added to the map.
51    /// If None, the area has always existed.
52    pub add_date: Option<NaiveDate>,
53    /// The date the area  was removed from the map.
54    /// If None, the area is still on the map.
55    pub remove_date: Option<NaiveDate>,
56    /// Markdown notes.
57    pub notes: String,
58}
59
60/// Used to create a new area.
61#[typeshare]
62#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
63#[serde(rename_all = "camelCase")]
64pub struct NewAreaDto {
65    /// The id of the area.
66    pub id: Uuid,
67    /// The layer the area is on.
68    pub layer_id: Uuid,
69    /// The type/strength of area.
70    pub area_type: AreaType,
71    /// The position of the area on the map.
72    ///
73    /// 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}`
74    #[schema(value_type = Object)]
75    pub geometry: Polygon<Point>,
76    /// The date the area was added to the map.
77    /// If None, the area has always existed.
78    pub add_date: Option<NaiveDate>,
79}
80
81#[typeshare]
82#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
83#[serde(tag = "type", content = "content")]
84pub enum AreaUpdate {
85    /// Update values of areas according to their type e.g for shades light => permanent.
86    UpdateValue(Vec<UpdateAreaValueDto>),
87    /// Change the `add_date` of an area.
88    UpdateAddDate(Vec<UpdateAddDateAreaDto>),
89    /// Change the `remove_date` of an area.
90    UpdateRemoveDate(Vec<UpdateRemoveDateAreaDto>),
91    /// Change the `notes` of area.
92    UpdateNotes(Vec<UpdateNotesAreaDto>),
93}
94
95/// Used to differentiate between different update operations on areas.
96/// One patch of updates happens on the same area kind.
97///
98/// Ordering of enum variants is important.
99/// Serde will try to deserialize starting from the top.
100#[typeshare]
101#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
102#[serde(rename_all = "camelCase")]
103pub struct UpdateAreaDto {
104    pub area_kind: AreaKind,
105    pub update: AreaUpdate,
106}
107
108/// Used to update the values of an existing area.
109#[typeshare]
110#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
111#[serde(rename_all = "camelCase")]
112pub struct UpdateAreaValueDto {
113    /// The id of the area.
114    pub id: Uuid,
115    /// The new `area_type`
116    pub area_type: AreaType,
117}
118
119/// Used to change the `add_date` of an area.
120#[typeshare]
121#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
122#[serde(rename_all = "camelCase")]
123pub struct UpdateAddDateAreaDto {
124    /// The id of the area.
125    pub id: Uuid,
126    /// The date the area was added to the map.
127    /// If None, the area has always existed.
128    pub add_date: Option<NaiveDate>,
129}
130
131/// Used to change the `remove_date` of an area.
132#[typeshare]
133#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
134#[serde(rename_all = "camelCase")]
135pub struct UpdateRemoveDateAreaDto {
136    /// The id of the shading.
137    pub id: Uuid,
138    /// The date the shading was removed from the map.
139    /// If None, the shading is still on the map.
140    pub remove_date: Option<NaiveDate>,
141}
142
143/// Used to change the `notes` of an area.
144#[typeshare]
145#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
146#[serde(rename_all = "camelCase")]
147pub struct UpdateNotesAreaDto {
148    /// The id of the area.
149    pub id: Uuid,
150    /// Markdown notes.
151    pub notes: String,
152}
153
154/// Query parameters for searching areas.
155#[typeshare]
156#[derive(Debug, Deserialize, IntoParams)]
157pub struct AreaSearchParameters {
158    /// The id of the layer the area is on.
159    pub layer_id: Option<Uuid>,
160    /// Areas that exist around this date are returned.
161    pub relative_to_date: NaiveDate,
162    /// The type of area.
163    pub kind: AreaKind,
164}