diff options
author | Bryan Newbold <bnewbold@robocracy.org> | 2018-11-26 23:43:56 -0800 |
---|---|---|
committer | Bryan Newbold <bnewbold@robocracy.org> | 2018-11-26 23:54:59 -0800 |
commit | d1fd57e7c5dcd9e0062163b4be997808084ef538 (patch) | |
tree | 5eacb7bc31292dcf1d17f26d3cc6138daf6f62f7 /rust/src/api_wrappers.rs | |
parent | e82f0cf5c4f27c099a8c52eeaec015fb78b7dde3 (diff) | |
download | fatcat-d1fd57e7c5dcd9e0062163b4be997808084ef538.tar.gz fatcat-d1fd57e7c5dcd9e0062163b4be997808084ef538.zip |
implement hide flag
Diffstat (limited to 'rust/src/api_wrappers.rs')
-rw-r--r-- | rust/src/api_wrappers.rs | 56 |
1 files changed, 50 insertions, 6 deletions
diff --git a/rust/src/api_wrappers.rs b/rust/src/api_wrappers.rs index 0daff73b..50a1665e 100644 --- a/rust/src/api_wrappers.rs +++ b/rust/src/api_wrappers.rs @@ -30,17 +30,22 @@ macro_rules! wrap_entity_handlers { &self, id: String, expand: Option<String>, + hide: Option<String>, _context: &Context, ) -> Box<Future<Item = $get_resp, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); // No transaction for GET let ret = match conn.transaction(|| { let entity_id = FatCatId::from_str(&id)?; + let hide_flags = match hide { + None => HideFlags::none(), + Some(param) => HideFlags::from_str(¶m)?, + }; match expand { - None => $model::db_get(&conn, entity_id), + None => $model::db_get(&conn, entity_id, hide_flags), Some(param) => { let expand_flags = ExpandFlags::from_str(¶m)?; - let mut entity = $model::db_get(&conn, entity_id)?; + let mut entity = $model::db_get(&conn, entity_id, hide_flags)?; entity.db_expand(&conn, expand_flags)?; Ok(entity) }, @@ -241,11 +246,16 @@ macro_rules! wrap_lookup_handler { fn $get_fn( &self, $idname: $idtype, + hide: Option<String>, _context: &Context, ) -> Box<Future<Item = $get_resp, Error = ApiError> + Send> { let conn = self.db_pool.get().expect("db_pool error"); + let hide_flags = match hide { + None => HideFlags::none(), + Some(param) => HideFlags::from_str(¶m).unwrap(), + }; // No transaction for GET - let ret = match self.$get_handler(&$idname, &conn) { + let ret = match self.$get_handler(&$idname, hide_flags, &conn) { Ok(entity) => $get_resp::FoundEntity(entity), Err(Error(ErrorKind::Diesel(::diesel::result::Error::NotFound), _)) => @@ -291,6 +301,40 @@ macro_rules! wrap_fcid_handler { } } +macro_rules! wrap_fcid_hide_handler { + ($get_fn:ident, $get_handler:ident, $get_resp:ident) => { + fn $get_fn( + &self, + id: String, + hide: Option<String>, + _context: &Context, + ) -> Box<Future<Item = $get_resp, Error = ApiError> + Send> { + let conn = self.db_pool.get().expect("db_pool error"); + // No transaction for GET + let ret = match (|| { + let fcid = FatCatId::from_str(&id)?; + let hide_flags = match hide { + None => HideFlags::none(), + Some(param) => HideFlags::from_str(¶m)?, + }; + self.$get_handler(fcid, hide_flags, &conn) + })() { + Ok(entity) => + $get_resp::Found(entity), + Err(Error(ErrorKind::Diesel(::diesel::result::Error::NotFound), _)) => + $get_resp::NotFound(ErrorResponse { message: format!("Not found: {}", id) }), + Err(Error(ErrorKind::MalformedExternalId(e), _)) => + $get_resp::BadRequest(ErrorResponse { message: e.to_string() }), + Err(e) => { + error!("{}", e); + $get_resp::BadRequest(ErrorResponse { message: e.to_string() }) + }, + }; + Box::new(futures::done(Ok(ret))) + } + } +} + impl Api for Server { wrap_entity_handlers!( get_container, @@ -403,17 +447,17 @@ impl Api for Server { String ); - wrap_fcid_handler!( + wrap_fcid_hide_handler!( get_release_files, get_release_files_handler, GetReleaseFilesResponse ); - wrap_fcid_handler!( + wrap_fcid_hide_handler!( get_work_releases, get_work_releases_handler, GetWorkReleasesResponse ); - wrap_fcid_handler!( + wrap_fcid_hide_handler!( get_creator_releases, get_creator_releases_handler, GetCreatorReleasesResponse |