api: fetch recipes list
This commit is contained in:
parent
40c99ea705
commit
e8a27729f6
|
@ -9,7 +9,7 @@ pub struct Ingredient {
|
|||
|
||||
impl From<&db::data::Ingredient> for Ingredient {
|
||||
fn from(db: &db::data::Ingredient) -> Self {
|
||||
Self::try_from((db, Lang::Rus)).unwrap()
|
||||
Self::from((db, Lang::Rus))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
use crate::repo::recipe::RecipeRepo;
|
||||
|
||||
use super::types;
|
||||
|
||||
pub fn execute(repo: &impl RecipeRepo) -> Vec<types::Recipe> {
|
||||
repo.get_recipes()
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
pub mod fetch_list;
|
||||
pub mod types;
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
use crate::domain::misc_types::Lang;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct Recipe {
|
||||
key: String,
|
||||
lang: Lang,
|
||||
name: String,
|
||||
instructions: Vec<String>,
|
||||
ingredients: Vec<RecipeIngredient>,
|
||||
}
|
||||
|
||||
impl From<&db::data::Recipe> for Recipe {
|
||||
fn from(db: &db::data::Recipe) -> Self {
|
||||
Self::try_from((db, Lang::Rus)).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl From<(&db::data::Recipe, Lang)> for Recipe {
|
||||
fn from((db, lang): (&db::data::Recipe, Lang)) -> Self {
|
||||
let tr = &db.translates;
|
||||
let ctr = match lang {
|
||||
Lang::Rus => &tr.rus,
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
|
||||
Self {
|
||||
key: db.key.to_string(),
|
||||
lang: if ctr.name == tr.rus.name {
|
||||
Lang::Rus
|
||||
} else {
|
||||
lang
|
||||
},
|
||||
name: ctr.name.to_string(),
|
||||
instructions: ctr.instructions.iter().copied().map(String::from).collect(),
|
||||
ingredients: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
pub struct RecipeIngredient {
|
||||
key: String,
|
||||
measure: RecipeIngredientMeasure,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[serde(tag = "measure", content = "value")]
|
||||
pub enum RecipeIngredientMeasure {
|
||||
#[serde(rename = "g")]
|
||||
Gram(u32),
|
||||
#[serde(rename = "kg")]
|
||||
KiloGram(u32),
|
||||
#[serde(rename = "ml")]
|
||||
MilliLiter(u32),
|
||||
#[serde(rename = "l")]
|
||||
Liter(u32),
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
pub mod ingredient;
|
||||
pub mod recipe;
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
use crate::domain::recipe::types;
|
||||
|
||||
pub trait RecipeRepo {
|
||||
fn get_recipes(&self) -> Vec<types::Recipe>;
|
||||
}
|
||||
|
||||
pub struct StaticRecipeRepo;
|
||||
|
||||
impl RecipeRepo for StaticRecipeRepo {
|
||||
fn get_recipes(&self) -> Vec<types::Recipe> {
|
||||
db::RECIPES.iter().map(From::from).collect()
|
||||
}
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
pub mod ingredient;
|
||||
pub mod recipe;
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
use std::io::Cursor;
|
||||
use std::str::FromStr;
|
||||
|
||||
use tiny_http::{Header, Response};
|
||||
|
||||
use crate::repo::recipe::StaticRecipeRepo;
|
||||
use crate::rest::types::Url;
|
||||
|
||||
pub fn fetch_list(url: &Url) -> Response<Cursor<Vec<u8>>> {
|
||||
use crate::domain::recipe::fetch_list;
|
||||
let repo = StaticRecipeRepo;
|
||||
|
||||
// TODO: catch notfound error
|
||||
let recipe = fetch_list::execute(&repo);
|
||||
let data = serde_json::to_string(&recipe).unwrap();
|
||||
|
||||
Response::from_string(data)
|
||||
.with_header(Header::from_str("content-type: application/json").unwrap())
|
||||
}
|
|
@ -26,6 +26,10 @@ pub fn start() {
|
|||
let res = rest::ctrl::ingredient::fetch_by_key(&url, key);
|
||||
rq.respond(res)
|
||||
}
|
||||
["api", "recipes"] => {
|
||||
let res = rest::ctrl::recipe::fetch_list(&url);
|
||||
rq.respond(res)
|
||||
}
|
||||
_ => rq.respond(Response::from_string("Not found")),
|
||||
};
|
||||
}
|
||||
|
|
20
db/build.rs
20
db/build.rs
|
@ -39,16 +39,16 @@ pub struct IngredientTranslate {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Recipe {
|
||||
key: &'static str,
|
||||
steps: u8,
|
||||
ingredients: Vec<RecipeIngredient>,
|
||||
translates: RecipeTranslates,
|
||||
pub key: &'static str,
|
||||
pub steps: u8,
|
||||
pub ingredients: Vec<RecipeIngredient>,
|
||||
pub translates: RecipeTranslates,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RecipeIngredient {
|
||||
key: &'static str,
|
||||
measure: RecipeIngredientMeasure,
|
||||
pub key: &'static str,
|
||||
pub measure: RecipeIngredientMeasure,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -61,14 +61,14 @@ pub enum RecipeIngredientMeasure {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct RecipeTranslates {
|
||||
rus: RecipeTranslate,
|
||||
eng: Option<RecipeTranslate>,
|
||||
pub rus: RecipeTranslate,
|
||||
pub eng: Option<RecipeTranslate>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RecipeTranslate {
|
||||
name: &'static str,
|
||||
instructions: Vec<&'static str>,
|
||||
pub name: &'static str,
|
||||
pub instructions: Vec<&'static str>,
|
||||
}
|
||||
"#;
|
||||
|
||||
|
|
|
@ -14,16 +14,16 @@ pub struct IngredientTranslate {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct Recipe {
|
||||
key: &'static str,
|
||||
steps: u8,
|
||||
ingredients: Vec<RecipeIngredient>,
|
||||
translates: RecipeTranslates,
|
||||
pub key: &'static str,
|
||||
pub steps: u8,
|
||||
pub ingredients: Vec<RecipeIngredient>,
|
||||
pub translates: RecipeTranslates,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RecipeIngredient {
|
||||
key: &'static str,
|
||||
measure: RecipeIngredientMeasure,
|
||||
pub key: &'static str,
|
||||
pub measure: RecipeIngredientMeasure,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
@ -36,14 +36,14 @@ pub enum RecipeIngredientMeasure {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub struct RecipeTranslates {
|
||||
rus: RecipeTranslate,
|
||||
eng: Option<RecipeTranslate>,
|
||||
pub rus: RecipeTranslate,
|
||||
pub eng: Option<RecipeTranslate>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct RecipeTranslate {
|
||||
name: &'static str,
|
||||
instructions: Vec<&'static str>,
|
||||
pub name: &'static str,
|
||||
pub instructions: Vec<&'static str>,
|
||||
}
|
||||
|
||||
pub const INGREDIENTS: [Ingredient; 11] = [
|
||||
|
|
Loading…
Reference in New Issue