From 0bc7522d8c658a099c1106b7fade3c4d2acc9775 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Mon, 28 May 2018 14:48:15 -0700 Subject: refactor to more ergonomic operation names --- rust/src/api_server.rs | 257 ++++++++++++++++++++++++++----------------------- 1 file changed, 139 insertions(+), 118 deletions(-) (limited to 'rust/src') diff --git a/rust/src/api_server.rs b/rust/src/api_server.rs index 883b3ae4..33195c02 100644 --- a/rust/src/api_server.rs +++ b/rust/src/api_server.rs @@ -79,7 +79,7 @@ macro_rules! wrap_entity_handlers { } } -macro_rules! entity_batch_post_handler { +macro_rules! entity_batch_handler { ($post_handler:ident, $post_batch_handler:ident, $model:ident) => { fn $post_batch_handler(&self, entity_list: &Vec) -> Result> { let conn = self.db_pool.get().expect("db_pool error"); @@ -286,7 +286,7 @@ fn work_row2entity(ident: Option, rev: WorkRevRow) -> Result Result { + fn get_container_handler(&self, id: String) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let id = uuid::Uuid::parse_str(&id)?; @@ -299,7 +299,7 @@ impl Server { container_row2entity(Some(ident), rev) } - fn container_lookup_get_handler(&self, issnl: String) -> Result { + fn lookup_container_handler(&self, issnl: String) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let (ident, rev): (ContainerIdentRow, ContainerRevRow) = container_ident::table @@ -312,7 +312,7 @@ impl Server { container_row2entity(Some(ident), rev) } - fn creator_id_get_handler(&self, id: String) -> Result { + fn get_creator_handler(&self, id: String) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let id = uuid::Uuid::parse_str(&id)?; @@ -324,7 +324,7 @@ impl Server { creator_row2entity(Some(ident), rev) } - fn creator_lookup_get_handler(&self, orcid: String) -> Result { + fn lookup_creator_handler(&self, orcid: String) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let (ident, rev): (CreatorIdentRow, CreatorRevRow) = creator_ident::table @@ -337,7 +337,7 @@ impl Server { creator_row2entity(Some(ident), rev) } - fn file_id_get_handler(&self, id: String) -> Result { + fn get_file_handler(&self, id: String) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let id = uuid::Uuid::parse_str(&id)?; @@ -349,7 +349,7 @@ impl Server { file_row2entity(Some(ident), rev, conn) } - fn file_lookup_get_handler(&self, sha1: String) -> Result { + fn lookup_file_handler(&self, sha1: String) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let (ident, rev): (FileIdentRow, FileRevRow) = file_ident::table @@ -362,7 +362,7 @@ impl Server { file_row2entity(Some(ident), rev, conn) } - fn release_id_get_handler(&self, id: String) -> Result { + fn get_release_handler(&self, id: String) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let id = uuid::Uuid::parse_str(&id)?; @@ -374,7 +374,7 @@ impl Server { release_row2entity(Some(ident), rev, conn) } - fn release_lookup_get_handler(&self, doi: String) -> Result { + fn lookup_release_handler(&self, doi: String) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let (ident, rev): (ReleaseIdentRow, ReleaseRevRow) = release_ident::table @@ -387,7 +387,7 @@ impl Server { release_row2entity(Some(ident), rev, conn) } - fn work_id_get_handler(&self, id: String) -> Result { + fn get_work_handler(&self, id: String) -> Result { let conn = self.db_pool.get().expect("db_pool error"); let id = uuid::Uuid::parse_str(&id)?; @@ -399,12 +399,16 @@ impl Server { work_row2entity(Some(ident), rev) } - fn container_post_handler(&self, entity: models::ContainerEntity, conn: Option<&DbConn>) -> Result { + fn create_container_handler( + &self, + entity: models::ContainerEntity, + conn: Option<&DbConn>, + ) -> Result { // TODO: still can't cast for some reason // There mut be a cleaner way to manage the lifetime here let real_conn = match conn { Some(_) => None, - None => { Some(self.db_pool.get().expect("database pool")) }, + None => Some(self.db_pool.get().expect("database pool")), }; let conn = match real_conn { Some(ref c) => c, @@ -438,28 +442,33 @@ impl Server { edit.to_model() } - entity_batch_post_handler!(container_post_handler, container_batch_post_handler, ContainerEntity); - entity_batch_post_handler!(creator_post_handler, creator_batch_post_handler, CreatorEntity); - entity_batch_post_handler!(file_post_handler, file_batch_post_handler, FileEntity); - entity_batch_post_handler!(release_post_handler, release_batch_post_handler, ReleaseEntity); - entity_batch_post_handler!(work_post_handler, work_batch_post_handler, WorkEntity); -/* XXX: - fn container_batch_post_handler(&self, entity_list: &Vec) -> Result> { - let conn = self.db_pool.get().expect("db_pool error"); - // TODO: start a transaction - let ret: Vec = vec![]; - for entity in entity_list.into_iter() { - ret.push(self.container_post_handler(*entity, Some(conn))?); - } - Ok(ret) - } -*/ + entity_batch_handler!( + create_container_handler, + create_container_batch_handler, + ContainerEntity + ); + entity_batch_handler!( + create_creator_handler, + create_creator_batch_handler, + CreatorEntity + ); + entity_batch_handler!(create_file_handler, create_file_batch_handler, FileEntity); + entity_batch_handler!( + create_release_handler, + create_release_batch_handler, + ReleaseEntity + ); + entity_batch_handler!(create_work_handler, create_work_batch_handler, WorkEntity); - fn creator_post_handler(&self, entity: models::CreatorEntity, conn: Option<&DbConn>) -> Result { + fn create_creator_handler( + &self, + entity: models::CreatorEntity, + conn: Option<&DbConn>, + ) -> Result { // There mut be a cleaner way to manage the lifetime here let real_conn = match conn { Some(_) => None, - None => { Some(self.db_pool.get().expect("database pool")) }, + None => Some(self.db_pool.get().expect("database pool")), }; let conn = match real_conn { Some(ref c) => c, @@ -490,11 +499,15 @@ impl Server { edit.to_model() } - fn file_post_handler(&self, entity: models::FileEntity, conn: Option<&DbConn>) -> Result { + fn create_file_handler( + &self, + entity: models::FileEntity, + conn: Option<&DbConn>, + ) -> Result { // There mut be a cleaner way to manage the lifetime here let real_conn = match conn { Some(_) => None, - None => { Some(self.db_pool.get().expect("database pool")) }, + None => Some(self.db_pool.get().expect("database pool")), }; let conn = match real_conn { Some(ref c) => c, @@ -550,11 +563,15 @@ impl Server { edit.to_model() } - fn release_post_handler(&self, entity: models::ReleaseEntity, conn: Option<&DbConn>) -> Result { + fn create_release_handler( + &self, + entity: models::ReleaseEntity, + conn: Option<&DbConn>, + ) -> Result { // There mut be a cleaner way to manage the lifetime here let real_conn = match conn { Some(_) => None, - None => { Some(self.db_pool.get().expect("database pool")) }, + None => Some(self.db_pool.get().expect("database pool")), }; let conn = match real_conn { Some(ref c) => c, @@ -654,11 +671,15 @@ impl Server { edit.to_model() } - fn work_post_handler(&self, entity: models::WorkEntity, conn: Option<&DbConn>) -> Result { + fn create_work_handler( + &self, + entity: models::WorkEntity, + conn: Option<&DbConn>, + ) -> Result { // There mut be a cleaner way to manage the lifetime here let real_conn = match conn { Some(_) => None, - None => { Some(self.db_pool.get().expect("database pool")) }, + None => Some(self.db_pool.get().expect("database pool")), }; let conn = match real_conn { Some(ref c) => c, @@ -690,7 +711,7 @@ impl Server { edit.to_model() } - fn editgroup_id_get_handler(&self, id: i64) -> Result> { + fn editgroup_handler(&self, id: i64) -> Result> { let conn = self.db_pool.get().expect("db_pool error"); let row: EditgroupRow = editgroup::table.find(id as i64).first(&conn)?; @@ -748,7 +769,7 @@ impl Server { Ok(Some(eg)) } - fn editor_get_handler(&self, username: String) -> Result> { + fn get_editor_handler(&self, username: String) -> Result> { let conn = self.db_pool.get().expect("db_pool error"); let row: EditorRow = editor::table @@ -787,134 +808,134 @@ impl Server { impl Api for Server { wrap_entity_handlers!( - container_id_get, - container_id_get_handler, - ContainerIdGetResponse, - container_post, - container_post_handler, - ContainerPostResponse, - container_batch_post, - container_batch_post_handler, - ContainerBatchPostResponse, + get_container, + get_container_handler, + GetContainerResponse, + create_container, + create_container_handler, + CreateContainerResponse, + create_container_batch, + create_container_batch_handler, + CreateContainerBatchResponse, ContainerEntity ); wrap_entity_handlers!( - creator_id_get, - creator_id_get_handler, - CreatorIdGetResponse, - creator_post, - creator_post_handler, - CreatorPostResponse, - creator_batch_post, - creator_batch_post_handler, - CreatorBatchPostResponse, + get_creator, + get_creator_handler, + GetCreatorResponse, + create_creator, + create_creator_handler, + CreateCreatorResponse, + create_creator_batch, + create_creator_batch_handler, + CreateCreatorBatchResponse, CreatorEntity ); wrap_entity_handlers!( - file_id_get, - file_id_get_handler, - FileIdGetResponse, - file_post, - file_post_handler, - FilePostResponse, - file_batch_post, - file_batch_post_handler, - FileBatchPostResponse, + get_file, + get_file_handler, + GetFileResponse, + create_file, + create_file_handler, + CreateFileResponse, + create_file_batch, + create_file_batch_handler, + CreateFileBatchResponse, FileEntity ); wrap_entity_handlers!( - release_id_get, - release_id_get_handler, - ReleaseIdGetResponse, - release_post, - release_post_handler, - ReleasePostResponse, - release_batch_post, - release_batch_post_handler, - ReleaseBatchPostResponse, + get_release, + get_release_handler, + GetReleaseResponse, + create_release, + create_release_handler, + CreateReleaseResponse, + create_release_batch, + create_release_batch_handler, + CreateReleaseBatchResponse, ReleaseEntity ); wrap_entity_handlers!( - work_id_get, - work_id_get_handler, - WorkIdGetResponse, - work_post, - work_post_handler, - WorkPostResponse, - work_batch_post, - work_batch_post_handler, - WorkBatchPostResponse, + get_work, + get_work_handler, + GetWorkResponse, + create_work, + create_work_handler, + CreateWorkResponse, + create_work_batch, + create_work_batch_handler, + CreateWorkBatchResponse, WorkEntity ); wrap_lookup_handler!( - container_lookup_get, - container_lookup_get_handler, - ContainerLookupGetResponse, + lookup_container, + lookup_container_handler, + LookupContainerResponse, issnl, String ); wrap_lookup_handler!( - creator_lookup_get, - creator_lookup_get_handler, - CreatorLookupGetResponse, + lookup_creator, + lookup_creator_handler, + LookupCreatorResponse, orcid, String ); wrap_lookup_handler!( - file_lookup_get, - file_lookup_get_handler, - FileLookupGetResponse, + lookup_file, + lookup_file_handler, + LookupFileResponse, sha1, String ); wrap_lookup_handler!( - release_lookup_get, - release_lookup_get_handler, - ReleaseLookupGetResponse, + lookup_release, + lookup_release_handler, + LookupReleaseResponse, doi, String ); - fn editgroup_id_accept_post( + fn accept_editgroup( &self, id: i64, _context: &Context, - ) -> Box + Send> { + ) -> Box + Send> { let conn = self.db_pool.get().expect("db_pool error"); accept_editgroup(id as i64, &conn).expect("failed to accept editgroup"); - let ret = EditgroupIdAcceptPostResponse::MergedSuccessfully(Success { + let ret = AcceptEditgroupResponse::MergedSuccessfully(Success { message: "horray!".to_string(), }); Box::new(futures::done(Ok(ret))) } - fn editgroup_id_get( + fn get_editgroup( &self, id: i64, _context: &Context, - ) -> Box + Send> { - let ret = match self.editgroup_id_get_handler(id) { + ) -> Box + Send> { + let ret = match self.editgroup_handler(id) { Ok(Some(entity)) => - EditgroupIdGetResponse::FoundEntity(entity), + GetEditgroupResponse::FoundEntity(entity), Ok(None) => - EditgroupIdGetResponse::NotFound( + GetEditgroupResponse::NotFound( ErrorResponse { message: "No such entity".to_string() }), Err(e) => // TODO: dig in to error type here - EditgroupIdGetResponse::BadRequest( + GetEditgroupResponse::BadRequest( ErrorResponse { message: e.to_string() }), }; Box::new(futures::done(Ok(ret))) } - fn editgroup_post( + fn create_editgroup( &self, entity: models::Editgroup, _context: &Context, - ) -> Box + Send> { + ) -> Box + Send> { let conn = self.db_pool.get().expect("db_pool error"); let row: EditgroupRow = insert_into(editgroup::table) @@ -934,42 +955,42 @@ impl Api for Server { extra: row.extra_json, }; Box::new(futures::done(Ok( - EditgroupPostResponse::SuccessfullyCreated(new_eg), + CreateEditgroupResponse::SuccessfullyCreated(new_eg), ))) } - fn editor_username_changelog_get( + fn get_editor_changelog( &self, username: String, _context: &Context, - ) -> Box + Send> { + ) -> Box + Send> { let ret = match self.editor_changelog_get_handler(username) { Ok(Some(entries)) => - EditorUsernameChangelogGetResponse::FoundMergedChanges(entries), + GetEditorChangelogResponse::FoundMergedChanges(entries), Ok(None) => - EditorUsernameChangelogGetResponse::NotFound( + GetEditorChangelogResponse::NotFound( ErrorResponse { message: "No such entity".to_string() }), Err(e) => // TODO: dig in to error type here - EditorUsernameChangelogGetResponse::GenericError( + GetEditorChangelogResponse::GenericError( ErrorResponse { message: e.to_string() }), }; Box::new(futures::done(Ok(ret))) } - fn editor_username_get( + fn get_editor( &self, username: String, _context: &Context, - ) -> Box + Send> { - let ret = match self.editor_get_handler(username) { + ) -> Box + Send> { + let ret = match self.get_editor_handler(username) { Ok(Some(entity)) => - EditorUsernameGetResponse::FoundEditor(entity), + GetEditorResponse::FoundEditor(entity), Ok(None) => - EditorUsernameGetResponse::NotFound(ErrorResponse { message: "No such entity".to_string() }), + GetEditorResponse::NotFound(ErrorResponse { message: "No such entity".to_string() }), Err(e) => // TODO: dig in to error type here - EditorUsernameGetResponse::GenericError(ErrorResponse { message: e.to_string() }), + GetEditorResponse::GenericError(ErrorResponse { message: e.to_string() }), }; Box::new(futures::done(Ok(ret))) } -- cgit v1.2.3