99 lines
2.9 KiB
Rust
99 lines
2.9 KiB
Rust
use std::io::Cursor;
|
|
use std::str::FromStr;
|
|
|
|
use tiny_http::{Header, Response};
|
|
|
|
use crate::domain;
|
|
use crate::domain::misc_types::Lang;
|
|
use crate::repo::ingredient::StaticIngredientRepo;
|
|
use crate::rest::types::{QueryParams, Url};
|
|
|
|
#[derive(Default, Debug)]
|
|
struct FetchIngredientsOpts<'a> {
|
|
lang: Option<&'a str>,
|
|
keys: Option<Vec<&'a str>>,
|
|
}
|
|
|
|
impl<'a> From<QueryParams<'a>> for FetchIngredientsOpts<'a> {
|
|
fn from(params: QueryParams<'a>) -> Self {
|
|
params
|
|
.into_iter()
|
|
.fold(FetchIngredientsOpts::default(), |mut opts, p| {
|
|
match p {
|
|
("lang", val) => opts.lang = Some(val),
|
|
("key", val) => {
|
|
let mut keys = opts.keys.unwrap_or_default();
|
|
keys.push(val);
|
|
opts.keys = Some(keys)
|
|
}
|
|
_ => {}
|
|
};
|
|
|
|
opts
|
|
})
|
|
}
|
|
}
|
|
|
|
impl From<FetchIngredientsOpts<'_>> for domain::ingredient::fetch_list::RequestOpts {
|
|
fn from(rest: FetchIngredientsOpts) -> Self {
|
|
let lang = rest.lang.and_then(|l| Lang::from_str(l).ok());
|
|
let keys = rest
|
|
.keys
|
|
.map(|keys| keys.into_iter().map(String::from).collect());
|
|
Self { lang, keys }
|
|
}
|
|
}
|
|
|
|
pub fn fetch_list(url: &Url) -> Response<Cursor<Vec<u8>>> {
|
|
use domain::ingredient::fetch_list;
|
|
let opts = FetchIngredientsOpts::from(url.query_params());
|
|
|
|
let repo = StaticIngredientRepo;
|
|
let ingredients = fetch_list::execute(&repo, opts.into());
|
|
let data = serde_json::to_string(&ingredients).unwrap();
|
|
|
|
Response::from_string(data)
|
|
.with_header(Header::from_str("content-type: application/json").unwrap())
|
|
}
|
|
|
|
#[derive(Default, Debug)]
|
|
struct FetchIngredientOpts<'a> {
|
|
lang: Option<&'a str>,
|
|
}
|
|
|
|
impl<'a> From<QueryParams<'a>> for FetchIngredientOpts<'a> {
|
|
fn from(params: QueryParams<'a>) -> Self {
|
|
params
|
|
.into_iter()
|
|
.fold(FetchIngredientOpts::default(), |mut opts, p| {
|
|
match p {
|
|
("lang", val) => opts.lang = Some(val),
|
|
_ => {}
|
|
};
|
|
|
|
opts
|
|
})
|
|
}
|
|
}
|
|
|
|
impl From<FetchIngredientOpts<'_>> for domain::ingredient::fetch_by_key::RequestOpts {
|
|
fn from(rest: FetchIngredientOpts) -> Self {
|
|
let lang = rest.lang.and_then(|l| Lang::from_str(l).ok());
|
|
Self { lang }
|
|
}
|
|
}
|
|
|
|
pub fn fetch_by_key(url: &Url, key: &str) -> Response<Cursor<Vec<u8>>> {
|
|
use domain::ingredient::fetch_by_key;
|
|
let opts = FetchIngredientOpts::from(url.query_params());
|
|
|
|
let repo = StaticIngredientRepo;
|
|
|
|
// TODO: catch notfound error
|
|
let ingredient = fetch_by_key::execute(&repo, key.to_string(), opts.into()).ok();
|
|
let data = serde_json::to_string(&ingredient).unwrap();
|
|
|
|
Response::from_string(data)
|
|
.with_header(Header::from_str("content-type: application/json").unwrap())
|
|
}
|