122 lines
3.8 KiB
Rust
122 lines
3.8 KiB
Rust
use crate::domain::ingredient::{fetch_by_key, fetch_list, types};
|
||
use crate::domain::misc_types::{self, Context};
|
||
|
||
#[derive(Default)]
|
||
pub struct GetIngredientOpts {
|
||
pub lang: Option<misc_types::Lang>,
|
||
}
|
||
|
||
impl From<fetch_by_key::RequestOpts> for GetIngredientOpts {
|
||
fn from(app: fetch_by_key::RequestOpts) -> Self {
|
||
Self { lang: app.lang }
|
||
}
|
||
}
|
||
|
||
#[derive(Default)]
|
||
pub struct GetIngredientsOpts {
|
||
pub keys: Option<Vec<String>>,
|
||
}
|
||
|
||
impl From<fetch_list::RequestOpts> for GetIngredientsOpts {
|
||
fn from(app: fetch_list::RequestOpts) -> Self {
|
||
Self { keys: app.keys }
|
||
}
|
||
}
|
||
|
||
pub trait IngredientRepo {
|
||
fn get_ingredient_opt(&self, ctx: &Context, key: String) -> Option<types::Ingredient>;
|
||
|
||
fn get_ingredients(&self, ctx: &Context, opts: GetIngredientsOpts) -> Vec<types::Ingredient>;
|
||
}
|
||
|
||
pub struct StaticIngredientRepo;
|
||
|
||
impl IngredientRepo for StaticIngredientRepo {
|
||
fn get_ingredient_opt(&self, ctx: &Context, key: String) -> Option<types::Ingredient> {
|
||
db::INGREDIENTS
|
||
.iter()
|
||
.find(|ing| ing.key == &key)
|
||
.map(|ing| types::Ingredient::from((ing, ctx.lang)))
|
||
}
|
||
|
||
fn get_ingredients(&self, ctx: &Context, opts: GetIngredientsOpts) -> Vec<types::Ingredient> {
|
||
let langs = [ctx.lang].repeat(db::INGREDIENTS.len());
|
||
db::INGREDIENTS
|
||
.iter()
|
||
.zip(langs)
|
||
.map(types::Ingredient::from)
|
||
.filter(|ing| opts.keys.is_none() || opts.keys.as_ref().unwrap().contains(&ing.key))
|
||
.collect::<Vec<_>>()
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
pub struct InMemoryIngredientRepo {
|
||
pub ingredients: Vec<db::data::Ingredient>,
|
||
}
|
||
|
||
#[cfg(test)]
|
||
impl InMemoryIngredientRepo {
|
||
pub fn new() -> Self {
|
||
Self {
|
||
ingredients: vec![
|
||
db::data::Ingredient {
|
||
key: "banana",
|
||
translates: db::data::IngredientTranslate {
|
||
rus: "Банан",
|
||
eng: Some("Banana"),
|
||
},
|
||
},
|
||
db::data::Ingredient {
|
||
key: "apple",
|
||
translates: db::data::IngredientTranslate {
|
||
rus: "Яблоко",
|
||
eng: Some("Apple"),
|
||
},
|
||
},
|
||
db::data::Ingredient {
|
||
key: "orange",
|
||
translates: db::data::IngredientTranslate {
|
||
rus: "Апельсин",
|
||
eng: None,
|
||
},
|
||
},
|
||
db::data::Ingredient {
|
||
key: "salt",
|
||
translates: db::data::IngredientTranslate {
|
||
rus: "Соль",
|
||
eng: Some("Salt"),
|
||
},
|
||
},
|
||
db::data::Ingredient {
|
||
key: "sugar",
|
||
translates: db::data::IngredientTranslate {
|
||
rus: "Сахар",
|
||
eng: None,
|
||
},
|
||
},
|
||
],
|
||
}
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
impl IngredientRepo for InMemoryIngredientRepo {
|
||
fn get_ingredient_opt(&self, ctx: &Context, key: String) -> Option<types::Ingredient> {
|
||
self.ingredients
|
||
.iter()
|
||
.find(|ing| ing.key == &key)
|
||
.map(|ing| types::Ingredient::from((ing, ctx.lang)))
|
||
}
|
||
|
||
fn get_ingredients(&self, ctx: &Context, opts: GetIngredientsOpts) -> Vec<types::Ingredient> {
|
||
let langs = [ctx.lang].repeat(self.ingredients.len());
|
||
self.ingredients
|
||
.iter()
|
||
.zip(langs)
|
||
.map(types::Ingredient::from)
|
||
.filter(|ing| opts.keys.is_none() || opts.keys.as_ref().unwrap().contains(&ing.key))
|
||
.collect::<Vec<_>>()
|
||
}
|
||
}
|