api: add missed rest types
This commit is contained in:
parent
af3a228edd
commit
f75efb55cf
|
@ -1,6 +1,6 @@
|
|||
use crate::domain::misc_types::Lang;
|
||||
use crate::domain::Lang;
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Ingredient {
|
||||
pub key: String,
|
||||
pub lang: Lang,
|
||||
|
|
|
@ -12,7 +12,7 @@ impl Context {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
pub enum Lang {
|
||||
Rus,
|
||||
#[allow(dead_code)]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use crate::domain::{ingredient::types::Ingredient, misc_types::Lang};
|
||||
use crate::domain::{Ingredient, Lang};
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Recipe {
|
||||
pub key: String,
|
||||
pub lang: Lang,
|
||||
|
@ -9,23 +9,16 @@ pub struct Recipe {
|
|||
pub ingredients: Vec<RecipeIngredient>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RecipeIngredient {
|
||||
#[serde(flatten)]
|
||||
pub ingredient: Ingredient,
|
||||
#[serde(flatten)]
|
||||
pub measure: Measure,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
|
||||
#[serde(tag = "measure", content = "amount")]
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum Measure {
|
||||
#[serde(rename = "g")]
|
||||
Gram(u32),
|
||||
#[serde(rename = "kg")]
|
||||
KiloGram(u32),
|
||||
#[serde(rename = "ml")]
|
||||
MilliLiter(u32),
|
||||
#[serde(rename = "l")]
|
||||
Liter(u32),
|
||||
}
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
pub mod ingredient;
|
||||
pub mod recipe;
|
|
@ -47,7 +47,10 @@ pub fn fetch_list(rest_ctx: &rest::Context, url: &Url) -> Response<Cursor<Vec<u8
|
|||
let ctx = rest_ctx.clone().into();
|
||||
|
||||
let repo = StaticIngredientRepo;
|
||||
let ingredients = fetch_list::execute(&repo, &ctx, opts.into());
|
||||
let ingredients = fetch_list::execute(&repo, &ctx, opts.into())
|
||||
.into_iter()
|
||||
.map(rest::Ingredient::from)
|
||||
.collect::<Vec<_>>();
|
||||
let data = serde_json::to_string(&ingredients).unwrap();
|
||||
|
||||
Response::from_string(data)
|
||||
|
@ -61,7 +64,9 @@ pub fn fetch_by_key(rest_ctx: &rest::Context, _url: &Url, key: &str) -> Response
|
|||
let ctx = rest_ctx.clone().into();
|
||||
|
||||
// TODO: catch notfound error
|
||||
let ingredient = fetch_by_key::execute(&repo, &ctx, key.to_string()).ok();
|
||||
let ingredient = fetch_by_key::execute(&repo, &ctx, key.to_string())
|
||||
.ok()
|
||||
.map(rest::Ingredient::from);
|
||||
let data = serde_json::to_string(&ingredient).unwrap();
|
||||
|
||||
Response::from_string(data)
|
|
@ -0,0 +1,2 @@
|
|||
pub mod ctrl;
|
||||
pub mod types;
|
|
@ -0,0 +1,19 @@
|
|||
use crate::domain;
|
||||
use crate::rest::Lang;
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Ingredient {
|
||||
pub key: String,
|
||||
pub lang: Lang,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
impl From<domain::Ingredient> for Ingredient {
|
||||
fn from(ingr: domain::Ingredient) -> Self {
|
||||
Self {
|
||||
key: ingr.key,
|
||||
lang: ingr.lang.into(),
|
||||
name: ingr.name,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
use crate::domain;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub enum Lang {
|
||||
Rus,
|
||||
#[allow(dead_code)]
|
||||
Eng,
|
||||
}
|
||||
|
||||
impl From<domain::Lang> for Lang {
|
||||
fn from(app: domain::Lang) -> Self {
|
||||
match app {
|
||||
domain::Lang::Rus => Lang::Rus,
|
||||
domain::Lang::Eng => Lang::Eng,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,12 @@
|
|||
pub mod context;
|
||||
pub mod ctrl;
|
||||
pub mod ingredient;
|
||||
pub mod misc_types;
|
||||
pub mod recipe;
|
||||
pub mod server;
|
||||
pub mod types;
|
||||
|
||||
pub use context::Context;
|
||||
|
||||
pub use ingredient::types::Ingredient;
|
||||
pub use misc_types::Lang;
|
||||
pub use recipe::types::{Measure, Recipe, RecipeIngredient};
|
||||
|
|
|
@ -14,7 +14,10 @@ pub fn fetch_list(rest_ctx: &rest::Context, _url: &Url) -> Response<Cursor<Vec<u
|
|||
let ctx = rest_ctx.clone().into();
|
||||
|
||||
// TODO: catch notfound error
|
||||
let recipe = fetch_list::execute(&repo, &ctx);
|
||||
let recipe = fetch_list::execute(&repo, &ctx)
|
||||
.into_iter()
|
||||
.map(rest::Recipe::from)
|
||||
.collect::<Vec<_>>();
|
||||
let data = serde_json::to_string(&recipe).unwrap();
|
||||
|
||||
Response::from_string(data)
|
|
@ -0,0 +1,2 @@
|
|||
pub mod ctrl;
|
||||
pub mod types;
|
|
@ -0,0 +1,67 @@
|
|||
use crate::{
|
||||
domain,
|
||||
rest::{Ingredient, Lang},
|
||||
};
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct Recipe {
|
||||
pub key: String,
|
||||
pub lang: Lang,
|
||||
pub name: String,
|
||||
pub instructions: Vec<String>,
|
||||
pub ingredients: Vec<RecipeIngredient>,
|
||||
}
|
||||
|
||||
impl From<domain::Recipe> for Recipe {
|
||||
fn from(app: domain::Recipe) -> Self {
|
||||
Self {
|
||||
key: app.key,
|
||||
lang: app.lang.into(),
|
||||
name: app.name,
|
||||
ingredients: app.ingredients.into_iter().map(From::from).collect(),
|
||||
instructions: app.instructions,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
pub struct RecipeIngredient {
|
||||
#[serde(flatten)]
|
||||
pub ingredient: Ingredient,
|
||||
#[serde(flatten)]
|
||||
pub measure: Measure,
|
||||
}
|
||||
|
||||
impl From<domain::RecipeIngredient> for RecipeIngredient {
|
||||
fn from(app: domain::RecipeIngredient) -> Self {
|
||||
Self {
|
||||
ingredient: app.ingredient.into(),
|
||||
measure: app.measure.into(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[serde(tag = "measure", content = "amount")]
|
||||
pub enum Measure {
|
||||
#[serde(rename = "g")]
|
||||
Gram(u32),
|
||||
#[serde(rename = "kg")]
|
||||
KiloGram(u32),
|
||||
#[serde(rename = "ml")]
|
||||
MilliLiter(u32),
|
||||
#[serde(rename = "l")]
|
||||
Liter(u32),
|
||||
}
|
||||
|
||||
impl From<domain::Measure> for Measure {
|
||||
fn from(app: domain::Measure) -> Self {
|
||||
use domain::Measure as M;
|
||||
match app {
|
||||
M::Gram(v) => Measure::Gram(v),
|
||||
M::KiloGram(v) => Measure::KiloGram(v),
|
||||
M::MilliLiter(v) => Measure::MilliLiter(v),
|
||||
M::Liter(v) => Measure::Liter(v),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -20,15 +20,15 @@ pub fn start() {
|
|||
let ctx = rest::Context::from(url.query_params());
|
||||
let _ = match url.path_segments() {
|
||||
["api", "ingredients"] => {
|
||||
let res = rest::ctrl::ingredient::fetch_list(&ctx, &url);
|
||||
let res = rest::ingredient::ctrl::fetch_list(&ctx, &url);
|
||||
rq.respond(res)
|
||||
}
|
||||
["api", "ingredients", key] => {
|
||||
let res = rest::ctrl::ingredient::fetch_by_key(&ctx, &url, key);
|
||||
let res = rest::ingredient::ctrl::fetch_by_key(&ctx, &url, key);
|
||||
rq.respond(res)
|
||||
}
|
||||
["api", "recipes"] => {
|
||||
let res = rest::ctrl::recipe::fetch_list(&ctx, &url);
|
||||
let res = rest::recipe::ctrl::fetch_list(&ctx, &url);
|
||||
rq.respond(res)
|
||||
}
|
||||
_ => rq.respond(Response::from_string("Not found")),
|
||||
|
|
Loading…
Reference in New Issue