diff --git a/src/resource.rs b/src/resource.rs index 7b76d7b..dc857fa 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -3,7 +3,8 @@ use serde::ser::Serialize; use rs_es::Client; use rs_es::operations::index::IndexResult; -use rs_es::operations::mapping::*; +use rs_es::operations::delete::DeleteResult; +use rs_es::operations::mapping::MappingResult; use rs_es::error::EsError; use params::*; @@ -20,6 +21,9 @@ pub trait Resource: Send + Sync + Any + Deserialize + Debug { /// Respond to POST requests indexing given entity fn index(&self, mut es: &mut Client, index: &str) -> Result; - /// Respond to DELETE requests deleting all the entities from given index + /// Respond to DELETE requests on given id deleting it from given index + fn delete(mut es: &mut Client, id: &str, index: &str) -> Result; + + /// Respond to DELETE requests rebuilding and reindexing given index fn reset_index(mut es: &mut Client, index: &str) -> Result; } diff --git a/src/resources/talent.rs b/src/resources/talent.rs index 71b7484..f13e645 100644 --- a/src/resources/talent.rs +++ b/src/resources/talent.rs @@ -7,6 +7,7 @@ use super::rs_es::Client; use super::rs_es::query::Query; use super::rs_es::operations::search::{Sort, SortField, Order}; use super::rs_es::operations::index::IndexResult; +use super::rs_es::operations::delete::DeleteResult; use super::rs_es::operations::mapping::*; use super::rs_es::query::full_text::MatchQueryType; use super::rs_es::error::EsError; @@ -306,6 +307,12 @@ impl Resource for Talent { } } + /// Delete the talent associated to given id. + fn delete(mut es: &mut Client, id: &str, index: &str) -> Result { + es.delete(index, ES_TYPE, id) + .send() + } + /// Reset the given index. All the data will be destroyed and then the index /// will be created again. The map that will be used is hardcoded. #[allow(unused_must_use)] @@ -635,8 +642,10 @@ mod tests { #[test] fn test_search() { let mut client = make_client(); + assert!(Talent::reset_index(&mut client, &*config.es.index).is_ok()); refresh_index(&mut client); + assert!(populate_index(&mut client)); refresh_index(&mut client); @@ -647,6 +656,18 @@ mod tests { assert!(results.highlights().iter().all(|r| r.is_none())); } + { + assert!(Talent::delete(&mut client, "1", &*config.es.index).is_ok()); + assert!(Talent::delete(&mut client, "4", &*config.es.index).is_ok()); + refresh_index(&mut client); + + let results = Talent::search(&mut client, &*config.es.index, &Map::new()); + assert_eq!(vec![5, 2], results.ids()); + + assert!(populate_index(&mut client)); + refresh_index(&mut client); + } + // a non existing index is given { let mut map = Map::new(); diff --git a/src/server.rs b/src/server.rs index d488bd2..9068660 100644 --- a/src/server.rs +++ b/src/server.rs @@ -153,6 +153,37 @@ impl Handler for IndexableHandler { } } +pub struct DeletableHandler { + config: Config, + resource: PhantomData +} + +impl DeletableHandler { + fn new(config: Config) -> Self { + DeletableHandler:: { resource: PhantomData, config: config } + } +} + +impl WritableEndpoint for DeletableHandler {} + +impl Handler for DeletableHandler { + fn handle(&self, req: &mut Request) -> IronResult { + if !self.is_authorized(self.config.auth.to_owned(), &req.headers) { + unauthorized!(); + } + + let mut client = Client::new(&*self.config.es.host, self.config.es.port); + let ref id = try_or_422!(req.extensions.get::().unwrap() + .find("id") + .ok_or("DELETE#:id not found")); + + match R::delete(&mut client, id, &*self.config.es.index) { + Ok(_) => Ok(Response::with(status::NoContent)), + Err(_) => Ok(Response::with(status::UnprocessableEntity)) + } + } +} + pub struct ResettableHandler { config: Config, resource: PhantomData @@ -208,6 +239,9 @@ impl Server { router.post(&self.endpoint, IndexableHandler::::new(self.config.to_owned())); router.delete(&self.endpoint, ResettableHandler::::new(self.config.to_owned())); + let deletable_endpoint = format!("{}/:id", self.endpoint); + router.delete(deletable_endpoint, DeletableHandler::::new(self.config.to_owned())); + match env::var("DYNO") { // for some reasons, chain::link makes heroku crash Ok(_) => Iron::new(router).http(&*host), Err(_) => { @@ -227,7 +261,8 @@ mod tests { use rs_es::Client; use rs_es::operations::index::IndexResult; - use rs_es::operations::mapping::*; + use rs_es::operations::delete::DeleteResult; + use rs_es::operations::mapping::{MappingOperation, MappingResult}; use rs_es::error::EsError; #[derive(Serialize, Deserialize, Clone, Debug)] @@ -251,6 +286,10 @@ mod tests { .send() } + fn delete(mut es: &mut Client, id: &str, index: &str) -> Result { + es.delete(index, ES_TYPE, id) + .send() + } fn reset_index(mut es: &mut Client, index: &str) -> Result { MappingOperation::new(&mut es, index).send()