diff --git a/examples/users.rs b/examples/users.rs index bcda16e..13cebea 100644 --- a/examples/users.rs +++ b/examples/users.rs @@ -54,6 +54,24 @@ impl CreateResource> for Users } } +impl ChangeAllResource, Success<()>> for Users +{ + fn change_all(_state : &mut State, body : Vec) -> Success<()> + { + info!("Changing all Users to {:?}", body.into_iter().map(|u| u.username).collect::>()); + ().into() + } +} + +impl ChangeResource> for Users +{ + fn change(_state : &mut State, id : u64, body : User) -> Success<()> + { + info!("Change User {} to {}", id, body.username); + ().into() + } +} + impl Resource for Users { fn setup(mut route : D) @@ -61,6 +79,8 @@ impl Resource for Users route.index::<_, Self>(); route.get::<_, _, Self>(); route.create::<_, _, Self>(); + route.change_all::<_, _, Self>(); + route.change::<_, _, _, Self>(); } } diff --git a/src/lib.rs b/src/lib.rs index 05a9345..bb1bae5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,9 @@ pub use resource::{ Resource, IndexResource, GetResource, - CreateResource + CreateResource, + ChangeAllResource, + ChangeResource }; mod result; diff --git a/src/resource.rs b/src/resource.rs index 15bdb9f..ea2c055 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -29,3 +29,17 @@ pub trait CreateResource { fn create(state : &mut State, body : Body) -> R; } + +/// Handle a PUT request on the Resource root. +pub trait ChangeAllResource +{ + fn change_all(state : &mut State, body : Body) -> R; +} + +/// Handle a PUT request on the Resource with an id. +pub trait ChangeResource +where + ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static +{ + fn change(state : &mut State, id : ID, body : Body) -> R; +} diff --git a/src/routing.rs b/src/routing.rs index 4a159ee..246171f 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -1,5 +1,7 @@ use crate::{ result::ResourceError, + ChangeAllResource, + ChangeResource, CreateResource, GetResource, IndexResource, @@ -56,6 +58,19 @@ pub trait DrawResourceRoutes Body : DeserializeOwned, R : ResourceResult, CR : CreateResource; + + fn change_all(&mut self) + where + Body : DeserializeOwned, + R : ResourceResult, + CR : ChangeAllResource; + + fn change(&mut self) + where + ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, + Body : DeserializeOwned, + R : ResourceResult, + CR : ChangeResource; } fn to_handler_future(mut state : State, get_result : F) -> Box @@ -137,6 +152,25 @@ fn create_handler(state, |state, body| CR::create(state, body)) } +fn change_all_handler>(state : State) -> Box +{ + handle_with_body::(state, |state, body| CR::change_all(state, body)) +} + +fn change_handler(state : State) -> Box +where + ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, + Body : DeserializeOwned, + R : ResourceResult, + CR : ChangeResource +{ + let id = { + let path : &PathExtractor = PathExtractor::borrow_from(&state); + path.id.clone() + }; + handle_with_body::(state, |state, body| CR::change(state, id, body)) +} + macro_rules! implDrawResourceRoutes { ($implType:ident) => { impl<'a, C, P> DrawResources for $implType<'a, C, P> @@ -184,6 +218,28 @@ macro_rules! implDrawResourceRoutes { self.0.post(&self.1) .to(|state| create_handler::(state)); } + + fn change_all(&mut self) + where + Body : DeserializeOwned, + R : ResourceResult, + CR : ChangeAllResource + { + self.0.put(&self.1) + .to(|state| change_all_handler::(state)); + } + + fn change(&mut self) + where + ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, + Body : DeserializeOwned, + R : ResourceResult, + CR : ChangeResource + { + self.0.put(&format!("{}/:id", self.1)) + .with_path_extractor::>() + .to(|state| change_handler::(state)); + } } } }