114 lines
3.5 KiB
Rust
114 lines
3.5 KiB
Rust
use crate::domain::{misc_types::Lang, recipe::types};
|
||
use crate::repo;
|
||
|
||
use super::ingredient::IngredientRepo;
|
||
|
||
use db::data as Db;
|
||
use Db::RecipeIngredientMeasure as DbRIM;
|
||
|
||
pub trait RecipeRepo {
|
||
fn get_recipes(&self) -> Vec<types::Recipe>;
|
||
}
|
||
|
||
pub struct StaticRecipeRepo;
|
||
|
||
impl RecipeRepo for StaticRecipeRepo {
|
||
fn get_recipes(&self) -> Vec<types::Recipe> {
|
||
let ings_repo = repo::ingredient::StaticIngredientRepo;
|
||
let ings = ings_repo.get_ingredients(Default::default());
|
||
|
||
let langs = [Lang::default()].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) -> Vec<types::Recipe> {
|
||
let ings_repo = repo::ingredient::InMemoryIngredientRepo::new();
|
||
let ings = ings_repo.get_ingredients(Default::default());
|
||
|
||
let langs = [Lang::default()].repeat(self.recipes.len());
|
||
self.recipes
|
||
.iter()
|
||
.zip(langs)
|
||
.filter_map(|(rec, lang)| types::Recipe::try_from((rec, lang, ings.clone())).ok())
|
||
.collect()
|
||
}
|
||
}
|