recipes/api/src/repo/recipe.rs

116 lines
3.6 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use crate::domain::{recipe::types, Context};
use crate::repo;
use super::ingredient::IngredientRepo;
#[cfg(test)]
use db::data as Db;
#[cfg(test)]
use db::data::RecipeIngredientMeasure as DbRIM;
pub trait RecipeRepo {
fn get_recipes(&self, ctx: &Context) -> Vec<types::Recipe>;
}
pub struct StaticRecipeRepo;
impl RecipeRepo for StaticRecipeRepo {
fn get_recipes(&self, ctx: &Context) -> Vec<types::Recipe> {
let ings_repo = repo::ingredient::StaticIngredientRepo;
let ings = ings_repo.get_ingredients(ctx, Default::default());
let langs = [ctx.lang].repeat(db::RECIPES.len());
db::RECIPES
.iter()
.zip(langs)
.filter_map(|(rec, lang)| types::Recipe::try_from((rec, lang, ings.clone())).ok())
.collect()
}
}
#[cfg(test)]
pub struct InMemoryRecipeRepo {
pub recipes: Vec<db::data::Recipe>,
}
#[cfg(test)]
impl InMemoryRecipeRepo {
pub fn new() -> Self {
Self {
recipes: vec![Db::Recipe {
key: "fruit_salad",
steps: 0,
ingredients: vec![
Db::RecipeIngredient {
key: "banana",
measure: DbRIM::Gram(150),
},
Db::RecipeIngredient {
key: "apple",
measure: DbRIM::Gram(150),
},
Db::RecipeIngredient {
key: "orange",
measure: DbRIM::Gram(150),
},
],
translates: Db::RecipeTranslates {
rus: Db::RecipeTranslate {
name: "Фруктовый салат",
instructions: vec![
"Нарезать бананы кружочками",
"Нарезать яблоки и апельсины кубиками",
"Все ингредиенты перемешать",
],
},
eng: None,
},
}],
}
}
pub fn with_no_ingredients_found(mut self) -> Self {
self.recipes.push(Db::Recipe {
key: "no_ingredients_found",
steps: 0,
ingredients: vec![
Db::RecipeIngredient {
key: "salt",
measure: DbRIM::Gram(5),
},
Db::RecipeIngredient {
key: "sugar",
measure: DbRIM::Gram(4),
},
Db::RecipeIngredient {
key: "wheat_flour",
measure: DbRIM::Gram(500),
},
],
translates: Db::RecipeTranslates {
rus: Db::RecipeTranslate {
name: "Не найдены ингредиенты",
instructions: vec![],
},
eng: None,
},
});
self
}
}
#[cfg(test)]
impl RecipeRepo for InMemoryRecipeRepo {
fn get_recipes(&self, ctx: &Context) -> Vec<types::Recipe> {
let ings_repo = repo::ingredient::InMemoryIngredientRepo::new();
let ings = ings_repo.get_ingredients(ctx, Default::default());
let langs = [ctx.lang].repeat(self.recipes.len());
self.recipes
.iter()
.zip(langs)
.filter_map(|(rec, lang)| types::Recipe::try_from((rec, lang, ings.clone())).ok())
.collect()
}
}