1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-23 13:02:28 +00:00

add delete resources

This commit is contained in:
Dominic 2019-09-29 19:19:38 +02:00
parent 2e2cb1b787
commit 3626e5360c
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
4 changed files with 87 additions and 1 deletions

View file

@ -72,6 +72,24 @@ impl ResourceUpdate<u64, User, Success<()>> for Users
} }
} }
impl ResourceDeleteAll<Success<()>> for Users
{
fn delete_all(_state : &mut State) -> Success<()>
{
info!("Delete all Users");
().into()
}
}
impl ResourceDelete<u64, Success<()>> for Users
{
fn delete(_state : &mut State, id : u64) -> Success<()>
{
info!("Delete User {}", id);
().into()
}
}
impl Resource for Users impl Resource for Users
{ {
fn setup<D : DrawResourceRoutes>(mut route : D) fn setup<D : DrawResourceRoutes>(mut route : D)

View file

@ -10,7 +10,9 @@ pub use resource::{
ResourceRead, ResourceRead,
ResourceCreate, ResourceCreate,
ResourceUpdateAll, ResourceUpdateAll,
ResourceUpdate ResourceUpdate,
ResourceDeleteAll,
ResourceDelete
}; };
mod result; mod result;

View file

@ -43,3 +43,17 @@ where
{ {
fn update(state : &mut State, id : ID, body : Body) -> R; fn update(state : &mut State, id : ID, body : Body) -> R;
} }
/// Handle a DELETE request on the Resource root.
pub trait ResourceDeleteAll<R : ResourceResult>
{
fn delete_all(state : &mut State) -> R;
}
/// Handle a DELETE request on the Resource with an id.
pub trait ResourceDelete<ID, R : ResourceResult>
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
{
fn delete(state : &mut State, id : ID) -> R;
}

View file

@ -65,6 +65,17 @@ pub trait DrawResourceRoutes
Body : DeserializeOwned, Body : DeserializeOwned,
Res : ResourceResult, Res : ResourceResult,
Handler : ResourceUpdate<ID, Body, Res>; Handler : ResourceUpdate<ID, Body, Res>;
fn delete_all<Handler, Res>(&mut self)
where
Res : ResourceResult,
Handler : ResourceDeleteAll<Res>;
fn delete<Handler, ID, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceDelete<ID, Res>;
} }
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>
@ -178,6 +189,27 @@ where
handle_with_body::<Body, _, _>(state, |state, body| Handler::update(state, id, body)) handle_with_body::<Body, _, _>(state, |state, body| Handler::update(state, id, body))
} }
fn delete_all_handler<Handler, Res>(state : State) -> Box<HandlerFuture>
where
Res : ResourceResult,
Handler : ResourceDeleteAll<Res>
{
to_handler_future(state, |state| Handler::delete_all(state))
}
fn delete_handler<Handler, ID, Res>(state : State) -> Box<HandlerFuture>
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceDelete<ID, Res>
{
let id = {
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
path.id.clone()
};
to_handler_future(state, |state| Handler::delete(state, id))
}
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>
@ -247,6 +279,26 @@ macro_rules! implDrawResourceRoutes {
.with_path_extractor::<PathExtractor<ID>>() .with_path_extractor::<PathExtractor<ID>>()
.to(|state| update_handler::<Handler, ID, Body, Res>(state)); .to(|state| update_handler::<Handler, ID, Body, Res>(state));
} }
fn delete_all<Handler, Res>(&mut self)
where
Res : ResourceResult,
Handler : ResourceDeleteAll<Res>
{
self.0.delete(&self.1)
.to(|state| delete_all_handler::<Handler, Res>(state));
}
fn delete<Handler, ID, Res>(&mut self)
where
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
Res : ResourceResult,
Handler : ResourceDelete<ID, Res>
{
self.0.delete(&format!("{}/:id", self.1))
.with_path_extractor::<PathExtractor<ID>>()
.to(|state| delete_handler::<Handler, ID, Res>(state));
}
} }
} }
} }