Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
refactor(map-state): actions now encapsulate their associated logic
Browse files Browse the repository at this point in the history
actions now encapsulate the following logic.
how it applies itself onto the state.
how it reverts itself.
how it communicates itself to the backend.
  • Loading branch information
Bushuo committed Jun 6, 2023
1 parent 712ee0f commit f7bfd3f
Show file tree
Hide file tree
Showing 16 changed files with 3,111 additions and 5,580 deletions.
7 changes: 7 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
"program": "${workspaceFolder}/backend/target/debug/backend",
"args": [],
"cwd": "${workspaceFolder}/backend"
},
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:5173",
"webRoot": "${workspaceFolder}/frontend"
}
]
}
4 changes: 2 additions & 2 deletions backend/src/config/api_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
dto::{
ConfigDto, MapDto, MapVersionDto, NewMapDto, NewMapVersionDto, NewPlantingDto,
NewSeedDto, PageMapDto, PageMapVersionDto, PagePlantingDto, PagePlantsSummaryDto,
PageSeedDto, PlantingDto, PlantsSummaryDto, SeedDto, UpdatePlantingDto,
PageSeedDto, PlantLayerObjectDto, PlantsSummaryDto, SeedDto, UpdatePlantingDto,
},
r#enum::{quality::Quality, quantity::Quantity},
},
Expand Down Expand Up @@ -88,7 +88,7 @@ struct PlantsApiDoc;
components(
schemas(
NewPlantingDto,
PlantingDto,
PlantLayerObjectDto,
UpdatePlantingDto,
PagePlantingDto
)
Expand Down
39 changes: 23 additions & 16 deletions backend/src/controller/plantings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use uuid::Uuid;
use crate::config::auth::user_info::{self, UserInfo};
use crate::model::dto::actions::{CreatePlantAction, DeletePlantAction};
use crate::model::dto::{
NewPlantingDto, Page, PageParameters, PlantingDto, PlantingSearchParameters, UpdatePlantingDto,
NewPlantingDto, Page, PageParameters, PlantLayerObjectDto, PlantingSearchParameters,
UpdatePlantingDto,
};
use crate::AppDataInner;

Expand Down Expand Up @@ -48,12 +49,16 @@ pub async fn find(
) -> Result<HttpResponse> {
// TODO: implement service that validates (permission, integrity) and filters for records.
let page = Page {
results: vec![PlantingDto {
results: vec![PlantLayerObjectDto {
id: "uuid".to_string(),
plant_id: 1,
plants_layer_id: 1,
x: 0,
y: 0,
height: 100,
width: 100,
rotation: 0,
scale_x: 1,
scale_y: 1,
}],
page: 1,
per_page: 10,
Expand Down Expand Up @@ -85,20 +90,21 @@ pub async fn create(
user_info: UserInfo,
) -> Result<HttpResponse> {
// TODO: implement service that validates (permission, integrity) and creates the record.
let dto = PlantingDto {
id: Uuid::new_v4().to_string(),
let dto = PlantLayerObjectDto {
id: new_plant_json.id.to_string(),
plant_id: new_plant_json.plant_id,
plants_layer_id: new_plant_json.plants_layer_id,
x: new_plant_json.x,
y: new_plant_json.y,
height: new_plant_json.height,
width: new_plant_json.width,
rotation: new_plant_json.rotation,
scale_x: new_plant_json.scale_x,
scale_y: new_plant_json.scale_y,
};

app_data
.broadcaster
.broadcast(
&user_info.id.to_string(),
&CreatePlantAction::new(dto.clone()).to_string(),
)
.broadcast(&CreatePlantAction::new(dto.clone(), user_info.id.to_string()).to_string())
.await;

Ok(HttpResponse::Created().json(dto))
Expand Down Expand Up @@ -127,12 +133,16 @@ pub async fn update(
app_data: Data<AppDataInner>,
) -> Result<HttpResponse> {
// TODO: implement service that validates (permission, integrity) and updates the record.
let dto = PlantingDto {
let dto = PlantLayerObjectDto {
id: "uuid".to_string(),
plant_id: 1,
plants_layer_id: 1,
x: 0,
y: 0,
height: 100,
width: 100,
rotation: 0,
scale_x: 1,
scale_y: 1,
};
Ok(HttpResponse::Ok().json(dto))
}
Expand Down Expand Up @@ -161,10 +171,7 @@ pub async fn delete(
// TODO: implement a service that validates (permissions) and deletes the planting
app_data
.broadcaster
.broadcast(
&user_info.id.to_string(),
&DeletePlantAction::new(path.to_string()).to_string(),
)
.broadcast(&DeletePlantAction::new(path.to_string(), user_info.id.to_string()).to_string())
.await;

Ok(HttpResponse::Ok().json(""))
Expand Down
53 changes: 35 additions & 18 deletions backend/src/model/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,37 +85,56 @@ pub struct PlantsSummaryDto {
/// E.g. a user drags a plant from the search results and drops it on the map.
#[typeshare]
#[derive(Serialize, Deserialize, ToSchema, Clone)]
pub struct PlantingDto {
/// The database id of the record.
pub struct PlantLayerObjectDto {
/// The database id of the record. This is a UUID.
pub id: String,
/// The plant that is planted.
#[serde(rename = "plantId")]
pub plant_id: i32,
/// The plants layer of the map the plant is placed on.
/// NOTE:
/// could be replaced by a `map_id` as the relation between `maps` and
/// `plants_layers` should be non-nullable and one to one.
pub plants_layer_id: i32,
/// The x coordinate of the position on the map.
pub x: i32,
/// The y coordinate of the position on the map.
pub y: i32,
/// The width of the plant on the map.
pub width: i32,
/// The height of the plant on the map.
pub height: i32,
/// The rotation of the plant on the map.
pub rotation: i32,
/// The x scale of the plant on the map.
#[serde(rename = "scaleX")]
pub scale_x: i32,
/// The y scale of the plant on the map.
#[serde(rename = "scaleY")]
pub scale_y: i32,
}

/// Used to create a new planting.
#[typeshare]
#[derive(Serialize, Deserialize, ToSchema)]
pub struct NewPlantingDto {
/// The database id of the record. This is a UUID.
pub id: String,
/// The plant that is planted.
pub plant_id: i32,
/// The plants layer of the map the plant is placed on.
/// NOTE:
/// could be replaced by a `map_id` as the relation between `maps` and
/// `plants_layers` should be non-nullable and one to one.
pub plants_layer_id: i32,
/// The the map the plant is placed on.
pub map_id: i32,
/// The x coordinate of the position on the map.
pub x: i32,
/// The y coordinate of the position on the map.
pub y: i32,
/// The width of the plant on the map.
pub width: i32,
/// The height of the plant on the map.
pub height: i32,
/// The rotation of the plant on the map.
pub rotation: i32,
/// The x scale of the plant on the map.
#[serde(rename = "scaleX")]
pub scale_x: i32,
/// The y scale of the plant on the map.
#[serde(rename = "scaleY")]
pub scale_y: i32,
}

/// Used to update an existing planting.
Expand All @@ -124,11 +143,9 @@ pub struct NewPlantingDto {
pub struct UpdatePlantingDto {
/// The plant that is planted.
pub plant_id: Option<i32>,
/// The plants layer of the map the plant is placed on.
/// NOTE:
/// could be replaced by a `map_id` as the relation between `maps` and
/// `plants_layers` should be non-nullable and one to one.
pub plants_layer_id: Option<i32>,
/// The map the plant is placed on.
/// Note: This field is not updated by this endpoint.
pub map_id: Option<i32>,
/// The x coordinate of the position on the map.
pub x: Option<i32>,
/// The y coordinate of the position on the map.
Expand Down Expand Up @@ -181,7 +198,7 @@ pub struct PageParameters {
PagePlantsSummaryDto = Page<PlantsSummaryDto>,
PageSeedDto = Page<SeedDto>,
PageMapDto = Page<MapDto>,
PagePlantingDto = Page<PlantingDto>,
PagePlantingDto = Page<PlantLayerObjectDto>,
PageMapVersionDto = Page<MapVersionDto>
)]
pub struct Page<T> {
Expand Down
26 changes: 21 additions & 5 deletions backend/src/model/dto/actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
use super::PlantingDto;
use serde::{Deserialize, Serialize};
use std::fmt::Display;

use super::PlantLayerObjectDto;
use serde::{ser, Deserialize, Serialize};
use typeshare::typeshare;

#[typeshare]
#[derive(Serialize, Deserialize)]
pub struct CreatePlantAction {
#[serde(rename = "type")]
_type: String,
payload: PlantingDto,
#[serde(rename = "userId")]
user_id: String,
payload: CreatePlantActionPayload,
}

#[typeshare]
pub type CreatePlantActionPayload = PlantLayerObjectDto;

impl CreatePlantAction {
pub fn new(payload: PlantingDto) -> Self {
pub fn new(payload: PlantLayerObjectDto, user_id: String) -> Self {
Self {
_type: "CREATE_PLANT".to_string(),
user_id,
payload,
}
}
Expand All @@ -23,21 +33,27 @@ impl ToString for CreatePlantAction {
}
}

#[typeshare]
#[derive(Serialize, Deserialize)]
pub struct DeletePlantAction {
#[serde(rename = "type")]
_type: String,
#[serde(rename = "userId")]
user_id: String,
payload: DeletePlantActionPayload,
}

#[typeshare]
#[derive(Serialize, Deserialize)]
pub struct DeletePlantActionPayload {
id: String,
}

impl DeletePlantAction {
pub fn new(id: String) -> Self {
pub fn new(id: String, user_id: String) -> Self {
Self {
_type: "DELETE_PLANT".to_string(),
user_id,
payload: DeletePlantActionPayload { id },
}
}
Expand Down
12 changes: 2 additions & 10 deletions backend/src/sse/broadcaster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,8 @@ impl Broadcaster {
}

/// Broadcasts `msg` to all clients.
pub async fn broadcast(&self, from: &str, msg: &str) {
let clients = self
.inner
.lock()
.await
.clients
.clone()
.into_iter()
.filter(|client| client.id != from)
.collect::<Vec<_>>();
pub async fn broadcast(&self, msg: &str) {
let clients = self.inner.lock().await.clients.clone();

let send_futures = clients
.iter()
Expand Down
Loading

0 comments on commit f7bfd3f

Please sign in to comment.