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

Commit

Permalink
Add text+images to drawings backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
horenso authored and danielsteinkogler committed Apr 11, 2024
1 parent 68b6d00 commit bef4ece
Show file tree
Hide file tree
Showing 12 changed files with 227 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
ALTER TYPE drawing_shape_type DROP VALUE 'label text';
ALTER TYPE drawing_shape_type DROP VALUE 'image';

ALTER TABLE drawings
ADD COLUMN color varchar NOT NULL,
ADD COLUMN fill_pattern boolean NOT NULL,
ADD COLUMN stroke_width real NOT NULL;
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
ALTER TYPE drawing_shape_type ADD VALUE 'label text';
ALTER TYPE drawing_shape_type ADD VALUE 'image';

ALTER TABLE drawings
DROP COLUMN color,
DROP COLUMN fill_pattern,
DROP COLUMN stroke_width;
36 changes: 34 additions & 2 deletions backend/src/config/api_doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use utoipa_swagger_ui::SwaggerUi;
use super::auth::Config;
use crate::{
controller::{
base_layer_image, blossoms, config, guided_tours, layers, map, plant_layer, plantings,
plants, seed, timeline, users,
base_layer_image, blossoms, config, drawings, guided_tours, layers, map, plant_layer,
plantings, plants, seed, timeline, users,
},
model::{
dto::{
Expand All @@ -20,6 +20,11 @@ use crate::{
ActionDtoWrapperNewPlantings, ActionDtoWrapperUpdatePlantings,
TimelinePagePlantingsDto,
},
drawings::{
DrawingDto, EllipseProperties, FreeLineProperties, ImageProperties,
LabelTextProperties, PolygonProperties, RectangleProperties,
UpdateAddDateDrawingDto, UpdateRemoveDateDrawingDto,
},
plantings::{
MovePlantingDto, NewPlantingDto, PlantingDto, TransformPlantingDto,
UpdateAddDatePlantingDto, UpdatePlantingDto, UpdatePlantingNoteDto,
Expand Down Expand Up @@ -256,6 +261,32 @@ struct BlossomsApiDoc;
)]
struct TimelineApiDoc;

/// Struct used by [`utoipa`] to generate `OpenApi` documentation for drawings endpoints.
#[derive(OpenApi)]
#[openapi(
paths(
drawings::find,
drawings::create,
drawings::update,
drawings::delete,
),
components(
schemas(
DrawingDto,
RectangleProperties,
EllipseProperties,
FreeLineProperties,
PolygonProperties,
LabelTextProperties,
ImageProperties,
UpdateAddDateDrawingDto,
UpdateRemoveDateDrawingDto
)
),
modifiers(&SecurityAddon)
)]
struct DrawingsApiDoc;

