api: fetch recipes list

This commit is contained in:
Dmitriy Pleshevskiy 2022-05-14 00:45:31 +03:00
parent 40c99ea705
commit e8a27729f6
11 changed files with 124 additions and 21 deletions

View File

@ -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))
}
}

View File

@ -0,0 +1,7 @@
use crate::repo::recipe::RecipeRepo;
use super::types;
pub fn execute(repo: &impl RecipeRepo) -> Vec<types::Recipe> {
repo.get_recipes()
}

View File

@ -1 +1,2 @@
pub mod fetch_list;
pub mod types;

View File

@ -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),
}

View File

@ -1 +1,2 @@
pub mod ingredient;
pub mod recipe;

13
api/src/repo/recipe.rs Normal file
View File

@ -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()
}
}

View File

@ -1 +1,2 @@
pub mod ingredient;
pub mod recipe;

View File

@ -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())
}

View File

@ -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")),
};
}

View File

@ -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>,
}
"#;

View File

@ -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] = [