Use rest-types to export TOs to the frontend

This commit is contained in:
Simon Goller 2024-06-06 08:29:56 +02:00
parent 91559224e5
commit 764faa6e6b
9 changed files with 257 additions and 217 deletions

11
Cargo.lock generated
View file

@ -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"

View file

@ -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"

21
rest-types/Cargo.toml Normal file
View file

@ -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"]

216
rest-types/src/lib.rs Normal file
View file

@ -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<PrimitiveDateTime>,
#[serde(default)]
pub deleted: Option<PrimitiveDateTime>,
#[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<str>,
#[serde(default)]
pub inactive: bool,
#[serde(default)]
pub deleted: Option<time::PrimitiveDateTime>,
#[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<service::slot::DayOfWeek> 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<DayOfWeekTO> 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<time::Date>,
#[serde(default)]
pub deleted: Option<time::PrimitiveDateTime>,
#[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,
}
}
}

View file

@ -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"]

View file

@ -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<PrimitiveDateTime>,
#[serde(default)]
pub deleted: Option<PrimitiveDateTime>,
#[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<RestState: RestStateDef>() -> Router<RestState> {
Router::new()
.route("/", get(get_all_bookings::<RestState>))

View file

@ -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<RestState: RestStateDef>() -> Router<RestState> {
Router::new()
.route("/user/", get(get_all_users::<RestState>))

View file

@ -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<str>,
#[serde(default)]
pub inactive: bool,
#[serde(default)]
pub deleted: Option<time::PrimitiveDateTime>,
#[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<RestState: RestStateDef>() -> Router<RestState> {
Router::new()
.route("/", get(get_all_sales_persons::<RestState>))

View file

@ -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<service::slot::DayOfWeek> 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<DayOfWeek> 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<time::Date>,
#[serde(default)]
pub deleted: Option<time::PrimitiveDateTime>,
#[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<RestState: RestStateDef>() -> Router<RestState> {
Router::new()
.route("/", get(get_all_slots::<RestState>))