Skip to content

Commit

Permalink
Merge pull request #164 from PathwayCommons/iss146_not-found
Browse files Browse the repository at this point in the history
Updates to GET endpoint
  • Loading branch information
maxkfranz authored Jul 31, 2024
2 parents 966f58b + 0b6a62b commit 230f449
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
33 changes: 20 additions & 13 deletions src/server/db/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { patches } from './patches';
const NS_FIELD = 'namespace';
const ORG_FIELD = 'organism';

const xref2id = (namespace, id) => `${namespace}:${id}`.toUpperCase();

/**
* @exports db
*/
Expand Down Expand Up @@ -232,7 +234,8 @@ const db = {
// remove the main name from the synonym list (if it exists) for the same reason
_.remove(entry.synonyms, syn => syn.toLowerCase() === entry.name.toLowerCase());

body.push( { index: { _index: INDEX, _id: (entry.namespace + ':' + entry.id).toUpperCase() } } );
const { namespace, id } = entry;
body.push( { index: { _index: INDEX, _id: xref2id( namespace, id ) } } );
body.push( entry );
} );

Expand All @@ -258,7 +261,8 @@ const db = {
let body = [];

updates.forEach( update => {
body.push( { update: { _index: INDEX, _id: (namespace + ':' + update.id).toUpperCase() } } );
const { id } = update;
body.push( { update: { _index: INDEX, _id: xref2id( namespace, id ) } } );
body.push( { doc: update.updates } );
} );

Expand Down Expand Up @@ -358,20 +362,23 @@ const db = {
* @param {string} id The id of entity to search
* @param {string} [namespace=undefined] Namespace to seek the entity e.g. 'uniprot', 'chebi', ...
* @returns {Promise} Promise objects represents the entity with the given id from the given namespace,
* if there is no such entity it represents null.
* if there is no such entity it will reject with err (status (404) and message ("Not Found")).
*/
get: function( id, namespace ){
let client = this.connect();
let body = {};

_.set( body, ['query', 'bool', 'must', 'multi_match', 'fields'], ['id', 'ids'] );
_.set( body, ['query', 'bool', 'must', 'multi_match', 'query'], id );
_.set( body, ['query', 'bool', 'filter', 'term', NS_FIELD], namespace );

return client.search({
body,
index: INDEX
}).then( res => res.hits.hits.map( e => e._source )[0] );
const _id = xref2id( namespace, id );

return client.get({
index: INDEX,
type: '_doc',
id: _id
}).then( res => {
if( res.found ){
return res._source;
} else {
throw new Error('Not Found');
}
});
},
/**
* Retrieve dbXrefs in another database, given a db and one or more ids
Expand Down
9 changes: 7 additions & 2 deletions src/server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ router.get('/search', function(req, res){
* responses:
* 200:
* description: The metadata for the grounding
* 404:
* description: The grounding was not found
*/
router.post('/get', function(req, res){
router.post('/get', function(req, res, next){
const { namespace, id } = req.body;

aggregate.get(namespace, id).then(searchRes => res.json(searchRes));
aggregate
.get(namespace, id)
.then(searchRes => res.json(searchRes))
.catch(next);
});

/**
Expand Down
11 changes: 11 additions & 0 deletions test/aggregate-quality.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,15 @@ describe('Search and Get Aggregate', function(){

}); // SEARCH_OBJECTS

describe('GET', () => {
it('Should not throw when id does exist', function(){
const xref_ncbi_known = { namespace: 'ncbi', id: '1' };
return expect( getEnt( xref_ncbi_known.namespace, xref_ncbi_known.id ) ).not.to.be.rejected;
});
it('Should throw when id does not exist', function(){
const xref_ncbi_unknown = { namespace: 'ncbi', id: '0' };
return expect( getEnt( xref_ncbi_unknown.namespace, xref_ncbi_unknown.id ) ).to.be.rejected;
});
}); // GET

}); // describe

0 comments on commit 230f449

Please sign in to comment.