diff --git a/examples/users.rs b/examples/users.rs index 7a3a78b..1cfe938 100644 --- a/examples/users.rs +++ b/examples/users.rs @@ -72,6 +72,24 @@ impl ResourceUpdate> for Users } } +impl ResourceDeleteAll> for Users +{ + fn delete_all(_state : &mut State) -> Success<()> + { + info!("Delete all Users"); + ().into() + } +} + +impl ResourceDelete> for Users +{ + fn delete(_state : &mut State, id : u64) -> Success<()> + { + info!("Delete User {}", id); + ().into() + } +} + impl Resource for Users { fn setup(mut route : D) diff --git a/src/lib.rs b/src/lib.rs index 936f179..7a3cfba 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,9 @@ pub use resource::{ ResourceRead, ResourceCreate, ResourceUpdateAll, - ResourceUpdate + ResourceUpdate, + ResourceDeleteAll, + ResourceDelete }; mod result; diff --git a/src/resource.rs b/src/resource.rs index 6bad8b2..5bc603b 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -43,3 +43,17 @@ where { fn update(state : &mut State, id : ID, body : Body) -> R; } + +/// Handle a DELETE request on the Resource root. +pub trait ResourceDeleteAll +{ + fn delete_all(state : &mut State) -> R; +} + +/// Handle a DELETE request on the Resource with an id. +pub trait ResourceDelete +where + ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static +{ + fn delete(state : &mut State, id : ID) -> R; +} diff --git a/src/routing.rs b/src/routing.rs index 1701843..7da5691 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -65,6 +65,17 @@ pub trait DrawResourceRoutes Body : DeserializeOwned, Res : ResourceResult, Handler : ResourceUpdate; + + fn delete_all(&mut self) + where + Res : ResourceResult, + Handler : ResourceDeleteAll; + + fn delete(&mut self) + where + ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, + Res : ResourceResult, + Handler : ResourceDelete; } fn to_handler_future(mut state : State, get_result : F) -> Box @@ -178,6 +189,27 @@ where handle_with_body::(state, |state, body| Handler::update(state, id, body)) } +fn delete_all_handler(state : State) -> Box +where + Res : ResourceResult, + Handler : ResourceDeleteAll +{ + to_handler_future(state, |state| Handler::delete_all(state)) +} + +fn delete_handler(state : State) -> Box +where + ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, + Res : ResourceResult, + Handler : ResourceDelete +{ + let id = { + let path : &PathExtractor = PathExtractor::borrow_from(&state); + path.id.clone() + }; + to_handler_future(state, |state| Handler::delete(state, id)) +} + macro_rules! implDrawResourceRoutes { ($implType:ident) => { impl<'a, C, P> DrawResources for $implType<'a, C, P> @@ -247,6 +279,26 @@ macro_rules! implDrawResourceRoutes { .with_path_extractor::>() .to(|state| update_handler::(state)); } + + fn delete_all(&mut self) + where + Res : ResourceResult, + Handler : ResourceDeleteAll + { + self.0.delete(&self.1) + .to(|state| delete_all_handler::(state)); + } + + fn delete(&mut self) + where + ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, + Res : ResourceResult, + Handler : ResourceDelete + { + self.0.delete(&format!("{}/:id", self.1)) + .with_path_extractor::>() + .to(|state| delete_handler::(state)); + } } } }