127 lines
3.7 KiB
Rust
127 lines
3.7 KiB
Rust
use crate::domain::ingredient::{fetch_by_key, fetch_list, types};
|
||
|
||
#[derive(Default)]
|
||
pub struct GetIngredientOpts {
|
||
pub lang: Option<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 lang: Option<types::Lang>,
|
||
pub keys: Option<Vec<String>>,
|
||
}
|
||
|
||
impl From<fetch_list::RequestOpts> for GetIngredientsOpts {
|
||
fn from(app: fetch_list::RequestOpts) -> Self {
|
||
Self {
|
||
lang: app.lang,
|
||
keys: app.keys,
|
||
}
|
||
}
|
||
}
|
||
|
||
pub trait IngredientRepo {
|
||
fn get_ingredient_opt(&self, key: String, opts: GetIngredientOpts)
|
||
-> Option<types::Ingredient>;
|
||
|
||
fn get_ingredients(&self, opts: GetIngredientsOpts) -> Vec<types::Ingredient>;
|
||
}
|
||
|
||
pub struct StaticIngredientRepo;
|
||
|
||
impl IngredientRepo for StaticIngredientRepo {
|
||
fn get_ingredient_opt(
|
||
&self,
|
||
key: String,
|
||
opts: GetIngredientOpts,
|
||
) -> Option<types::Ingredient> {
|
||
db::INGREDIENTS
|
||
.iter()
|
||
.find(|ing| ing.key == &key)
|
||
.map(|ing| types::Ingredient::from((ing, opts.lang.unwrap_or_default())))
|
||
}
|
||
|
||
fn get_ingredients(&self, opts: GetIngredientsOpts) -> Vec<types::Ingredient> {
|
||
let langs = [opts.lang.unwrap_or_default()].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: "apple",
|
||
translates: db::data::IngredientTranslate {
|
||
ru: "Яблоко",
|
||
en: Some("Apple"),
|
||
},
|
||
},
|
||
db::data::Ingredient {
|
||
key: "orange",
|
||
translates: db::data::IngredientTranslate {
|
||
ru: "Апельсин",
|
||
en: None,
|
||
},
|
||
},
|
||
db::data::Ingredient {
|
||
key: "salt",
|
||
translates: db::data::IngredientTranslate {
|
||
ru: "Соль",
|
||
en: Some("Salt"),
|
||
},
|
||
},
|
||
db::data::Ingredient {
|
||
key: "sugar",
|
||
translates: db::data::IngredientTranslate {
|
||
ru: "Сахар",
|
||
en: None,
|
||
},
|
||
},
|
||
],
|
||
}
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
impl IngredientRepo for InMemoryIngredientRepo {
|
||
fn get_ingredient_opt(
|
||
&self,
|
||
key: String,
|
||
opts: GetIngredientOpts,
|
||
) -> Option<types::Ingredient> {
|
||
self.ingredients
|
||
.iter()
|
||
.find(|ing| ing.key == &key)
|
||
.map(|ing| types::Ingredient::from((ing, opts.lang.unwrap_or_default())))
|
||
}
|
||
|
||
fn get_ingredients(&self, opts: GetIngredientsOpts) -> Vec<types::Ingredient> {
|
||
let langs = [opts.lang.unwrap_or_default()].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<_>>()
|
||
}
|
||
}
|