From 6e72fb3dace2709c52a55fb3b4c82d5ac8f7ee03 Mon Sep 17 00:00:00 2001 From: punkish Date: Mon, 27 Apr 2020 09:43:16 +0200 Subject: [PATCH] more clean up, more comments, plus fixed a bug in the generation of ddKeys in dd2queries.js --- api/v2/lib/dd2queries.js | 15 +++++++++++---- api/v2/lib/qparts.js | 19 +++++++++++-------- api/v2/lib/zenodeo.js | 3 +++ api/v2/utils.js | 17 ++++++++++++++--- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/api/v2/lib/dd2queries.js b/api/v2/lib/dd2queries.js index 50b0aee..33defed 100644 --- a/api/v2/lib/dd2queries.js +++ b/api/v2/lib/dd2queries.js @@ -119,11 +119,14 @@ const calcQuery = function(ddKeys, queryGroup, query, queryObject, matchTables, const offset = queryObject.offset || query.offset || 0; if (matchTables.length) { + tables.push(...matchTables); } if (queryGroup !== 'taxonStats' && queryGroup !== 'related') { + if (addedConstraint.length) { + constraint.push(...addedConstraint); } } @@ -134,11 +137,13 @@ const calcQuery = function(ddKeys, queryGroup, query, queryObject, matchTables, // now, figure out the sort params, if applicable let [sortcol, sortdir] = ['', '']; if (queryObject.sortBy && Object.keys(sortBy).length) { + [sortcol, sortdir] = calcSortParams(sortBy, queryObject); sql += ` ORDER BY ${sortcol} ${sortdir}`; } if (group && Object.keys(group).length) { + sql += ` GROUP BY ${group.join(' ')}`; } @@ -180,7 +185,11 @@ const getDdKeys = function() { table: f.table || false, resourceId: f.resourceId ? f.plaziName : false } + } + + if (f.resourceId) { + byResourceIds[resource] = f.plaziName } } @@ -219,13 +228,11 @@ const dd2queries = function(queryObject) { const doGroups = ['essential']; if (queryObject[pk]) { - console.log(`pk key: ${pk}`); - console.log(`pk val: ${queryObject[pk]}`); - doGroups.push(...[ 'related', 'taxonStats' ]); + doGroups.push(...[ 'related', 'taxonStats' ]); } else { - console.log('no pk'); + if (queryObject.facets) doGroups.push('facets'); if (queryObject.stats) doGroups.push('stats'); } diff --git a/api/v2/lib/qparts.js b/api/v2/lib/qparts.js index e30f390..ef836c2 100644 --- a/api/v2/lib/qparts.js +++ b/api/v2/lib/qparts.js @@ -215,8 +215,8 @@ const queryParts = { tables: ['materialsCitations'], constraint: [ 'deleted = 0', - 'latitude != ""', - 'longitude != ""', + "latitude != ''", + "longitude != ''", 'treatmentId = @treatmentId' ], sortBy: {}, @@ -232,7 +232,10 @@ const queryParts = { 'thumbnailUri' ], tables: ['figureCitations'], - constraint: ['deleted = 0', 'treatmentId = @treatmentId'], + constraint: [ + 'deleted = 0', + 'treatmentId = @treatmentId' + ], sortBy: {}, group: [] } @@ -444,7 +447,7 @@ const queryParts = { 'treatments.id', 'treatments.treatmentId AS treatmentId', 'treatmentTitle', - 'authorityName || ". " || authorityYear || ". " || articleTitle || ". " || journalTitle || ", " || journalYear || ", pp. " || pages || ", vol. " || journalVolume || ", issue " || journalIssue AS context' + "authorityName || '. ' || authorityYear || '. ' || articleTitle || '. ' || journalTitle || ', ' || journalYear || ', pp. ' || pages || ', vol. ' || journalVolume || ', issue ' || journalIssue AS context" ], tables: [ 'treatments', @@ -509,7 +512,7 @@ const queryParts = { 'treatments.id', 'treatments.treatmentId AS treatmentId', 'treatmentTitle', - 'authorityName || ". " || authorityYear || ". " || articleTitle || ". " || journalTitle || ", " || journalYear || ", pp. " || pages || ", vol. " || journalVolume || ", issue " || journalIssue AS context' + "authorityName || '. ' || authorityYear || '. ' || articleTitle || '. ' || journalTitle || ', ' || journalYear || ', pp. ' || pages || ', vol. ' || journalVolume || ', issue ' || journalIssue AS context" ], tables: [ 'treatments', @@ -565,7 +568,7 @@ const queryParts = { 'treatments.id', 'treatments.treatmentId AS treatmentId', 'treatmentTitle', - 'authorityName || ". " || authorityYear || ". " || articleTitle || ". " || journalTitle || ", " || journalYear || ", pp. " || pages || ", vol. " || journalVolume || ", issue " || journalIssue AS context' + "authorityName || '. ' || authorityYear || '. ' || articleTitle || '. ' || journalTitle || ', ' || journalYear || ', pp. ' || pages || ', vol. ' || journalVolume || ', issue ' || journalIssue AS context" ], tables: [ 'treatments', @@ -629,7 +632,7 @@ const queryParts = { 'treatments.id', 'treatments.treatmentId AS treatmentId', 'treatmentTitle', - 'authorityName || ". " || authorityYear || ". " || articleTitle || ". " || journalTitle || ", " || journalYear || ", pp. " || pages || ", vol. " || journalVolume || ", issue " || journalIssue AS context' + "authorityName || '. ' || authorityYear || '. ' || articleTitle || '. ' || journalTitle || ', ' || journalYear || ', pp. ' || pages || ', vol. ' || journalVolume || ', issue ' || journalIssue AS context" ], tables: [ 'treatments', @@ -726,7 +729,7 @@ const queryParts = { 'treatments.id', 'treatments.treatmentId AS treatmentId', 'treatmentTitle', - 'authorityName || ". " || authorityYear || ". " || articleTitle || ". " || journalTitle || ", " || journalYear || ", pp. " || pages || ", vol. " || journalVolume || ", issue " || journalIssue AS context' + "authorityName || '. ' || authorityYear || '. ' || articleTitle || '. ' || journalTitle || ', ' || journalYear || ', pp. ' || pages || ', vol. ' || journalVolume || ', issue ' || journalIssue AS context" ], tables: [ 'treatments', diff --git a/api/v2/lib/zenodeo.js b/api/v2/lib/zenodeo.js index d850b67..17e5bf1 100644 --- a/api/v2/lib/zenodeo.js +++ b/api/v2/lib/zenodeo.js @@ -167,13 +167,16 @@ const getOneRecord = function(queryObject) { // We are done if no records found if (! data['num-of-records']) { + timer = process.hrtime(timer); return Utils.dataForDelivery(timer, data, debug); } // more data from beyond the database if (queryObject.resource === 'treatments') { + if (queryObject.xml && queryObject.xml === 'true') { + data.records[0].xml = getXml(queryObject.treatmentId); } diff --git a/api/v2/utils.js b/api/v2/utils.js index d6ead28..5a6b128 100644 --- a/api/v2/utils.js +++ b/api/v2/utils.js @@ -250,10 +250,21 @@ module.exports = { halify: function({records, uri, resource, id}) { records.forEach(row => { - row._links = this.makeSelfLink({ + // row._links = this.makeLink({ + // uri: uri, + // path: resource.toLowerCase(), + // queryString: `${id}=${row[id]}` + // }) + row._links = {}; + row._links.self = this.makeLink({ uri: uri, - path: resource.toLowerCase(), - queryString: `${id}=${row[id]}` + params: { + path: resource.toLowerCase(), + resourceId: { + key: id, + val: row[id] + } + } }) });