From e8a27729f66c1a76bab78d0f4318cae07ec42c49 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sat, 14 May 2022 00:45:31 +0300 Subject: [PATCH] api: fetch recipes list --- api/src/domain/ingredient/types.rs | 2 +- api/src/domain/recipe/fetch_list.rs | 7 ++++ api/src/domain/recipe/mod.rs | 1 + api/src/domain/recipe/types.rs | 57 +++++++++++++++++++++++++++++ api/src/repo/mod.rs | 1 + api/src/repo/recipe.rs | 13 +++++++ api/src/rest/ctrl/mod.rs | 1 + api/src/rest/ctrl/recipe.rs | 19 ++++++++++ api/src/rest/server.rs | 4 ++ db/build.rs | 20 +++++----- db/src/data.rs | 20 +++++----- 11 files changed, 124 insertions(+), 21 deletions(-) create mode 100644 api/src/domain/recipe/fetch_list.rs create mode 100644 api/src/repo/recipe.rs create mode 100644 api/src/rest/ctrl/recipe.rs diff --git a/api/src/domain/ingredient/types.rs b/api/src/domain/ingredient/types.rs index ce5b39b..e3e6103 100644 --- a/api/src/domain/ingredient/types.rs +++ b/api/src/domain/ingredient/types.rs @@ -9,7 +9,7 @@ pub struct Ingredient { impl From<&db::data::Ingredient> for Ingredient { fn from(db: &db::data::Ingredient) -> Self { - Self::try_from((db, Lang::Rus)).unwrap() + Self::from((db, Lang::Rus)) } } diff --git a/api/src/domain/recipe/fetch_list.rs b/api/src/domain/recipe/fetch_list.rs new file mode 100644 index 0000000..90f528f --- /dev/null +++ b/api/src/domain/recipe/fetch_list.rs @@ -0,0 +1,7 @@ +use crate::repo::recipe::RecipeRepo; + +use super::types; + +pub fn execute(repo: &impl RecipeRepo) -> Vec { + repo.get_recipes() +} diff --git a/api/src/domain/recipe/mod.rs b/api/src/domain/recipe/mod.rs index cd40856..1f3c4a4 100644 --- a/api/src/domain/recipe/mod.rs +++ b/api/src/domain/recipe/mod.rs @@ -1 +1,2 @@ +pub mod fetch_list; pub mod types; diff --git a/api/src/domain/recipe/types.rs b/api/src/domain/recipe/types.rs index e69de29..a63635d 100644 --- a/api/src/domain/recipe/types.rs +++ b/api/src/domain/recipe/types.rs @@ -0,0 +1,57 @@ +use crate::domain::misc_types::Lang; + +#[derive(Debug, Clone, Serialize)] +pub struct Recipe { + key: String, + lang: Lang, + name: String, + instructions: Vec, + ingredients: Vec, +} + +impl From<&db::data::Recipe> for Recipe { + fn from(db: &db::data::Recipe) -> Self { + Self::try_from((db, Lang::Rus)).unwrap() + } +} + +impl From<(&db::data::Recipe, Lang)> for Recipe { + fn from((db, lang): (&db::data::Recipe, Lang)) -> Self { + let tr = &db.translates; + let ctr = match lang { + Lang::Rus => &tr.rus, + _ => unimplemented!(), + }; + + Self { + key: db.key.to_string(), + lang: if ctr.name == tr.rus.name { + Lang::Rus + } else { + lang + }, + name: ctr.name.to_string(), + instructions: ctr.instructions.iter().copied().map(String::from).collect(), + ingredients: vec![], + } + } +} + +#[derive(Debug, Clone, Serialize)] +pub struct RecipeIngredient { + key: String, + measure: RecipeIngredientMeasure, +} + +#[derive(Debug, Clone, Serialize)] +#[serde(tag = "measure", content = "value")] +pub enum RecipeIngredientMeasure { + #[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/repo/mod.rs b/api/src/repo/mod.rs index ac67972..922aa9c 100644 --- a/api/src/repo/mod.rs +++ b/api/src/repo/mod.rs @@ -1 +1,2 @@ pub mod ingredient; +pub mod recipe; diff --git a/api/src/repo/recipe.rs b/api/src/repo/recipe.rs new file mode 100644 index 0000000..c29e75f --- /dev/null +++ b/api/src/repo/recipe.rs @@ -0,0 +1,13 @@ +use crate::domain::recipe::types; + +pub trait RecipeRepo { + fn get_recipes(&self) -> Vec; +} + +pub struct StaticRecipeRepo; + +impl RecipeRepo for StaticRecipeRepo { + fn get_recipes(&self) -> Vec { + db::RECIPES.iter().map(From::from).collect() + } +} diff --git a/api/src/rest/ctrl/mod.rs b/api/src/rest/ctrl/mod.rs index ac67972..922aa9c 100644 --- a/api/src/rest/ctrl/mod.rs +++ b/api/src/rest/ctrl/mod.rs @@ -1 +1,2 @@ pub mod ingredient; +pub mod recipe; diff --git a/api/src/rest/ctrl/recipe.rs b/api/src/rest/ctrl/recipe.rs new file mode 100644 index 0000000..b937ae9 --- /dev/null +++ b/api/src/rest/ctrl/recipe.rs @@ -0,0 +1,19 @@ +use std::io::Cursor; +use std::str::FromStr; + +use tiny_http::{Header, Response}; + +use crate::repo::recipe::StaticRecipeRepo; +use crate::rest::types::Url; + +pub fn fetch_list(url: &Url) -> Response>> { + use crate::domain::recipe::fetch_list; + let repo = StaticRecipeRepo; + + // TODO: catch notfound error + let recipe = fetch_list::execute(&repo); + let data = serde_json::to_string(&recipe).unwrap(); + + Response::from_string(data) + .with_header(Header::from_str("content-type: application/json").unwrap()) +} diff --git a/api/src/rest/server.rs b/api/src/rest/server.rs index 6ccf016..5d5dce8 100644 --- a/api/src/rest/server.rs +++ b/api/src/rest/server.rs @@ -26,6 +26,10 @@ pub fn start() { let res = rest::ctrl::ingredient::fetch_by_key(&url, key); rq.respond(res) } + ["api", "recipes"] => { + let res = rest::ctrl::recipe::fetch_list(&url); + rq.respond(res) + } _ => rq.respond(Response::from_string("Not found")), }; } diff --git a/db/build.rs b/db/build.rs index 8311c4a..3ea5946 100644 --- a/db/build.rs +++ b/db/build.rs @@ -39,16 +39,16 @@ pub struct IngredientTranslate { #[derive(Debug)] pub struct Recipe { - key: &'static str, - steps: u8, - ingredients: Vec, - translates: RecipeTranslates, + pub key: &'static str, + pub steps: u8, + pub ingredients: Vec, + pub translates: RecipeTranslates, } #[derive(Debug)] pub struct RecipeIngredient { - key: &'static str, - measure: RecipeIngredientMeasure, + pub key: &'static str, + pub measure: RecipeIngredientMeasure, } #[derive(Debug)] @@ -61,14 +61,14 @@ pub enum RecipeIngredientMeasure { #[derive(Debug)] pub struct RecipeTranslates { - rus: RecipeTranslate, - eng: Option, + pub rus: RecipeTranslate, + pub eng: Option, } #[derive(Debug)] pub struct RecipeTranslate { - name: &'static str, - instructions: Vec<&'static str>, + pub name: &'static str, + pub instructions: Vec<&'static str>, } "#; diff --git a/db/src/data.rs b/db/src/data.rs index 0d2b040..8526364 100644 --- a/db/src/data.rs +++ b/db/src/data.rs @@ -14,16 +14,16 @@ pub struct IngredientTranslate { #[derive(Debug)] pub struct Recipe { - key: &'static str, - steps: u8, - ingredients: Vec, - translates: RecipeTranslates, + pub key: &'static str, + pub steps: u8, + pub ingredients: Vec, + pub translates: RecipeTranslates, } #[derive(Debug)] pub struct RecipeIngredient { - key: &'static str, - measure: RecipeIngredientMeasure, + pub key: &'static str, + pub measure: RecipeIngredientMeasure, } #[derive(Debug)] @@ -36,14 +36,14 @@ pub enum RecipeIngredientMeasure { #[derive(Debug)] pub struct RecipeTranslates { - rus: RecipeTranslate, - eng: Option, + pub rus: RecipeTranslate, + pub eng: Option, } #[derive(Debug)] pub struct RecipeTranslate { - name: &'static str, - instructions: Vec<&'static str>, + pub name: &'static str, + pub instructions: Vec<&'static str>, } pub const INGREDIENTS: [Ingredient; 11] = [