recipes/api/src/repo/ingredient.rs

135 lines
4.0 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::ingredient::{fetch_by_key, fetch_list, types};
use crate::domain::misc_types;
#[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 lang: Option<misc_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: "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,
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<_>>()
}
}