mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-23 13:02:28 +00:00
add put requests
This commit is contained in:
parent
d13155c90a
commit
6751f840da
4 changed files with 93 additions and 1 deletions
|
@ -54,6 +54,24 @@ impl CreateResource<User, Success<()>> for Users
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl ChangeAllResource<Vec<User>, Success<()>> for Users
|
||||||
|
{
|
||||||
|
fn change_all(_state : &mut State, body : Vec<User>) -> Success<()>
|
||||||
|
{
|
||||||
|
info!("Changing all Users to {:?}", body.into_iter().map(|u| u.username).collect::<Vec<String>>());
|
||||||
|
().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ChangeResource<u64, User, Success<()>> for Users
|
||||||
|
{
|
||||||
|
fn change(_state : &mut State, id : u64, body : User) -> Success<()>
|
||||||
|
{
|
||||||
|
info!("Change User {} to {}", id, body.username);
|
||||||
|
().into()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Resource for Users
|
impl Resource for Users
|
||||||
{
|
{
|
||||||
fn setup<D : DrawResourceRoutes>(mut route : D)
|
fn setup<D : DrawResourceRoutes>(mut route : D)
|
||||||
|
@ -61,6 +79,8 @@ impl Resource for Users
|
||||||
route.index::<_, Self>();
|
route.index::<_, Self>();
|
||||||
route.get::<_, _, Self>();
|
route.get::<_, _, Self>();
|
||||||
route.create::<_, _, Self>();
|
route.create::<_, _, Self>();
|
||||||
|
route.change_all::<_, _, Self>();
|
||||||
|
route.change::<_, _, _, Self>();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,9 @@ pub use resource::{
|
||||||
Resource,
|
Resource,
|
||||||
IndexResource,
|
IndexResource,
|
||||||
GetResource,
|
GetResource,
|
||||||
CreateResource
|
CreateResource,
|
||||||
|
ChangeAllResource,
|
||||||
|
ChangeResource
|
||||||
};
|
};
|
||||||
|
|
||||||
mod result;
|
mod result;
|
||||||
|
|
|
@ -29,3 +29,17 @@ pub trait CreateResource<Body : DeserializeOwned, R : ResourceResult>
|
||||||
{
|
{
|
||||||
fn create(state : &mut State, body : Body) -> R;
|
fn create(state : &mut State, body : Body) -> R;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Handle a PUT request on the Resource root.
|
||||||
|
pub trait ChangeAllResource<Body : DeserializeOwned, R : ResourceResult>
|
||||||
|
{
|
||||||
|
fn change_all(state : &mut State, body : Body) -> R;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handle a PUT request on the Resource with an id.
|
||||||
|
pub trait ChangeResource<ID, Body : DeserializeOwned, R : ResourceResult>
|
||||||
|
where
|
||||||
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
||||||
|
{
|
||||||
|
fn change(state : &mut State, id : ID, body : Body) -> R;
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
result::ResourceError,
|
result::ResourceError,
|
||||||
|
ChangeAllResource,
|
||||||
|
ChangeResource,
|
||||||
CreateResource,
|
CreateResource,
|
||||||
GetResource,
|
GetResource,
|
||||||
IndexResource,
|
IndexResource,
|
||||||
|
@ -56,6 +58,19 @@ pub trait DrawResourceRoutes
|
||||||
Body : DeserializeOwned,
|
Body : DeserializeOwned,
|
||||||
R : ResourceResult,
|
R : ResourceResult,
|
||||||
CR : CreateResource<Body, R>;
|
CR : CreateResource<Body, R>;
|
||||||
|
|
||||||
|
fn change_all<Body, R, CR>(&mut self)
|
||||||
|
where
|
||||||
|
Body : DeserializeOwned,
|
||||||
|
R : ResourceResult,
|
||||||
|
CR : ChangeAllResource<Body, R>;
|
||||||
|
|
||||||
|
fn change<ID, Body, R, CR>(&mut self)
|
||||||
|
where
|
||||||
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
|
Body : DeserializeOwned,
|
||||||
|
R : ResourceResult,
|
||||||
|
CR : ChangeResource<ID, Body, R>;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
|
fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
|
||||||
|
@ -137,6 +152,25 @@ fn create_handler<Body : DeserializeOwned, R : ResourceResult, CR : CreateResour
|
||||||
handle_with_body::<Body, _, _>(state, |state, body| CR::create(state, body))
|
handle_with_body::<Body, _, _>(state, |state, body| CR::create(state, body))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn change_all_handler<Body : DeserializeOwned, R : ResourceResult, CR : ChangeAllResource<Body, R>>(state : State) -> Box<HandlerFuture>
|
||||||
|
{
|
||||||
|
handle_with_body::<Body, _, _>(state, |state, body| CR::change_all(state, body))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn change_handler<ID, Body, R, CR>(state : State) -> Box<HandlerFuture>
|
||||||
|
where
|
||||||
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
|
Body : DeserializeOwned,
|
||||||
|
R : ResourceResult,
|
||||||
|
CR : ChangeResource<ID, Body, R>
|
||||||
|
{
|
||||||
|
let id = {
|
||||||
|
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
|
||||||
|
path.id.clone()
|
||||||
|
};
|
||||||
|
handle_with_body::<Body, _, _>(state, |state, body| CR::change(state, id, body))
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! implDrawResourceRoutes {
|
macro_rules! implDrawResourceRoutes {
|
||||||
($implType:ident) => {
|
($implType:ident) => {
|
||||||
impl<'a, C, P> DrawResources for $implType<'a, C, P>
|
impl<'a, C, P> DrawResources for $implType<'a, C, P>
|
||||||
|
@ -184,6 +218,28 @@ macro_rules! implDrawResourceRoutes {
|
||||||
self.0.post(&self.1)
|
self.0.post(&self.1)
|
||||||
.to(|state| create_handler::<Body, R, CR>(state));
|
.to(|state| create_handler::<Body, R, CR>(state));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn change_all<Body, R, CR>(&mut self)
|
||||||
|
where
|
||||||
|
Body : DeserializeOwned,
|
||||||
|
R : ResourceResult,
|
||||||
|
CR : ChangeAllResource<Body, R>
|
||||||
|
{
|
||||||
|
self.0.put(&self.1)
|
||||||
|
.to(|state| change_all_handler::<Body, R, CR>(state));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn change<ID, Body, R, CR>(&mut self)
|
||||||
|
where
|
||||||
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||||
|
Body : DeserializeOwned,
|
||||||
|
R : ResourceResult,
|
||||||
|
CR : ChangeResource<ID, Body, R>
|
||||||
|
{
|
||||||
|
self.0.put(&format!("{}/:id", self.1))
|
||||||
|
.with_path_extractor::<PathExtractor<ID>>()
|
||||||
|
.to(|state| change_handler::<ID, Body, R, CR>(state));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue