#![allow(clippy::missing_docs_in_private_items)]
#![allow(clippy::missing_const_for_fn)]
use chrono::NaiveDate;
use postgis_diesel::types::{Point, Polygon};
use serde::Serialize;
use typeshare::typeshare;
use uuid::Uuid;
use crate::model::dto::{
drawings::DrawingDto,
layers::{LayerDto, LayerRenameDto},
plantings::{
DeletePlantingDto, MovePlantingDto, PlantingDto, TransformPlantingDto,
UpdateAddDatePlantingDto, UpdatePlantingNoteDto, UpdateRemoveDatePlantingDto,
},
BaseLayerImageDto, UpdateMapGeometryDto,
};
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Action {
pub action_id: Uuid,
pub user_id: Uuid,
pub action: ActionType,
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(tag = "type", content = "payload")]
pub enum ActionType {
CreateLayer(LayerDto),
DeleteLayer(Uuid),
RestoreDrawingLayer(RestoreDrawingLayerActionPayload),
RenameLayer(LayerRenameDto),
ReorderLayers(Vec<Uuid>),
CreatePlanting(Vec<PlantingDto>),
DeletePlanting(Vec<DeletePlantActionPayload>),
MovePlanting(Vec<MovePlantActionPayload>),
TransformPlanting(Vec<TransformPlantActionPayload>),
UpdatePlantingAddDate(Vec<UpdateAddDateActionPayload>),
UpdatePlantingRemoveDate(Vec<UpdateRemoveDateActionPayload>),
UpdatePlatingNotes(Vec<UpdatePlantingNoteActionPayload>),
CreateBaseLayerImage(CreateBaseLayerImageActionPayload),
UpdateBaseLayerImage(UpdateBaseLayerImageActionPayload),
DeleteBaseLayerImage(DeleteBaseLayerImageActionPayload),
UpdateMapGeometry(UpdateMapGeometryActionPayload),
UpdatePlantingAdditionalName(UpdatePlantingAdditionalNamePayload),
CreateDrawing(Vec<DrawingDto>),
DeleteDrawing(Vec<Uuid>),
UpdateDrawing(Vec<DrawingDto>),
UpdateDrawingAddDate(Vec<DrawingDto>),
UpdateDrawingRemoveDate(Vec<DrawingDto>),
UpdateDrawingNotes(Vec<DrawingDto>),
}
impl Action {
#[must_use]
pub fn new_create_planting_action(
dtos: Vec<PlantingDto>,
user_id: Uuid,
action_id: Uuid,
) -> Self {
Self {
action_id,
user_id,
action: ActionType::CreatePlanting(dtos),
}
}
#[must_use]
pub fn new_delete_planting_action(
dtos: &[DeletePlantingDto],
user_id: Uuid,
action_id: Uuid,
) -> Self {
Self {
action_id,
user_id,
action: ActionType::DeletePlanting(
dtos.iter()
.map(|dto| DeletePlantActionPayload { id: dto.id })
.collect(),
),
}
}
#[must_use]
pub fn new_move_planting_action(
dtos: &[MovePlantingDto],
user_id: Uuid,
action_id: Uuid,
) -> Self {
Self {
action_id,
user_id,
action: ActionType::MovePlanting(
dtos.iter()
.map(|dto| MovePlantActionPayload {
id: dto.id,
x: dto.x,
y: dto.y,
})
.collect(),
),
}
}
#[must_use]
pub fn new_transform_planting_action(
dtos: &[TransformPlantingDto],
user_id: Uuid,
action_id: Uuid,
) -> Self {
Self {
action_id,
user_id,
action: ActionType::TransformPlanting(
dtos.iter()
.map(|dto| TransformPlantActionPayload {
id: dto.id,
x: dto.x,
y: dto.y,
rotation: dto.rotation,
size_x: dto.size_x,
size_y: dto.size_y,
})
.collect(),
),
}
}
#[must_use]
pub fn new_update_planting_add_date_action(
dtos: &[UpdateAddDatePlantingDto],
user_id: Uuid,
action_id: Uuid,
) -> Self {
Self {
action_id,
user_id,
action: ActionType::UpdatePlantingAddDate(
dtos.iter()
.map(|dto| UpdateAddDateActionPayload {
id: dto.id,
add_date: dto.add_date,
})
.collect(),
),
}
}
#[must_use]
pub fn new_update_planting_remove_date_action(
dtos: &[UpdateRemoveDatePlantingDto],
user_id: Uuid,
action_id: Uuid,
) -> Self {
Self {
action_id,
user_id,
action: ActionType::UpdatePlantingRemoveDate(
dtos.iter()
.map(|dto| UpdateRemoveDateActionPayload {
id: dto.id,
remove_date: dto.remove_date,
})
.collect(),
),
}
}
#[must_use]
pub fn new_update_planting_note_action(
dtos: &[UpdatePlantingNoteDto],
user_id: Uuid,
action_id: Uuid,
) -> Self {
Self {
action_id,
user_id,
action: ActionType::UpdatePlatingNotes(
dtos.iter()
.map(|dto| UpdatePlantingNoteActionPayload {
id: dto.id,
notes: dto.notes.clone(),
})
.collect(),
),
}
}
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct RestoreDrawingLayerActionPayload {
id: Uuid,
drawings: Vec<DrawingDto>,
}
impl RestoreDrawingLayerActionPayload {
#[must_use]
pub fn new(id: Uuid, drawings: Vec<DrawingDto>) -> Self {
Self { id, drawings }
}
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CreatePlantActionPayload {
id: Uuid,
layer_id: Uuid,
plant_id: i32,
x: i32,
y: i32,
rotation: f32,
size_x: i32,
size_y: i32,
add_date: Option<NaiveDate>,
remove_date: Option<NaiveDate>,
seed_id: Option<i32>,
additional_name: Option<String>,
is_area: bool,
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DeletePlantActionPayload {
id: Uuid,
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct MovePlantActionPayload {
id: Uuid,
x: i32,
y: i32,
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct TransformPlantActionPayload {
id: Uuid,
x: i32,
y: i32,
rotation: f32,
size_x: i32,
size_y: i32,
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UpdatePlantingNoteActionPayload {
id: Uuid,
notes: String,
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CreateBaseLayerImageActionPayload {
id: Uuid,
layer_id: Uuid,
rotation: f32,
scale: f32,
path: String,
}
impl CreateBaseLayerImageActionPayload {
#[must_use]
pub fn new(payload: BaseLayerImageDto) -> Self {
Self {
id: payload.id,
layer_id: payload.layer_id,
rotation: payload.rotation,
scale: payload.scale,
path: payload.path,
}
}
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct DeleteBaseLayerImageActionPayload {
id: Uuid,
}
impl DeleteBaseLayerImageActionPayload {
#[must_use]
pub fn new(id: Uuid) -> Self {
Self { id }
}
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UpdateBaseLayerImageActionPayload {
id: Uuid,
layer_id: Uuid,
rotation: f32,
scale: f32,
path: String,
}
impl UpdateBaseLayerImageActionPayload {
#[must_use]
pub fn new(payload: BaseLayerImageDto) -> Self {
Self {
id: payload.id,
layer_id: payload.layer_id,
rotation: payload.rotation,
scale: payload.scale,
path: payload.path,
}
}
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UpdateAddDateActionPayload {
id: Uuid,
add_date: Option<NaiveDate>,
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UpdateRemoveDateActionPayload {
id: Uuid,
remove_date: Option<NaiveDate>,
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UpdateMapGeometryActionPayload {
map_id: i32,
#[typeshare(serialized_as = "object")]
geometry: Polygon<Point>,
}
impl UpdateMapGeometryActionPayload {
#[must_use]
pub fn new(payload: UpdateMapGeometryDto, map_id: i32) -> Self {
Self {
map_id,
geometry: payload.geometry,
}
}
}
#[typeshare]
#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct UpdatePlantingAdditionalNamePayload {
id: Uuid,
additional_name: Option<String>,
}
impl UpdatePlantingAdditionalNamePayload {
#[must_use]
pub fn new(payload: &PlantingDto, new_additional_name: Option<String>) -> Self {
Self {
id: payload.id,
additional_name: new_additional_name,
}
}
}