From 3a49586df2545723a6cc6280cd8c9b75a535042f Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Thu, 28 Jun 2018 12:24:06 -0700 Subject: auto-create works for releases If a work_id isn't supplied, create a new work and link it. Note that the work_id doesn't get passed back in the response, only the release_id. --- rust/src/api_server.rs | 21 +++++++++++++++++++-- rust/tests/test_api_server.rs | 15 +++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) (limited to 'rust') diff --git a/rust/src/api_server.rs b/rust/src/api_server.rs index 61005226..ca57ae29 100644 --- a/rust/src/api_server.rs +++ b/rust/src/api_server.rs @@ -291,7 +291,7 @@ fn release_row2entity( container_id: rev.container_ident_id.map(|u| u.to_string()), publisher: rev.publisher, language: rev.language, - work_id: rev.work_ident_id.to_string(), + work_id: Some(rev.work_ident_id.to_string()), refs: Some(refs), contribs: Some(contribs), state: state, @@ -676,7 +676,24 @@ impl Server { Some(param) => param as i64, }; - let work_id = uuid::Uuid::parse_str(&entity.work_id)?; + let work_id = match entity.work_id { + Some(work_id) => uuid::Uuid::parse_str(&work_id)?, + None => { + // If a work_id wasn't passed, create a new work under the current editgroup + let work_model = models::WorkEntity { + work_type: None, + ident: None, + revision: None, + redirect: None, + state: None, + editgroup_id: Some(editgroup_id), + extra: None, + }; + let new_entity = self.create_work_handler(work_model, Some(&conn))?; + uuid::Uuid::parse_str(&new_entity.ident)? + } + }; + let container_id: Option = match entity.container_id { Some(id) => Some(uuid::Uuid::parse_str(&id)?), None => None, diff --git a/rust/tests/test_api_server.rs b/rust/tests/test_api_server.rs index 7d63afe9..3db01f65 100644 --- a/rust/tests/test_api_server.rs +++ b/rust/tests/test_api_server.rs @@ -286,6 +286,21 @@ fn test_post_release() { None, ); // TODO: "secret paper" + // No work_id supplied (auto-created) + check_response( + request::post( + "http://localhost:9411/v0/release", + headers.clone(), + // TODO: target_release_id + r#"{"title": "secret minimal paper the second", + "release_type": "journal-article" + }"#, + &router, + ), + status::Created, + None, + ); + check_response( request::post( "http://localhost:9411/v0/release", -- cgit v1.2.3