api: add ingredients to recipes

This commit is contained in:
Dmitriy Pleshevskiy 2022-05-14 16:12:15 +03:00
parent e8a27729f6
commit 426c2ae4c2
4 changed files with 49 additions and 17 deletions

View File

@ -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<RecipeIngredient>,
}
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<Ingredient>)> for Recipe {
fn from((db, lang, ingredients): (&db::data::Recipe, Lang, Vec<Ingredient>)) -> 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<db::data::RecipeIngredientMeasure> 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),
}
}
}

View File

@ -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<types::Recipe>;
@ -8,6 +10,14 @@ pub struct StaticRecipeRepo;
impl RecipeRepo for StaticRecipeRepo {
fn get_recipes(&self) -> Vec<types::Recipe> {
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()
}
}

View File

@ -51,7 +51,7 @@ pub struct RecipeIngredient {
pub measure: RecipeIngredientMeasure,
}
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub enum RecipeIngredientMeasure {
Gram(u32),
KiloGram(u32),

View File

@ -26,7 +26,7 @@ pub struct RecipeIngredient {
pub measure: RecipeIngredientMeasure,
}
#[derive(Debug)]
#[derive(Debug, Clone, Copy)]
pub enum RecipeIngredientMeasure {
Gram(u32),
KiloGram(u32),