From 764faa6e6bdba376043a0bc57e252fe6c083c531 Mon Sep 17 00:00:00 2001 From: Simon Goller Date: Thu, 6 Jun 2024 08:29:56 +0200 Subject: [PATCH] Use rest-types to export TOs to the frontend --- Cargo.lock | 11 ++ Cargo.toml | 2 +- rest-types/Cargo.toml | 21 ++++ rest-types/src/lib.rs | 216 +++++++++++++++++++++++++++++++++++++++ rest/Cargo.toml | 3 + rest/src/booking.rs | 48 +-------- rest/src/permission.rs | 51 +-------- rest/src/sales_person.rs | 39 +------ rest/src/slot.rs | 83 +-------------- 9 files changed, 257 insertions(+), 217 deletions(-) create mode 100644 rest-types/Cargo.toml create mode 100644 rest-types/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 181365d..01ed12b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1740,6 +1740,7 @@ dependencies = [ "axum-oidc", "bytes", "http-body 1.0.0", + "rest-types", "serde", "serde_json", "service", @@ -1751,6 +1752,16 @@ dependencies = [ "uuid", ] +[[package]] +name = "rest-types" +version = "0.1.0" +dependencies = [ + "serde", + "service", + "time", + "uuid", +] + [[package]] name = "rfc6979" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 77069c4..eb79366 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,3 @@ [workspace] -members = ["rest", "service", "service_impl", "app", "dao", "dao_impl"] +members = ["rest", "service", "service_impl", "app", "dao", "dao_impl", "rest-types"] resolver = "2" diff --git a/rest-types/Cargo.toml b/rest-types/Cargo.toml new file mode 100644 index 0000000..4a713e6 --- /dev/null +++ b/rest-types/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "rest-types" +version = "0.1.0" +edition = "2021" +resolver = "2" + + +[dependencies.service] +path = "../service" + +[dependencies.serde] +version = "1.0.198" +features = ["derive", "std", "alloc", "rc"] + +[dependencies.uuid] +version = "1.8.0" +features = ["v4", "serde"] + +[dependencies.time] +version = "0.3.36" +features = ["serde-human-readable"] diff --git a/rest-types/src/lib.rs b/rest-types/src/lib.rs new file mode 100644 index 0000000..9234e03 --- /dev/null +++ b/rest-types/src/lib.rs @@ -0,0 +1,216 @@ +use std::sync::Arc; + +use serde::{Deserialize, Serialize}; +use service::{booking::Booking, sales_person::SalesPerson}; +use time::PrimitiveDateTime; +use uuid::Uuid; + +#[derive(Debug, Serialize, Deserialize)] +pub struct UserTO { + pub name: String, +} +impl From<&service::User> for UserTO { + fn from(user: &service::User) -> Self { + Self { + name: user.name.to_string(), + } + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct RoleTO { + pub name: String, +} +impl From<&service::Role> for RoleTO { + fn from(role: &service::Role) -> Self { + Self { + name: role.name.to_string(), + } + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct PrivilegeTO { + pub name: String, +} +impl From<&service::Privilege> for PrivilegeTO { + fn from(privilege: &service::Privilege) -> Self { + Self { + name: privilege.name.to_string(), + } + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct UserRole { + pub user: String, + pub role: String, +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct RolePrivilege { + pub role: String, + pub privilege: String, +} + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct BookingTO { + #[serde(default)] + pub id: Uuid, + pub sales_person_id: Uuid, + pub slot_id: Uuid, + pub calendar_week: i32, + pub year: u32, + #[serde(default)] + pub created: Option, + #[serde(default)] + pub deleted: Option, + #[serde(rename = "$version")] + #[serde(default)] + pub version: Uuid, +} +impl From<&Booking> for BookingTO { + fn from(booking: &Booking) -> Self { + Self { + id: booking.id, + sales_person_id: booking.sales_person_id, + slot_id: booking.slot_id, + calendar_week: booking.calendar_week, + year: booking.year, + created: booking.created, + deleted: booking.deleted, + version: booking.version, + } + } +} +impl From<&BookingTO> for Booking { + fn from(booking: &BookingTO) -> Self { + Self { + id: booking.id, + sales_person_id: booking.sales_person_id, + slot_id: booking.slot_id, + calendar_week: booking.calendar_week, + year: booking.year, + created: booking.created, + deleted: booking.deleted, + version: booking.version, + } + } +} + +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct SalesPersonTO { + #[serde(default)] + pub id: Uuid, + pub name: Arc, + #[serde(default)] + pub inactive: bool, + #[serde(default)] + pub deleted: Option, + #[serde(rename = "$version")] + #[serde(default)] + pub version: Uuid, +} +impl From<&SalesPerson> for SalesPersonTO { + fn from(sales_person: &SalesPerson) -> Self { + Self { + id: sales_person.id, + name: sales_person.name.clone(), + inactive: sales_person.inactive, + deleted: sales_person.deleted, + version: sales_person.version, + } + } +} +impl From<&SalesPersonTO> for SalesPerson { + fn from(sales_person: &SalesPersonTO) -> Self { + Self { + id: sales_person.id, + name: sales_person.name.clone(), + inactive: sales_person.inactive, + deleted: sales_person.deleted, + version: sales_person.version, + } + } +} + +#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] +pub enum DayOfWeekTO { + Monday, + Tuesday, + Wednesday, + Thursday, + Friday, + Saturday, + Sunday, +} +impl From for DayOfWeekTO { + fn from(day_of_week: service::slot::DayOfWeek) -> Self { + match day_of_week { + service::slot::DayOfWeek::Monday => Self::Monday, + service::slot::DayOfWeek::Tuesday => Self::Tuesday, + service::slot::DayOfWeek::Wednesday => Self::Wednesday, + service::slot::DayOfWeek::Thursday => Self::Thursday, + service::slot::DayOfWeek::Friday => Self::Friday, + service::slot::DayOfWeek::Saturday => Self::Saturday, + service::slot::DayOfWeek::Sunday => Self::Sunday, + } + } +} +impl From for service::slot::DayOfWeek { + fn from(day_of_week: DayOfWeekTO) -> Self { + match day_of_week { + DayOfWeekTO::Monday => Self::Monday, + DayOfWeekTO::Tuesday => Self::Tuesday, + DayOfWeekTO::Wednesday => Self::Wednesday, + DayOfWeekTO::Thursday => Self::Thursday, + DayOfWeekTO::Friday => Self::Friday, + DayOfWeekTO::Saturday => Self::Saturday, + DayOfWeekTO::Sunday => Self::Sunday, + } + } +} + +#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct SlotTO { + #[serde(default)] + pub id: Uuid, + pub day_of_week: DayOfWeekTO, + pub from: time::Time, + pub to: time::Time, + pub valid_from: time::Date, + pub valid_to: Option, + #[serde(default)] + pub deleted: Option, + #[serde(rename = "$version")] + #[serde(default)] + pub version: Uuid, +} +impl From<&service::slot::Slot> for SlotTO { + fn from(slot: &service::slot::Slot) -> Self { + Self { + id: slot.id, + day_of_week: slot.day_of_week.into(), + from: slot.from, + to: slot.to, + valid_from: slot.valid_from, + valid_to: slot.valid_to, + deleted: slot.deleted, + version: slot.version, + } + } +} +impl From<&SlotTO> for service::slot::Slot { + fn from(slot: &SlotTO) -> Self { + Self { + id: slot.id, + day_of_week: slot.day_of_week.into(), + from: slot.from, + to: slot.to, + valid_from: slot.valid_from, + valid_to: slot.valid_to, + deleted: slot.deleted, + version: slot.version, + } + } +} diff --git a/rest/Cargo.toml b/rest/Cargo.toml index 99234f0..07a86f0 100644 --- a/rest/Cargo.toml +++ b/rest/Cargo.toml @@ -26,6 +26,9 @@ features = ["full"] [dependencies.service] path = "../service" +[dependencies.rest-types] +path = "../rest-types" + [dependencies.uuid] version = "1.8.0" features = ["v4", "serde"] diff --git a/rest/src/booking.rs b/rest/src/booking.rs index eeff622..c70ecb1 100644 --- a/rest/src/booking.rs +++ b/rest/src/booking.rs @@ -5,58 +5,12 @@ use axum::extract::Path; use axum::routing::{delete, get, post}; use axum::{extract::State, response::Response}; use axum::{Extension, Json, Router}; -use serde::{Deserialize, Serialize}; -use time::PrimitiveDateTime; +use rest_types::BookingTO; use uuid::Uuid; use crate::{error_handler, Context, RestStateDef}; use service::booking::{Booking, BookingService}; -#[derive(Serialize, Deserialize, Clone, Debug)] -pub struct BookingTO { - #[serde(default)] - pub id: Uuid, - pub sales_person_id: Uuid, - pub slot_id: Uuid, - pub calendar_week: i32, - pub year: u32, - #[serde(default)] - pub created: Option, - #[serde(default)] - pub deleted: Option, - #[serde(rename = "$version")] - #[serde(default)] - pub version: Uuid, -} -impl From<&Booking> for BookingTO { - fn from(booking: &Booking) -> Self { - Self { - id: booking.id, - sales_person_id: booking.sales_person_id, - slot_id: booking.slot_id, - calendar_week: booking.calendar_week, - year: booking.year, - created: booking.created, - deleted: booking.deleted, - version: booking.version, - } - } -} -impl From<&BookingTO> for Booking { - fn from(booking: &BookingTO) -> Self { - Self { - id: booking.id, - sales_person_id: booking.sales_person_id, - slot_id: booking.slot_id, - calendar_week: booking.calendar_week, - year: booking.year, - created: booking.created, - deleted: booking.deleted, - version: booking.version, - } - } -} - pub fn generate_route() -> Router { Router::new() .route("/", get(get_all_bookings::)) diff --git a/rest/src/permission.rs b/rest/src/permission.rs index 6a73f78..0444868 100644 --- a/rest/src/permission.rs +++ b/rest/src/permission.rs @@ -1,6 +1,7 @@ -use serde::{Deserialize, Serialize}; use std::sync::Arc; +use rest_types::*; + use axum::{ body::Body, extract::State, @@ -12,54 +13,6 @@ use axum::{ use crate::{error_handler, Context, RestStateDef}; use service::PermissionService; -#[derive(Debug, Serialize, Deserialize)] -pub struct UserTO { - pub name: String, -} -impl From<&service::User> for UserTO { - fn from(user: &service::User) -> Self { - Self { - name: user.name.to_string(), - } - } -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct RoleTO { - pub name: String, -} -impl From<&service::Role> for RoleTO { - fn from(role: &service::Role) -> Self { - Self { - name: role.name.to_string(), - } - } -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct PrivilegeTO { - pub name: String, -} -impl From<&service::Privilege> for PrivilegeTO { - fn from(privilege: &service::Privilege) -> Self { - Self { - name: privilege.name.to_string(), - } - } -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct UserRole { - pub user: String, - pub role: String, -} - -#[derive(Debug, Serialize, Deserialize)] -pub struct RolePrivilege { - pub role: String, - pub privilege: String, -} - pub fn generate_route() -> Router { Router::new() .route("/user/", get(get_all_users::)) diff --git a/rest/src/sales_person.rs b/rest/src/sales_person.rs index 512bc7a..f291300 100644 --- a/rest/src/sales_person.rs +++ b/rest/src/sales_person.rs @@ -5,49 +5,12 @@ use axum::extract::Path; use axum::routing::{delete, get, post, put}; use axum::{extract::State, response::Response}; use axum::{Extension, Json, Router}; -use serde::{Deserialize, Serialize}; -use service::sales_person::SalesPerson; +use rest_types::SalesPersonTO; use service::sales_person::SalesPersonService; use uuid::Uuid; use crate::{error_handler, Context, RestError, RestStateDef}; -#[derive(Serialize, Deserialize, Clone, Debug)] -pub struct SalesPersonTO { - #[serde(default)] - pub id: Uuid, - pub name: Arc, - #[serde(default)] - pub inactive: bool, - #[serde(default)] - pub deleted: Option, - #[serde(rename = "$version")] - #[serde(default)] - pub version: Uuid, -} -impl From<&SalesPerson> for SalesPersonTO { - fn from(sales_person: &SalesPerson) -> Self { - Self { - id: sales_person.id, - name: sales_person.name.clone(), - inactive: sales_person.inactive, - deleted: sales_person.deleted, - version: sales_person.version, - } - } -} -impl From<&SalesPersonTO> for SalesPerson { - fn from(sales_person: &SalesPersonTO) -> Self { - Self { - id: sales_person.id, - name: sales_person.name.clone(), - inactive: sales_person.inactive, - deleted: sales_person.deleted, - version: sales_person.version, - } - } -} - pub fn generate_route() -> Router { Router::new() .route("/", get(get_all_sales_persons::)) diff --git a/rest/src/slot.rs b/rest/src/slot.rs index 2601ca6..6108c6b 100644 --- a/rest/src/slot.rs +++ b/rest/src/slot.rs @@ -7,93 +7,12 @@ use axum::{ routing::{get, post, put}, Extension, Json, Router, }; -use serde::{Deserialize, Serialize}; +use rest_types::SlotTO; use service::slot::SlotService; use uuid::Uuid; use crate::{error_handler, Context, RestError, RestStateDef}; -#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize)] -pub enum DayOfWeek { - Monday, - Tuesday, - Wednesday, - Thursday, - Friday, - Saturday, - Sunday, -} -impl From for DayOfWeek { - fn from(day_of_week: service::slot::DayOfWeek) -> Self { - match day_of_week { - service::slot::DayOfWeek::Monday => Self::Monday, - service::slot::DayOfWeek::Tuesday => Self::Tuesday, - service::slot::DayOfWeek::Wednesday => Self::Wednesday, - service::slot::DayOfWeek::Thursday => Self::Thursday, - service::slot::DayOfWeek::Friday => Self::Friday, - service::slot::DayOfWeek::Saturday => Self::Saturday, - service::slot::DayOfWeek::Sunday => Self::Sunday, - } - } -} -impl From for service::slot::DayOfWeek { - fn from(day_of_week: DayOfWeek) -> Self { - match day_of_week { - DayOfWeek::Monday => Self::Monday, - DayOfWeek::Tuesday => Self::Tuesday, - DayOfWeek::Wednesday => Self::Wednesday, - DayOfWeek::Thursday => Self::Thursday, - DayOfWeek::Friday => Self::Friday, - DayOfWeek::Saturday => Self::Saturday, - DayOfWeek::Sunday => Self::Sunday, - } - } -} - -#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] -pub struct SlotTO { - #[serde(default)] - pub id: Uuid, - pub day_of_week: DayOfWeek, - pub from: time::Time, - pub to: time::Time, - pub valid_from: time::Date, - pub valid_to: Option, - #[serde(default)] - pub deleted: Option, - #[serde(rename = "$version")] - #[serde(default)] - pub version: Uuid, -} -impl From<&service::slot::Slot> for SlotTO { - fn from(slot: &service::slot::Slot) -> Self { - Self { - id: slot.id, - day_of_week: slot.day_of_week.into(), - from: slot.from, - to: slot.to, - valid_from: slot.valid_from, - valid_to: slot.valid_to, - deleted: slot.deleted, - version: slot.version, - } - } -} -impl From<&SlotTO> for service::slot::Slot { - fn from(slot: &SlotTO) -> Self { - Self { - id: slot.id, - day_of_week: slot.day_of_week.into(), - from: slot.from, - to: slot.to, - valid_from: slot.valid_from, - valid_to: slot.valid_to, - deleted: slot.deleted, - version: slot.version, - } - } -} - pub fn generate_route() -> Router { Router::new() .route("/", get(get_all_slots::))