/// Merges `OpenApi` and then serves it using `Swagger`.
pub fn config(cfg: &mut web::ServiceConfig) {
let mut openapi = ConfigApiDoc::openapi();
Expand All @@ -268,6 +299,7 @@ pub fn config(cfg: &mut web::ServiceConfig) {
openapi.merge(PlantingsApiDoc::openapi());
openapi.merge(UsersApiDoc::openapi());
openapi.merge(TimelineApiDoc::openapi());
openapi.merge(DrawingsApiDoc::openapi());

cfg.service(SwaggerUi::new("/doc/api/swagger/ui/{_:.*}").url("/doc/api/openapi.json", openapi));
}
Expand Down
87 changes: 82 additions & 5 deletions backend/src/model/dto/drawings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use typeshare::typeshare;
use utoipa::ToSchema;
use uuid::Uuid;

use crate::model::r#enum::drawing_shape_type::DrawingShapeType;

/// Represents user drawing.
#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DrawingDto {
pub id: Uuid,
pub shape_type: DrawingShapeType,

pub variant: DrawingVariant,

pub layer_id: i32,
pub add_date: Option<NaiveDate>,
pub remove_date: Option<NaiveDate>,
Expand All @@ -21,10 +21,87 @@ pub struct DrawingDto {
pub scale_y: f32,
pub x: i32,
pub y: i32,
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct RectangleProperties {
pub width: f32,
pub height: f32,
pub color: String,
pub fill_pattern: FillPatternType,
pub stroke_width: f32,
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EllipseProperties {
pub radius_x: f32,
pub radius_y: f32,
pub color: String,
pub fill_pattern: FillPatternType,
pub stroke_width: f32,
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct FreeLineProperties {
pub points: Vec<Vec<f32>>,
pub color: String,
pub fill_pattern: FillPatternType,
pub stroke_width: f32,
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PolygonProperties {
pub points: Vec<Vec<f32>>,
pub color: String,
pub fill_pattern: String,
pub fill_pattern: FillPatternType,
pub stroke_width: f32,
pub properties: serde_json::Value,
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LabelTextProperties {
pub text: String,
pub width: i32,
pub height: i32,
pub color: String,
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ImageProperties {
pub path: String,
}

/// Represents user drawing.
#[typeshare]
#[derive(Debug, Clone, Deserialize, ToSchema, Serialize)]
#[serde(tag = "type", content = "properties")]
pub enum DrawingVariant {
Rectangle(RectangleProperties),
Ellipse(EllipseProperties),
FreeLine(FreeLineProperties),
BezierPolygon(PolygonProperties),
LabelText(LabelTextProperties),
Image(ImageProperties),
}

#[typeshare]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum FillPatternType {
#[serde(rename = "fill")]
Fill,
#[serde(rename = "none")]
None,
}

/// Used to change the `add_date` of a drawing.
Expand Down
79 changes: 62 additions & 17 deletions backend/src/model/dto/drawings_impl.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,66 @@
use super::drawings::{
DrawingDto, UpdateAddDateDrawingDto, UpdateDrawingsDto, UpdateRemoveDateDrawingDto,
DrawingDto, DrawingVariant, UpdateAddDateDrawingDto, UpdateDrawingsDto,
UpdateRemoveDateDrawingDto,
};
use crate::model::entity::drawings::{Drawing, UpdateDrawing};
use crate::model::{
entity::drawings::{Drawing, UpdateDrawing},
r#enum::drawing_shape_type::DrawingShapeType,
};

impl DrawingVariant {
pub fn to_enum_and_json(&self) -> (DrawingShapeType, serde_json::Value) {
match self {
DrawingVariant::Rectangle(x) => (
DrawingShapeType::Rectangle,
serde_json::to_value(&x).unwrap(),
),
DrawingVariant::Ellipse(x) => {
(DrawingShapeType::Ellipse, serde_json::to_value(&x).unwrap())
}
DrawingVariant::FreeLine(x) => (
DrawingShapeType::FreeLine,
serde_json::to_value(&x).unwrap(),
),
DrawingVariant::BezierPolygon(x) => (
DrawingShapeType::BezierPolygon,
serde_json::to_value(&x).unwrap(),
),
DrawingVariant::LabelText(x) => (
DrawingShapeType::LabelText,
serde_json::to_value(&x).unwrap(),
),
DrawingVariant::Image(x) => {
(DrawingShapeType::Image, serde_json::to_value(&x).unwrap())
}
}
}
}

impl From<Drawing> for DrawingDto {
fn from(drawing: Drawing) -> Self {
let drawing_variant = match drawing.shape_type {
DrawingShapeType::Rectangle => {
DrawingVariant::Rectangle(serde_json::from_value(drawing.properties).unwrap())
}
DrawingShapeType::Ellipse => {
DrawingVariant::Ellipse(serde_json::from_value(drawing.properties).unwrap())
}
DrawingShapeType::FreeLine => {
DrawingVariant::FreeLine(serde_json::from_value(drawing.properties).unwrap())
}
DrawingShapeType::BezierPolygon => {
DrawingVariant::BezierPolygon(serde_json::from_value(drawing.properties).unwrap())
}
DrawingShapeType::LabelText => {
DrawingVariant::LabelText(serde_json::from_value(drawing.properties).unwrap())
}
DrawingShapeType::Image => {
DrawingVariant::Image(serde_json::from_value(drawing.properties).unwrap())
}
};
Self {
id: drawing.id,
shape_type: drawing.shape_type,
variant: drawing_variant,
layer_id: drawing.layer_id,
add_date: drawing.add_date,
remove_date: drawing.remove_date,
Expand All @@ -16,19 +69,16 @@ impl From<Drawing> for DrawingDto {
scale_y: drawing.scale_y,
x: drawing.x,
y: drawing.y,
color: drawing.color,
fill_pattern: drawing.fill_pattern,
stroke_width: drawing.stroke_width,
properties: drawing.properties,
}
}
}

impl From<DrawingDto> for Drawing {
fn from(drawing_dto: DrawingDto) -> Self {
let (shape_type, properties) = drawing_dto.variant.to_enum_and_json();
Self {
id: drawing_dto.id,
shape_type: drawing_dto.shape_type,
shape_type,
layer_id: drawing_dto.layer_id,
add_date: drawing_dto.add_date,
remove_date: drawing_dto.remove_date,
Expand All @@ -37,19 +87,17 @@ impl From<DrawingDto> for Drawing {
scale_y: drawing_dto.scale_y,
x: drawing_dto.x,
y: drawing_dto.y,
color: drawing_dto.color,
fill_pattern: drawing_dto.fill_pattern,
stroke_width: drawing_dto.stroke_width,
properties: drawing_dto.properties,
properties,
}
}
}

impl From<DrawingDto> for UpdateDrawing {
fn from(drawing_dto: DrawingDto) -> Self {
let (shape_type, properties) = drawing_dto.variant.to_enum_and_json();
Self {
id: drawing_dto.id,
shape_type: Some(drawing_dto.shape_type),
shape_type: Some(shape_type),
layer_id: Some(drawing_dto.layer_id),
add_date: Some(drawing_dto.add_date),
remove_date: Some(drawing_dto.remove_date),
Expand All @@ -58,10 +106,7 @@ impl From<DrawingDto> for UpdateDrawing {
scale_y: Some(drawing_dto.scale_y),
x: Some(drawing_dto.x),
y: Some(drawing_dto.y),
color: Some(drawing_dto.color),
fill_pattern: Some(drawing_dto.fill_pattern),
stroke_width: Some(drawing_dto.stroke_width),
properties: Some(drawing_dto.properties),
properties: Some(properties),
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions backend/src/model/entity/drawings.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use chrono::NaiveDate;
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
use serde_json;
use uuid::Uuid;

use crate::model::r#enum::drawing_shape_type::DrawingShapeType;
Expand All @@ -18,10 +19,7 @@ pub struct Drawing {
pub scale_y: f32,
pub x: i32,
pub y: i32,
pub color: String,
pub stroke_width: f32,
pub properties: serde_json::Value,
pub fill_pattern: String,
}

#[derive(Debug, Clone, Default, AsChangeset)]
Expand All @@ -37,8 +35,5 @@ pub struct UpdateDrawing {
pub scale_y: Option<f32>,
pub x: Option<i32>,
pub y: Option<i32>,
pub color: Option<String>,
pub stroke_width: Option<f32>,
pub properties: Option<serde_json::Value>,
pub fill_pattern: Option<String>,
}
2 changes: 1 addition & 1 deletion backend/src/model/enum/drawing_shape_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub enum DrawingShapeType {
#[db_rename = "bezier polygon"]
BezierPolygon,
#[db_rename = "label text"]
Text,
LabelText,
#[db_rename = "image"]
Image,
}
Loading

0 comments on commit bef4ece

Please sign in to comment.