From c4618dd2fb070eefcdb38bca7b92fe32da766702 Mon Sep 17 00:00:00 2001 From: Bryan Newbold Date: Wed, 25 Jul 2018 12:18:34 -0700 Subject: more external ident handling --- rust/src/api_server.rs | 55 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 10 deletions(-) (limited to 'rust/src/api_server.rs') diff --git a/rust/src/api_server.rs b/rust/src/api_server.rs index 5aa075dd..64c028be 100644 --- a/rust/src/api_server.rs +++ b/rust/src/api_server.rs @@ -1,6 +1,6 @@ //! API endpoint handlers -use api_helpers::{accept_editgroup, fcid2uuid, get_or_create_editgroup, uuid2fcid}; +use api_helpers::*; use chrono; use database_models::*; use database_schema::{ @@ -95,6 +95,7 @@ fn container_row2entity( }; Ok(ContainerEntity { issnl: rev.issnl, + wikidata_qid: rev.wikidata_qid, publisher: rev.publisher, name: rev.name, abbrev: rev.abbrev, @@ -122,6 +123,7 @@ fn creator_row2entity(ident: Option, rev: CreatorRevRow) -> Res given_name: rev.given_name, surname: rev.surname, orcid: rev.orcid, + wikidata_qid: rev.wikidata_qid, state: state, ident: ident_id, revision: Some(rev.id.to_string()), @@ -249,6 +251,7 @@ fn release_row2entity( pmid: rev.pmid, pmcid: rev.pmcid, isbn13: rev.isbn13, + wikidata_qid: rev.wikidata_qid, volume: rev.volume, issue: rev.issue, pages: rev.pages, @@ -304,6 +307,7 @@ impl Server { pub fn lookup_container_handler(&self, issnl: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); + check_issn(issnl)?; let (ident, rev): (ContainerIdentRow, ContainerRevRow) = container_ident::table .inner_join(container_rev::table) .filter(container_rev::issnl.eq(issnl)) @@ -329,6 +333,7 @@ impl Server { pub fn lookup_creator_handler(&self, orcid: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); + check_orcid(orcid)?; let (ident, rev): (CreatorIdentRow, CreatorRevRow) = creator_ident::table .inner_join(creator_rev::table) .filter(creator_rev::orcid.eq(orcid)) @@ -397,6 +402,7 @@ impl Server { pub fn lookup_release_handler(&self, doi: &str) -> Result { let conn = self.db_pool.get().expect("db_pool error"); + check_doi(doi)?; let (ident, rev): (ReleaseIdentRow, ReleaseRevRow) = release_ident::table .inner_join(release_rev::table) .filter(release_rev::doi.eq(doi)) @@ -472,20 +478,27 @@ impl Server { None => get_or_create_editgroup(editor_id, &conn)?, Some(param) => fcid2uuid(¶m)?, }; + if let Some(ref extid) = entity.wikidata_qid { + check_wikidata_qid(extid)?; + } + if let Some(ref extid) = entity.issnl { + check_issn(extid)?; + } let edit: ContainerEditRow = diesel::sql_query( - "WITH rev AS ( INSERT INTO container_rev (name, publisher, issnl, abbrev, coden, extra_json) - VALUES ($1, $2, $3, $4, $5, $6) + "WITH rev AS ( INSERT INTO container_rev (name, publisher, issnl, wikidata_qid, abbrev, coden, extra_json) + VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id ), ident AS ( INSERT INTO container_ident (rev_id) VALUES ((SELECT rev.id FROM rev)) RETURNING id ) INSERT INTO container_edit (editgroup_id, ident_id, rev_id) VALUES - ($7, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev)) + ($8, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev)) RETURNING *", ).bind::(entity.name) .bind::, _>(entity.publisher) .bind::, _>(entity.issnl) + .bind::, _>(entity.wikidata_qid) .bind::, _>(entity.abbrev) .bind::, _>(entity.coden) .bind::, _>(entity.extra) @@ -514,21 +527,28 @@ impl Server { None => get_or_create_editgroup(editor_id, &conn).expect("current editgroup"), Some(param) => fcid2uuid(¶m)?, }; + if let Some(ref extid) = entity.orcid { + check_orcid(extid)?; + } + if let Some(ref extid) = entity.wikidata_qid { + check_wikidata_qid(extid)?; + } let edit: CreatorEditRow = diesel::sql_query( - "WITH rev AS ( INSERT INTO creator_rev (display_name, given_name, surname, orcid, extra_json) - VALUES ($1, $2, $3, $4, $5) + "WITH rev AS ( INSERT INTO creator_rev (display_name, given_name, surname, orcid, wikidata_qid, extra_json) + VALUES ($1, $2, $3, $4, $5, $6) RETURNING id ), ident AS ( INSERT INTO creator_ident (rev_id) VALUES ((SELECT rev.id FROM rev)) RETURNING id ) INSERT INTO creator_edit (editgroup_id, ident_id, rev_id) VALUES - ($6, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev)) + ($7, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev)) RETURNING *", ).bind::(entity.display_name) .bind::, _>(entity.given_name) .bind::, _>(entity.surname) .bind::, _>(entity.orcid) + .bind::, _>(entity.wikidata_qid) .bind::, _>(entity.extra) .bind::(editgroup_id) .get_result(conn)?; @@ -644,6 +664,18 @@ impl Server { None => get_or_create_editgroup(editor_id, &conn).expect("current editgroup"), Some(param) => fcid2uuid(¶m)?, }; + if let Some(ref extid) = entity.doi { + check_doi(extid)?; + } + if let Some(ref extid) = entity.pmid { + check_pmid(extid)?; + } + if let Some(ref extid) = entity.pmcid { + check_pmcid(extid)?; + } + if let Some(ref extid) = entity.wikidata_qid { + check_wikidata_qid(extid)?; + } let work_id = match entity.work_id { Some(work_id) => fcid2uuid(&work_id)?, @@ -668,14 +700,14 @@ impl Server { }; let edit: ReleaseEditRow = diesel::sql_query( - "WITH rev AS ( INSERT INTO release_rev (title, release_type, release_status, release_date, doi, isbn13, volume, issue, pages, work_ident_id, container_ident_id, publisher, language, extra_json) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) + "WITH rev AS ( INSERT INTO release_rev (title, release_type, release_status, release_date, doi, pmid, pmcid, wikidata_qid, isbn13, volume, issue, pages, work_ident_id, container_ident_id, publisher, language, extra_json) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17) RETURNING id ), ident AS ( INSERT INTO release_ident (rev_id) VALUES ((SELECT rev.id FROM rev)) RETURNING id ) INSERT INTO release_edit (editgroup_id, ident_id, rev_id) VALUES - ($15, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev)) + ($18, (SELECT ident.id FROM ident), (SELECT rev.id FROM rev)) RETURNING *", ).bind::(entity.title) .bind::, _>(entity.release_type) @@ -683,6 +715,9 @@ impl Server { .bind::, _>( entity.release_date.map(|v| v.naive_utc().date())) .bind::, _>(entity.doi) + .bind::, _>(entity.pmid) + .bind::, _>(entity.pmcid) + .bind::, _>(entity.wikidata_qid) .bind::, _>(entity.isbn13) .bind::, _>(entity.volume) .bind::, _>(entity.issue) -- cgit v1.2.3