recipes/api/src/repo/ingredient.rs

79 lines
2.3 KiB
Rust
Raw Normal View History

2022-05-09 17:52:22 +03:00
use crate::domain::ingredient::types;
2022-05-10 00:56:08 +03:00
#[derive(Default)]
pub struct GetIngredientOpts {
pub lang: Option<types::Lang>,
}
2022-05-09 17:52:22 +03:00
pub trait IngredientRepo {
2022-05-10 00:56:08 +03:00
fn get_ingredients(&self, opts: GetIngredientOpts) -> Vec<types::Ingredient>;
2022-05-09 17:52:22 +03:00
}
2022-05-12 00:01:34 +03:00
pub struct StaticIngredientRepo;
2022-05-09 17:52:22 +03:00
impl IngredientRepo for StaticIngredientRepo {
2022-05-10 00:56:08 +03:00
fn get_ingredients(&self, opts: GetIngredientOpts) -> Vec<types::Ingredient> {
let langs = [opts.lang.unwrap_or_default()].repeat(db::INGREDIENTS.len());
db::INGREDIENTS
.iter()
.zip(langs)
.filter_map(|tup| TryFrom::try_from(tup).ok())
.collect::<Vec<_>>()
2022-05-09 17:52:22 +03:00
}
}
2022-05-10 00:09:47 +03:00
#[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"),
},
},
2022-05-10 00:56:08 +03:00
db::data::Ingredient {
key: "orange",
translates: db::data::IngredientTranslate {
ru: "Апельсин",
en: None,
},
},
2022-05-10 00:09:47 +03:00
db::data::Ingredient {
key: "salt",
translates: db::data::IngredientTranslate {
ru: "Соль",
en: Some("Salt"),
},
},
2022-05-10 00:56:08 +03:00
db::data::Ingredient {
key: "sugar",
translates: db::data::IngredientTranslate {
ru: "Сахар",
en: None,
},
},
2022-05-10 00:09:47 +03:00
],
}
}
}
#[cfg(test)]
impl IngredientRepo for InMemoryIngredientRepo {
2022-05-10 00:56:08 +03:00
fn get_ingredients(&self, opts: GetIngredientOpts) -> Vec<types::Ingredient> {
let langs = [opts.lang.unwrap_or_default()].repeat(self.ingredients.len());
2022-05-10 00:09:47 +03:00
self.ingredients
.iter()
.zip(langs)
2022-05-10 00:56:08 +03:00
.filter_map(|tup| TryFrom::try_from(tup).ok())
2022-05-10 00:09:47 +03:00
.collect::<Vec<_>>()
}
}