From 77b82971114e70ce5ba82d3c26398d487e424e31 Mon Sep 17 00:00:00 2001 From: Hendrik Leppelsack Date: Sun, 23 Apr 2017 12:28:01 +0200 Subject: [PATCH] Initial entity search api (#114) * initial entity search api * remove console.log * limit and offset result --- api/controllers/search.js | 11 +++- api/dsap/search.js | 53 ++++++++++++++++++++ api/swagger/src/definitions/definitions.yaml | 31 ------------ api/swagger/src/paths/search/entities.yaml | 19 ++++--- 4 files changed, 75 insertions(+), 39 deletions(-) create mode 100644 api/dsap/search.js diff --git a/api/controllers/search.js b/api/controllers/search.js index 5e0e2c4..75a5a77 100644 --- a/api/controllers/search.js +++ b/api/controllers/search.js @@ -1,6 +1,7 @@ 'use strict'; var context = require('../../database.js').getContext(); +var search = require('../dsap/search.js')(context); module.exports = { searchEntities: searchEntities, @@ -9,8 +10,14 @@ module.exports = { }; function searchEntities(req, res) { - res.status(501); - res.json('Not implemented!'); + const query = req.swagger.params.q.value; + const limit = req.swagger.params.limit.value; + const offset = req.swagger.params.offset.value; + search.searchEntities(query, limit, offset).then(function(list) { + res.status(200).json(list); + }).catch(function(error) { + res.status(500).send(error); + }); } function searchRelations(req, res) { diff --git a/api/dsap/search.js b/api/dsap/search.js new file mode 100644 index 0000000..06fb6b7 --- /dev/null +++ b/api/dsap/search.js @@ -0,0 +1,53 @@ +'use strict'; +module.exports = function (context) { + const models = context.models; + + return { + searchEntities: function(searchString, limit, offset) { + const artistsSearch = models.artists.findAll({ + where: { + name: { + $like: '%' + searchString + '%' + } + }, + order: [['id', 'DESC']], + limit: limit, + offset: offset + }).then((results) => { + return { artists: results } + }); + + const instrumentsSearch = models.instruments.findAll({ + where: { + name: { + $like: '%' + searchString + '%' + } + }, + order: [['id', 'DESC']], + limit: limit, + offset: offset + }).then((results) => { + return { instruments: results } + }); + + const releasesSearch = models.releases.findAll({ + where: { + title: { + $like: '%' + searchString + '%' + } + }, + order: [['id', 'DESC']], + limit: limit, + offset: offset + }).then((results) => { + return { releases: results } + }); + + return Promise.all([ + artistsSearch, instrumentsSearch, releasesSearch + ]).then(function(result) { + return result; + }); + } + } +}; diff --git a/api/swagger/src/definitions/definitions.yaml b/api/swagger/src/definitions/definitions.yaml index a50f7e9..11db37b 100644 --- a/api/swagger/src/definitions/definitions.yaml +++ b/api/swagger/src/definitions/definitions.yaml @@ -28,34 +28,6 @@ Artist: type: string name: type: string - artist_type: - type: string - dateOfBirth : - type: string - format: date - placeOfBirth : - type: string - dateOfDeath : - type: string - format: date - placeOfDeath : - type: string - nationality: - type: string - tags: - type: array - items: - type: string - pseudonym: - type: array - items: - type: string - source_link: - type: string - wiki_link: - type: string - wiki_pageid: - type: string UpdateArtist: properties: @@ -139,9 +111,6 @@ Work: type: string title: type: string - compositionyear: - type: integer - format: int32 UpdateWork: properties: diff --git a/api/swagger/src/paths/search/entities.yaml b/api/swagger/src/paths/search/entities.yaml index 1732555..c37c435 100644 --- a/api/swagger/src/paths/search/entities.yaml +++ b/api/swagger/src/paths/search/entities.yaml @@ -3,16 +3,23 @@ get: description: Returns entities matching the search criteria operationId: searchEntities parameters: - - name: subjectName + - name: q in: query - description: The name of the subject + description: Query string required: true type: string - - name: relationName + - name: offset in: query - description: The name of the relation - required: true - type: string + description: Return results after a certain point + required: false + type: integer + format: int32 + - name: limit + in: query + description: Only return (size) entries + required: false + type: integer + format: int32 responses: "200": description: Success