From f75efb55cfe8fe669f7ec9dd93a7920f39e90ea3 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 27 May 2022 00:53:42 +0300 Subject: [PATCH] api: add missed rest types --- api/src/domain/ingredient/types.rs | 4 +- api/src/domain/misc_types.rs | 2 +- api/src/domain/recipe/types.rs | 15 ++--- api/src/rest/ctrl/mod.rs | 2 - .../ingredient.rs => ingredient/ctrl.rs} | 9 ++- api/src/rest/ingredient/mod.rs | 2 + api/src/rest/ingredient/types.rs | 19 ++++++ api/src/rest/misc_types.rs | 17 +++++ api/src/rest/mod.rs | 8 ++- .../rest/{ctrl/recipe.rs => recipe/ctrl.rs} | 5 +- api/src/rest/recipe/mod.rs | 2 + api/src/rest/recipe/types.rs | 67 +++++++++++++++++++ api/src/rest/server.rs | 6 +- 13 files changed, 135 insertions(+), 23 deletions(-) delete mode 100644 api/src/rest/ctrl/mod.rs rename api/src/rest/{ctrl/ingredient.rs => ingredient/ctrl.rs} (93%) create mode 100644 api/src/rest/ingredient/mod.rs create mode 100644 api/src/rest/ingredient/types.rs create mode 100644 api/src/rest/misc_types.rs rename api/src/rest/{ctrl/recipe.rs => recipe/ctrl.rs} (81%) create mode 100644 api/src/rest/recipe/mod.rs create mode 100644 api/src/rest/recipe/types.rs diff --git a/api/src/domain/ingredient/types.rs b/api/src/domain/ingredient/types.rs index b94f16e..69ae2da 100644 --- a/api/src/domain/ingredient/types.rs +++ b/api/src/domain/ingredient/types.rs @@ -1,6 +1,6 @@ -use crate::domain::misc_types::Lang; +use crate::domain::Lang; -#[derive(Debug, Clone, Serialize)] +#[derive(Debug, Clone)] pub struct Ingredient { pub key: String, pub lang: Lang, diff --git a/api/src/domain/misc_types.rs b/api/src/domain/misc_types.rs index 0fa57cb..d542f4b 100644 --- a/api/src/domain/misc_types.rs +++ b/api/src/domain/misc_types.rs @@ -12,7 +12,7 @@ impl Context { } } -#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize)] +#[derive(Debug, Copy, Clone, PartialEq, Eq)] pub enum Lang { Rus, #[allow(dead_code)] diff --git a/api/src/domain/recipe/types.rs b/api/src/domain/recipe/types.rs index 13fd71d..1a75739 100644 --- a/api/src/domain/recipe/types.rs +++ b/api/src/domain/recipe/types.rs @@ -1,6 +1,6 @@ -use crate::domain::{ingredient::types::Ingredient, misc_types::Lang}; +use crate::domain::{Ingredient, Lang}; -#[derive(Debug, Clone, Serialize)] +#[derive(Debug, Clone)] pub struct Recipe { pub key: String, pub lang: Lang, @@ -9,23 +9,16 @@ pub struct Recipe { pub ingredients: Vec, } -#[derive(Debug, Clone, Serialize)] +#[derive(Debug, Clone)] pub struct RecipeIngredient { - #[serde(flatten)] pub ingredient: Ingredient, - #[serde(flatten)] pub measure: Measure, } -#[derive(Debug, Clone, PartialEq, Eq, Serialize)] -#[serde(tag = "measure", content = "amount")] +#[derive(Debug, Clone, PartialEq, Eq)] pub enum Measure { - #[serde(rename = "g")] Gram(u32), - #[serde(rename = "kg")] KiloGram(u32), - #[serde(rename = "ml")] MilliLiter(u32), - #[serde(rename = "l")] Liter(u32), } diff --git a/api/src/rest/ctrl/mod.rs b/api/src/rest/ctrl/mod.rs deleted file mode 100644 index 922aa9c..0000000 --- a/api/src/rest/ctrl/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod ingredient; -pub mod recipe; diff --git a/api/src/rest/ctrl/ingredient.rs b/api/src/rest/ingredient/ctrl.rs similarity index 93% rename from api/src/rest/ctrl/ingredient.rs rename to api/src/rest/ingredient/ctrl.rs index abe430b..c9fcd94 100644 --- a/api/src/rest/ctrl/ingredient.rs +++ b/api/src/rest/ingredient/ctrl.rs @@ -47,7 +47,10 @@ pub fn fetch_list(rest_ctx: &rest::Context, url: &Url) -> Response>(); let data = serde_json::to_string(&ingredients).unwrap(); Response::from_string(data) @@ -61,7 +64,9 @@ pub fn fetch_by_key(rest_ctx: &rest::Context, _url: &Url, key: &str) -> Response let ctx = rest_ctx.clone().into(); // TODO: catch notfound error - let ingredient = fetch_by_key::execute(&repo, &ctx, key.to_string()).ok(); + let ingredient = fetch_by_key::execute(&repo, &ctx, key.to_string()) + .ok() + .map(rest::Ingredient::from); let data = serde_json::to_string(&ingredient).unwrap(); Response::from_string(data) diff --git a/api/src/rest/ingredient/mod.rs b/api/src/rest/ingredient/mod.rs new file mode 100644 index 0000000..dd78892 --- /dev/null +++ b/api/src/rest/ingredient/mod.rs @@ -0,0 +1,2 @@ +pub mod ctrl; +pub mod types; diff --git a/api/src/rest/ingredient/types.rs b/api/src/rest/ingredient/types.rs new file mode 100644 index 0000000..e4735a1 --- /dev/null +++ b/api/src/rest/ingredient/types.rs @@ -0,0 +1,19 @@ +use crate::domain; +use crate::rest::Lang; + +#[derive(Debug, Serialize)] +pub struct Ingredient { + pub key: String, + pub lang: Lang, + pub name: String, +} + +impl From for Ingredient { + fn from(ingr: domain::Ingredient) -> Self { + Self { + key: ingr.key, + lang: ingr.lang.into(), + name: ingr.name, + } + } +} diff --git a/api/src/rest/misc_types.rs b/api/src/rest/misc_types.rs new file mode 100644 index 0000000..061a4bd --- /dev/null +++ b/api/src/rest/misc_types.rs @@ -0,0 +1,17 @@ +use crate::domain; + +#[derive(Debug, Serialize, Deserialize)] +pub enum Lang { + Rus, + #[allow(dead_code)] + Eng, +} + +impl From for Lang { + fn from(app: domain::Lang) -> Self { + match app { + domain::Lang::Rus => Lang::Rus, + domain::Lang::Eng => Lang::Eng, + } + } +} diff --git a/api/src/rest/mod.rs b/api/src/rest/mod.rs index c0c2281..5f47121 100644 --- a/api/src/rest/mod.rs +++ b/api/src/rest/mod.rs @@ -1,6 +1,12 @@ pub mod context; -pub mod ctrl; +pub mod ingredient; +pub mod misc_types; +pub mod recipe; pub mod server; pub mod types; pub use context::Context; + +pub use ingredient::types::Ingredient; +pub use misc_types::Lang; +pub use recipe::types::{Measure, Recipe, RecipeIngredient}; diff --git a/api/src/rest/ctrl/recipe.rs b/api/src/rest/recipe/ctrl.rs similarity index 81% rename from api/src/rest/ctrl/recipe.rs rename to api/src/rest/recipe/ctrl.rs index d97bfd2..22e3779 100644 --- a/api/src/rest/ctrl/recipe.rs +++ b/api/src/rest/recipe/ctrl.rs @@ -14,7 +14,10 @@ pub fn fetch_list(rest_ctx: &rest::Context, _url: &Url) -> Response>(); let data = serde_json::to_string(&recipe).unwrap(); Response::from_string(data) diff --git a/api/src/rest/recipe/mod.rs b/api/src/rest/recipe/mod.rs new file mode 100644 index 0000000..dd78892 --- /dev/null +++ b/api/src/rest/recipe/mod.rs @@ -0,0 +1,2 @@ +pub mod ctrl; +pub mod types; diff --git a/api/src/rest/recipe/types.rs b/api/src/rest/recipe/types.rs new file mode 100644 index 0000000..175c2de --- /dev/null +++ b/api/src/rest/recipe/types.rs @@ -0,0 +1,67 @@ +use crate::{ + domain, + rest::{Ingredient, Lang}, +}; + +#[derive(Debug, Serialize)] +pub struct Recipe { + pub key: String, + pub lang: Lang, + pub name: String, + pub instructions: Vec, + pub ingredients: Vec, +} + +impl From for Recipe { + fn from(app: domain::Recipe) -> Self { + Self { + key: app.key, + lang: app.lang.into(), + name: app.name, + ingredients: app.ingredients.into_iter().map(From::from).collect(), + instructions: app.instructions, + } + } +} + +#[derive(Debug, Serialize)] +pub struct RecipeIngredient { + #[serde(flatten)] + pub ingredient: Ingredient, + #[serde(flatten)] + pub measure: Measure, +} + +impl From for RecipeIngredient { + fn from(app: domain::RecipeIngredient) -> Self { + Self { + ingredient: app.ingredient.into(), + measure: app.measure.into(), + } + } +} + +#[derive(Debug, Serialize)] +#[serde(tag = "measure", content = "amount")] +pub enum Measure { + #[serde(rename = "g")] + Gram(u32), + #[serde(rename = "kg")] + KiloGram(u32), + #[serde(rename = "ml")] + MilliLiter(u32), + #[serde(rename = "l")] + Liter(u32), +} + +impl From for Measure { + fn from(app: domain::Measure) -> Self { + use domain::Measure as M; + match app { + M::Gram(v) => Measure::Gram(v), + M::KiloGram(v) => Measure::KiloGram(v), + M::MilliLiter(v) => Measure::MilliLiter(v), + M::Liter(v) => Measure::Liter(v), + } + } +} diff --git a/api/src/rest/server.rs b/api/src/rest/server.rs index 83cbc41..5971907 100644 --- a/api/src/rest/server.rs +++ b/api/src/rest/server.rs @@ -20,15 +20,15 @@ pub fn start() { let ctx = rest::Context::from(url.query_params()); let _ = match url.path_segments() { ["api", "ingredients"] => { - let res = rest::ctrl::ingredient::fetch_list(&ctx, &url); + let res = rest::ingredient::ctrl::fetch_list(&ctx, &url); rq.respond(res) } ["api", "ingredients", key] => { - let res = rest::ctrl::ingredient::fetch_by_key(&ctx, &url, key); + let res = rest::ingredient::ctrl::fetch_by_key(&ctx, &url, key); rq.respond(res) } ["api", "recipes"] => { - let res = rest::ctrl::recipe::fetch_list(&ctx, &url); + let res = rest::recipe::ctrl::fetch_list(&ctx, &url); rq.respond(res) } _ => rq.respond(Response::from_string("Not found")),