This commit is contained in:
Carlos Jimenez Ruiz 2019-02-19 12:14:27 +01:00
commit ff415dc654
5 changed files with 68 additions and 45 deletions

View File

@ -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,
@ -49,9 +87,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({
@ -62,8 +100,7 @@ module.exports = Self => {
});
}
}
stmt.merge(Self.buildSuffix(filter, 'i'));
stmt.merge(conn.makeSuffix(filter));
return Self.rawStmt(stmt);
};
};

View File

@ -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);

View File

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

View File

@ -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();
}
showItemDescriptor(event, itemFk) {
@ -63,16 +60,8 @@ class Controller {
this.$.workerDescriptor.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();
}
@ -90,8 +79,7 @@ class Controller {
}
preview(event, item) {
event.preventDefault();
event.stopImmediatePropagation();
this.stopEvent(event);
this.itemSelected = item;
this.$.preview.show();
}

View File

@ -40,6 +40,13 @@
field="filter.typeFk">
</vn-autocomplete>
</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-autocomplete
vn-id="tag"
@ -74,13 +81,6 @@
tabindex="-1">
</vn-icon-button>
</vn-horizontal>
<vn-horizontal>
<vn-textfield
vn-one
label="Description"
model="filter.description">
</vn-textfield>
</vn-horizontal>
<vn-horizontal>
<vn-icon-button
vn-bind="+"