From 054cf21c1fc4351663f814400132e7441db9ffc9 Mon Sep 17 00:00:00 2001 From: Gerard Date: Tue, 19 Feb 2019 10:15:39 +0100 Subject: [PATCH 1/2] item index fixes --- modules/item/back/methods/item/filter.js | 45 ++++++++++++++++++++-- modules/item/front/index/index.html | 9 ++--- modules/item/front/index/index.js | 36 ++++++----------- modules/item/front/search-panel/index.html | 14 +++---- 4 files changed, 64 insertions(+), 40 deletions(-) diff --git a/modules/item/back/methods/item/filter.js b/modules/item/back/methods/item/filter.js index 5f7f4f675..665a55a4e 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(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, @@ -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/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">
- + @@ -45,6 +43,7 @@ {{::item.id | zeroFill:6}} diff --git a/modules/item/front/index/index.js b/modules/item/front/index/index.js index e2decd07b..5e43c5ba8 100644 --- a/modules/item/front/index/index.js +++ b/modules/item/front/index/index.js @@ -16,19 +16,16 @@ class Controller { }; } - exprBuilder(param, value) { - switch (param) { - case 'search': - return /^\d+$/.test(value) - ? {id: value} - : {name: {like: `%${value}%`}}; - case 'name': - case 'description': - return {[param]: {like: `%${value}%`}}; - case 'id': - case 'typeFk': - return {[param]: value}; - } + stopEvent(event) { + event.preventDefault(); + event.stopImmediatePropagation(); + } + + onSearch(params) { + if (params) + this.$.model.applyFilter(null, params); + else + this.$.model.clear(); } showDescriptor(event, itemFk) { @@ -48,16 +45,8 @@ class Controller { this.$scope.descriptor.show(); } - paramBuilder(param, value) { - switch (param) { - case 'tags': - return {[param]: value}; - } - } - cloneItem(event, item) { - event.preventDefault(); - event.stopImmediatePropagation(); + this.stopEvent(event); this.itemSelected = item; this.$.clone.show(); } @@ -75,8 +64,7 @@ class Controller { } preview(event, item) { - event.preventDefault(); - event.stopImmediatePropagation(); + this.stopEvent(event); this.itemSelected = item; this.$.preview.show(); } diff --git a/modules/item/front/search-panel/index.html b/modules/item/front/search-panel/index.html index 2231deefc..abbb56627 100644 --- a/modules/item/front/search-panel/index.html +++ b/modules/item/front/search-panel/index.html @@ -40,6 +40,13 @@ field="filter.typeFk"> + + + + - - - - Date: Tue, 19 Feb 2019 11:17:08 +0100 Subject: [PATCH 2/2] tests updated --- modules/item/back/methods/item/filter.js | 6 +++--- modules/item/back/methods/item/specs/filter.spec.js | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/modules/item/back/methods/item/filter.js b/modules/item/back/methods/item/filter.js index 665a55a4e..694bb777d 100644 --- a/modules/item/back/methods/item/filter.js +++ b/modules/item/back/methods/item/filter.js @@ -65,7 +65,7 @@ module.exports = Self => { } }); - filter = mergeFilters(filter, {where}); + filter = mergeFilters(ctx.args.filter, {where}); let stmt = new ParameterizedSQL( `SELECT i.id, i.image, i.name, i.description, @@ -86,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({ 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);