diff --git a/modules/item/back/methods/item/filter.js b/modules/item/back/methods/item/filter.js index 5f7f4f675..694bb777d 100644 --- a/modules/item/back/methods/item/filter.js +++ b/modules/item/back/methods/item/filter.js @@ -1,8 +1,10 @@ const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; +const buildFilter = require('vn-loopback/util/filter').buildFilter; +const mergeFilters = require('vn-loopback/util/filter').mergeFilters; module.exports = Self => { - Self.remoteMethod('filter', { + Self.remoteMethodCtx('filter', { description: 'Find all instances of the model matched by filter from the data source.', accessType: 'READ', accepts: [ @@ -16,6 +18,21 @@ module.exports = Self => { type: ['Object'], description: 'List of tags to filter with', http: {source: 'query'} + }, { + arg: 'search', + type: 'String', + description: `If it's and integer searchs by id, otherwise it searchs by name`, + http: {source: 'query'} + }, { + arg: 'categoryFk', + type: 'Integer', + description: 'Category id', + http: {source: 'query'} + }, { + arg: 'typeFk', + type: 'Integer', + description: 'Type id', + http: {source: 'query'} } ], returns: { @@ -28,7 +45,28 @@ module.exports = Self => { } }); - Self.filter = async(filter, tags) => { + Self.filter = async(ctx, filter, tags) => { + let conn = Self.dataSource.connector; + + let where = buildFilter(ctx.args, (param, value) => { + switch (param) { + case 'search': + return /^\d+$/.test(value) + ? {'i.id': {inq: value}} + : {'i.name': {like: `%${value}%`}}; + case 'id': + return {'i.id': value}; + case 'description': + return {'i.description': {like: `%${value}%`}}; + case 'categoryFk': + return {'ic.id': value}; + case 'typeFk': + return {'t.id': value}; + } + }); + + filter = mergeFilters(ctx.args.filter, {where}); + let stmt = new ParameterizedSQL( `SELECT i.id, i.image, i.name, i.description, i.size, i.tag5, i.value5, i.tag6, i.value6, @@ -48,9 +86,9 @@ module.exports = Self => { LEFT JOIN taxClass tc ON tc.id = i.taxClassFk` ); - if (tags) { + if (ctx.args.tags) { let i = 1; - for (let tag of tags) { + for (let tag of ctx.args.tags) { if (tag.value == null) continue; let tAlias = `it${i++}`; stmt.merge({ @@ -61,8 +99,7 @@ module.exports = Self => { }); } } - - stmt.merge(Self.buildSuffix(filter, 'i')); + stmt.merge(conn.makeSuffix(filter)); return Self.rawStmt(stmt); }; }; diff --git a/modules/item/back/methods/item/specs/filter.spec.js b/modules/item/back/methods/item/specs/filter.spec.js index 3e08e764d..e21031f70 100644 --- a/modules/item/back/methods/item/specs/filter.spec.js +++ b/modules/item/back/methods/item/specs/filter.spec.js @@ -4,11 +4,10 @@ describe('item filter()', () => { it('should return 1 result using filter and tags', async() => { let filter = { order: 'isActive ASC, name', - limit: 8, - where: {and: [{typeFk: 2}]} + limit: 8 }; let tags = [{value: 'Gem2', tagFk: 58}]; - let result = await app.models.Item.filter(filter, tags); + let result = await app.models.Item.filter({args: {filter: filter, typeFk: 2, tags: tags}}); expect(result.length).toEqual(1); expect(result[0].id).toEqual(2); diff --git a/modules/item/front/index/index.html b/modules/item/front/index/index.html index 48ca536ec..c3241da94 100644 --- a/modules/item/front/index/index.html +++ b/modules/item/front/index/index.html @@ -4,22 +4,20 @@ limit="12" order="isActive DESC, name, id" data="items" - auto-load="true"> + auto-load="false">