Merge branch 'test' of gerard/salix into dev
gitea/salix/dev This commit looks good Details

This commit is contained in:
Gerard Miguel 2019-02-19 10:42:40 +00:00 committed by Gitea
commit 12337d6bcf
5 changed files with 68 additions and 45 deletions

View File

@ -1,8 +1,10 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; 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 => { module.exports = Self => {
Self.remoteMethod('filter', { Self.remoteMethodCtx('filter', {
description: 'Find all instances of the model matched by filter from the data source.', description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ', accessType: 'READ',
accepts: [ accepts: [
@ -16,6 +18,21 @@ module.exports = Self => {
type: ['Object'], type: ['Object'],
description: 'List of tags to filter with', description: 'List of tags to filter with',
http: {source: 'query'} 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: { 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( let stmt = new ParameterizedSQL(
`SELECT i.id, i.image, i.name, i.description, `SELECT i.id, i.image, i.name, i.description,
i.size, i.tag5, i.value5, i.tag6, i.value6, 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` LEFT JOIN taxClass tc ON tc.id = i.taxClassFk`
); );
if (tags) { if (ctx.args.tags) {
let i = 1; let i = 1;
for (let tag of tags) { for (let tag of ctx.args.tags) {
if (tag.value == null) continue; if (tag.value == null) continue;
let tAlias = `it${i++}`; let tAlias = `it${i++}`;
stmt.merge({ stmt.merge({
@ -61,8 +99,7 @@ module.exports = Self => {
}); });
} }
} }
stmt.merge(conn.makeSuffix(filter));
stmt.merge(Self.buildSuffix(filter, 'i'));
return Self.rawStmt(stmt); return Self.rawStmt(stmt);
}; };
}; };

View File

@ -4,11 +4,10 @@ describe('item filter()', () => {
it('should return 1 result using filter and tags', async() => { it('should return 1 result using filter and tags', async() => {
let filter = { let filter = {
order: 'isActive ASC, name', order: 'isActive ASC, name',
limit: 8, limit: 8
where: {and: [{typeFk: 2}]}
}; };
let tags = [{value: 'Gem2', tagFk: 58}]; 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.length).toEqual(1);
expect(result[0].id).toEqual(2); expect(result[0].id).toEqual(2);

View File

@ -4,22 +4,20 @@
limit="12" limit="12"
order="isActive DESC, name, id" order="isActive DESC, name, id"
data="items" data="items"
auto-load="true"> auto-load="false">
</vn-crud-model> </vn-crud-model>
<div class="content-block"> <div class="content-block">
<div class="vn-list"> <div class="vn-list">
<vn-card pad-medium-h> <vn-card pad-medium-h>
<vn-searchbar <vn-searchbar
panel="vn-item-search-panel" panel="vn-item-search-panel"
model="model" on-search="$ctrl.onSearch($params)"
expr-builder="$ctrl.exprBuilder(param, value)"
param-builder="$ctrl.paramBuilder(param, value)"
vn-focus> vn-focus>
</vn-searchbar> </vn-searchbar>
</vn-card> </vn-card>
</div> </div>
<vn-card margin-medium-v> <vn-card margin-medium-v>
<vn-table model="model" show-fields="$ctrl.showFields" vn-uvc="itemIndex"> <vn-table model="model" auto-load="false" show-fields="$ctrl.showFields" vn-uvc="itemIndex">
<vn-thead> <vn-thead>
<vn-tr> <vn-tr>
<vn-th th-id="picture"></vn-th> <vn-th th-id="picture"></vn-th>
@ -45,6 +43,7 @@
<img <img
ng-src="{{::$ctrl.imagesPath}}/50x50/{{::item.image}}" ng-src="{{::$ctrl.imagesPath}}/50x50/{{::item.image}}"
zoom-image="{{::$ctrl.imagesPath}}/1600x900/{{::item.image}}" zoom-image="{{::$ctrl.imagesPath}}/1600x900/{{::item.image}}"
ng-click="$ctrl.stopEvent($event)"
on-error-src/> on-error-src/>
</vn-td> </vn-td>
<vn-td number>{{::item.id | zeroFill:6}}</vn-td> <vn-td number>{{::item.id | zeroFill:6}}</vn-td>

View File

@ -16,19 +16,16 @@ class Controller {
}; };
} }
exprBuilder(param, value) { stopEvent(event) {
switch (param) { event.preventDefault();
case 'search': event.stopImmediatePropagation();
return /^\d+$/.test(value) }
? {id: value}
: {name: {like: `%${value}%`}}; onSearch(params) {
case 'name': if (params)
case 'description': this.$.model.applyFilter(null, params);
return {[param]: {like: `%${value}%`}}; else
case 'id': this.$.model.clear();
case 'typeFk':
return {[param]: value};
}
} }
showDescriptor(event, itemFk) { showDescriptor(event, itemFk) {
@ -48,16 +45,8 @@ class Controller {
this.$scope.descriptor.show(); this.$scope.descriptor.show();
} }
paramBuilder(param, value) {
switch (param) {
case 'tags':
return {[param]: value};
}
}
cloneItem(event, item) { cloneItem(event, item) {
event.preventDefault(); this.stopEvent(event);
event.stopImmediatePropagation();
this.itemSelected = item; this.itemSelected = item;
this.$.clone.show(); this.$.clone.show();
} }
@ -75,8 +64,7 @@ class Controller {
} }
preview(event, item) { preview(event, item) {
event.preventDefault(); this.stopEvent(event);
event.stopImmediatePropagation();
this.itemSelected = item; this.itemSelected = item;
this.$.preview.show(); this.$.preview.show();
} }

View File

@ -40,6 +40,13 @@
field="filter.typeFk"> field="filter.typeFk">
</vn-autocomplete> </vn-autocomplete>
</vn-horizontal> </vn-horizontal>
<vn-horizontal>
<vn-textfield
vn-one
label="Description"
model="filter.description">
</vn-textfield>
</vn-horizontal>
<vn-horizontal ng-repeat="itemTag in filter.tags"> <vn-horizontal ng-repeat="itemTag in filter.tags">
<vn-autocomplete <vn-autocomplete
vn-id="tag" vn-id="tag"
@ -74,13 +81,6 @@
tabindex="-1"> tabindex="-1">
</vn-icon-button> </vn-icon-button>
</vn-horizontal> </vn-horizontal>
<vn-horizontal>
<vn-textfield
vn-one
label="Description"
model="filter.description">
</vn-textfield>
</vn-horizontal>
<vn-horizontal> <vn-horizontal>
<vn-icon-button <vn-icon-button
vn-bind="+" vn-bind="+"