use reqwest::StatusCode;
use super::drawings::{
DrawingDto, DrawingVariant, UpdateAddDateDrawingDto, UpdateDrawingsDto, UpdateNotesDrawingDto,
UpdateRemoveDateDrawingDto,
};
use crate::{
error::ServiceError,
model::{
entity::drawings::{Drawing, UpdateDrawing},
r#enum::drawing_shape_type::DrawingShapeType,
},
};
fn _internal_server_error_mismatch_json(_: serde_json::Error) -> ServiceError {
ServiceError::new(
StatusCode::INTERNAL_SERVER_ERROR,
"mismatch between database json and drawings properties",
)
}
impl TryFrom<(DrawingShapeType, serde_json::Value)> for DrawingVariant {
type Error = serde_json::Error;
fn try_from(value: (DrawingShapeType, serde_json::Value)) -> Result<Self, Self::Error> {
let (shape_type, val) = value;
match shape_type {
DrawingShapeType::Rectangle => serde_json::from_value(val).map(Self::Rectangle),
DrawingShapeType::Ellipse => serde_json::from_value(val).map(Self::Ellipse),
DrawingShapeType::FreeLine => serde_json::from_value(val).map(Self::FreeLine),
DrawingShapeType::BezierPolygon => serde_json::from_value(val).map(Self::BezierPolygon),
DrawingShapeType::LabelText => serde_json::from_value(val).map(Self::LabelText),
DrawingShapeType::Image => serde_json::from_value(val).map(Self::Image),
}
}
}
impl TryFrom<DrawingVariant> for (DrawingShapeType, serde_json::Value) {
type Error = serde_json::Error;
fn try_from(drawing_variant: DrawingVariant) -> Result<Self, Self::Error> {
Ok(match drawing_variant {
DrawingVariant::Rectangle(x) => (DrawingShapeType::Rectangle, serde_json::to_value(x)?),
DrawingVariant::Ellipse(x) => (DrawingShapeType::Ellipse, serde_json::to_value(x)?),
DrawingVariant::FreeLine(x) => (DrawingShapeType::FreeLine, serde_json::to_value(x)?),
DrawingVariant::BezierPolygon(x) => {
(DrawingShapeType::BezierPolygon, serde_json::to_value(x)?)
}
DrawingVariant::LabelText(x) => (DrawingShapeType::LabelText, serde_json::to_value(x)?),
DrawingVariant::Image(x) => (DrawingShapeType::Image, serde_json::to_value(x)?),
})
}
}
impl TryFrom<Drawing> for DrawingDto {
type Error = ServiceError;
fn try_from(drawing: Drawing) -> Result<Self, Self::Error> {
let drawing_variant = DrawingVariant::try_from((drawing.shape_type, drawing.properties))
.map_err(_internal_server_error_mismatch_json)?;
Ok(Self {
id: drawing.id,
variant: drawing_variant,
layer_id: drawing.layer_id,
add_date: drawing.add_date,
remove_date: drawing.remove_date,
rotation: drawing.rotation,
scale_x: drawing.scale_x,
scale_y: drawing.scale_y,
x: drawing.x,
y: drawing.y,
notes: drawing.notes,
})
}
}
impl TryFrom<DrawingDto> for Drawing {
type Error = ServiceError;
fn try_from(drawing_dto: DrawingDto) -> Result<Self, Self::Error> {
let (shape_type, properties) = drawing_dto
.variant
.try_into()
.map_err(_internal_server_error_mismatch_json)?;
Ok(Self {
id: drawing_dto.id,
shape_type,
layer_id: drawing_dto.layer_id,
add_date: drawing_dto.add_date,
remove_date: drawing_dto.remove_date,
rotation: drawing_dto.rotation,
scale_x: drawing_dto.scale_x,
scale_y: drawing_dto.scale_y,
x: drawing_dto.x,
y: drawing_dto.y,
properties,
notes: drawing_dto.notes,
})
}
}
impl TryFrom<DrawingDto> for UpdateDrawing {
type Error = ServiceError;
fn try_from(drawing_dto: DrawingDto) -> Result<Self, Self::Error> {
let (shape_type, properties) = drawing_dto
.variant
.try_into()
.map_err(_internal_server_error_mismatch_json)?;
Ok(Self {
id: drawing_dto.id,
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),
rotation: Some(drawing_dto.rotation),
scale_x: Some(drawing_dto.scale_x),
scale_y: Some(drawing_dto.scale_y),
x: Some(drawing_dto.x),
y: Some(drawing_dto.y),
properties: Some(properties),
notes: Some(drawing_dto.notes),
})
}
}
impl From<UpdateAddDateDrawingDto> for UpdateDrawing {
fn from(dto: UpdateAddDateDrawingDto) -> Self {
Self {
id: dto.id,
add_date: Some(dto.add_date),
..Default::default()
}
}
}
impl From<UpdateRemoveDateDrawingDto> for UpdateDrawing {
fn from(dto: UpdateRemoveDateDrawingDto) -> Self {
Self {
id: dto.id,
remove_date: Some(dto.remove_date),
..Default::default()
}
}
}
impl From<UpdateNotesDrawingDto> for UpdateDrawing {
fn from(dto: UpdateNotesDrawingDto) -> Self {
Self {
id: dto.id,
notes: Some(dto.notes),
..Default::default()
}
}
}
impl TryFrom<UpdateDrawingsDto> for Vec<UpdateDrawing> {
type Error = ServiceError;
fn try_from(dto: UpdateDrawingsDto) -> Result<Self, Self::Error> {
match dto {
UpdateDrawingsDto::Update(vec) => vec.into_iter().map(DrawingDto::try_into).collect(),
UpdateDrawingsDto::UpdateAddDate(vec) => Ok(vec.into_iter().map(Into::into).collect()),
UpdateDrawingsDto::UpdateRemoveDate(vec) => {
Ok(vec.into_iter().map(Into::into).collect())
}
UpdateDrawingsDto::UpdateNotes(vec) => Ok(vec.into_iter().map(Into::into).collect()),
}
}
}