diff --git a/api/src/domain/recipe/types.rs b/api/src/domain/recipe/types.rs index a63635d..05bb566 100644 --- a/api/src/domain/recipe/types.rs +++ b/api/src/domain/recipe/types.rs @@ -1,4 +1,4 @@ -use crate::domain::misc_types::Lang; +use crate::domain::{ingredient::types::Ingredient, misc_types::Lang}; #[derive(Debug, Clone, Serialize)] pub struct Recipe { @@ -9,18 +9,12 @@ pub struct Recipe { 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 { +impl From<(&db::data::Recipe, Lang, Vec)> for Recipe { + fn from((db, lang, ingredients): (&db::data::Recipe, Lang, Vec)) -> Self { let tr = &db.translates; let ctr = match lang { Lang::Rus => &tr.rus, - _ => unimplemented!(), + Lang::Eng => tr.eng.as_ref().unwrap_or(&tr.rus), }; Self { @@ -32,19 +26,33 @@ impl From<(&db::data::Recipe, Lang)> for Recipe { }, name: ctr.name.to_string(), instructions: ctr.instructions.iter().copied().map(String::from).collect(), - ingredients: vec![], + ingredients: db + .ingredients + .iter() + .filter_map(|sing| { + ingredients + .iter() + .find(|ing| sing.key == ing.key) + .map(|ing| RecipeIngredient { + ingredient: ing.clone(), + measure: sing.measure.into(), + }) + }) + .collect(), } } } #[derive(Debug, Clone, Serialize)] pub struct RecipeIngredient { - key: String, + #[serde(flatten)] + ingredient: Ingredient, + #[serde(flatten)] measure: RecipeIngredientMeasure, } #[derive(Debug, Clone, Serialize)] -#[serde(tag = "measure", content = "value")] +#[serde(tag = "measure", content = "amount")] pub enum RecipeIngredientMeasure { #[serde(rename = "g")] Gram(u32), @@ -55,3 +63,17 @@ pub enum RecipeIngredientMeasure { #[serde(rename = "l")] Liter(u32), } + +impl From for RecipeIngredientMeasure { + fn from(db: db::data::RecipeIngredientMeasure) -> Self { + use db::data::RecipeIngredientMeasure as DbRIM; + use RecipeIngredientMeasure as RIM; + + match db { + DbRIM::Gram(val) => RIM::Gram(val), + DbRIM::KiloGram(val) => RIM::KiloGram(val), + DbRIM::MilliLiter(val) => RIM::MilliLiter(val), + DbRIM::Liter(val) => RIM::Liter(val), + } + } +} diff --git a/api/src/repo/recipe.rs b/api/src/repo/recipe.rs index c29e75f..fa39842 100644 --- a/api/src/repo/recipe.rs +++ b/api/src/repo/recipe.rs @@ -1,4 +1,6 @@ -use crate::domain::recipe::types; +use crate::domain::{misc_types::Lang, recipe::types}; + +use super::ingredient::IngredientRepo; pub trait RecipeRepo { fn get_recipes(&self) -> Vec; @@ -8,6 +10,14 @@ pub struct StaticRecipeRepo; impl RecipeRepo for StaticRecipeRepo { fn get_recipes(&self) -> Vec { - db::RECIPES.iter().map(From::from).collect() + let ings_repo = crate::repo::ingredient::StaticIngredientRepo; + let ings = ings_repo.get_ingredients(Default::default()); + + let langs = [Lang::default()].repeat(db::RECIPES.len()); + db::RECIPES + .iter() + .zip(langs) + .map(|(rec, lang)| From::from((rec, lang, ings.clone()))) + .collect() } } diff --git a/db/build.rs b/db/build.rs index 3ea5946..9aff7bb 100644 --- a/db/build.rs +++ b/db/build.rs @@ -51,7 +51,7 @@ pub struct RecipeIngredient { pub measure: RecipeIngredientMeasure, } -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub enum RecipeIngredientMeasure { Gram(u32), KiloGram(u32), diff --git a/db/src/data.rs b/db/src/data.rs index 8526364..e63a8fc 100644 --- a/db/src/data.rs +++ b/db/src/data.rs @@ -26,7 +26,7 @@ pub struct RecipeIngredient { pub measure: RecipeIngredientMeasure, } -#[derive(Debug)] +#[derive(Debug, Clone, Copy)] pub enum RecipeIngredientMeasure { Gram(u32), KiloGram(u32),