This commit is contained in:
2021-10-29 18:42:04 +02:00
parent 4c689a9a9e
commit f2c470ed3d
19 changed files with 6097 additions and 744 deletions

View File

@@ -1,4 +1,4 @@
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::convert::Infallible;
use warp;
@@ -19,9 +19,28 @@ struct ErrorMessage {
message: String,
}
#[derive(Deserialize, Clone)]
#[serde(deny_unknown_fields)]
struct JsonListItem {
name: String,
count: i32,
}
pub fn new() -> warp::filters::BoxedFilter<(impl warp::Reply,)> {
let accept_json = warp::header::exact("accept", "application/json");
let cors = warp::cors().allow_any_origin();
let content_json = warp::header::exact("content-type", "application/json");
let cors = warp::cors()
.allow_any_origin()
.allow_methods(&[
warp::http::Method::GET,
warp::http::Method::POST,
warp::http::Method::DELETE,
])
.allow_headers(vec!["accept", "content-type"]);
fn json_body() -> impl Filter<Extract = (JsonListItem,), Error = warp::Rejection> + Clone {
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
}
let root = warp::path::end().map(|| "Hi");
@@ -55,6 +74,56 @@ pub fn new() -> warp::filters::BoxedFilter<(impl warp::Reply,)> {
})
.with(&cors);
let list_items = warp::path!("v1" / "lists" / String / "items")
.and(warp::path::end())
.and(warp::get())
.and(accept_json)
.and_then(|list_id: String| async move {
match Uuid::parse_str(&list_id) {
Ok(uuid) => {
let items = &super::get_packagelist_items(uuid);
Ok(warp::reply::json(items))
}
Err(_) => Err(warp::reject::custom(InvalidUuid)),
}
})
.with(&cors);
let preparation = warp::path!("v1" / "lists" / String / "items" / String / "preparation")
.and(warp::path::end())
.and(warp::get())
.and(accept_json)
.and_then(|list_id: String, item_id: String| async move {
match Uuid::parse_str(&list_id) {
Ok(list_id) => match Uuid::parse_str(&item_id) {
Err(_) => Err(warp::reject::custom(InvalidUuid)),
Ok(item_id) => {
let items = &super::get_preparation(list_id, item_id);
Ok(warp::reply::json(items))
}
},
Err(_) => Err(warp::reject::custom(InvalidUuid)),
}
})
.with(&cors);
let new_item = warp::path!("v1" / "lists" / String / "items")
.and(warp::path::end())
.and(warp::post())
.and(accept_json)
.and(content_json)
.and(json_body())
.and_then(|list_id: String, item: JsonListItem| async move {
match Uuid::parse_str(&list_id) {
Ok(list_id) => {
let new_item = &super::new_item(list_id, item.name, item.count);
Ok(warp::reply::json(new_item))
}
Err(_) => Err(warp::reject::custom(InvalidUuid)),
}
})
.with(&cors);
let trips = warp::path!("v1" / "trips")
.and(warp::path::end())
.and(warp::get())
@@ -65,6 +134,9 @@ pub fn new() -> warp::filters::BoxedFilter<(impl warp::Reply,)> {
root.or(v1)
.or(lists)
.or(list)
.or(new_item)
.or(list_items)
.or(preparation)
.or(trips)
.recover(handle_rejection)
.boxed()
@@ -81,10 +153,10 @@ async fn handle_rejection(err: warp::Rejection) -> Result<impl warp::Reply, Infa
} else if let Some(InvalidUuid) = err.find() {
code = StatusCode::BAD_REQUEST;
message = "INVALID_UUID";
} else if let Some(_) = err.find::<warp::filters::body::BodyDeserializeError>() {
} else if err.find::<warp::filters::body::BodyDeserializeError>().is_some() {
message = "BAD_REQUEST";
code = StatusCode::BAD_REQUEST;
} else if let Some(_) = err.find::<warp::reject::MethodNotAllowed>() {
} else if err.find::<warp::reject::MethodNotAllowed>().is_some() {
message = "METHOD_NOT_ALLOWED";
code = StatusCode::METHOD_NOT_ALLOWED;
} else {