refs #5334 filter
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Satorres 2023-05-30 12:08:45 +02:00
parent e26eedb51c
commit e7d25a6cf3
3 changed files with 91 additions and 2 deletions

View File

@ -0,0 +1,90 @@
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.remoteMethodCtx('filter', {
description: 'Find all instances of the model matched by filter from the data source.',
accessType: 'READ',
accepts: [
{
arg: 'filter',
type: 'Object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string',
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: 'id',
type: 'Integer',
description: 'The department id',
http: {source: 'query'}
},
{
arg: 'code',
type: 'String',
description: 'The department code',
http: {source: 'query'}
},
{
arg: 'name',
type: 'String',
description: 'The department name',
http: {source: 'query'}
}
],
returns: {
type: ['Object'],
root: true
},
http: {
path: `/filter`,
verb: 'GET'
}
});
Self.filter = async(ctx, filter) => {
let conn = Self.dataSource.connector;
let where = buildFilter(ctx.args, (param, value) => {
switch (param) {
case 'search':
return /^\d+$/.test(value)
? {'d.id': value}
: {or: [
{'d.code': {like: `%${value}%`}},
{'d.name': {like: `%${value}%`}}
]};
case 'id':
return {'d.id': value};
case 'code':
return {'d.code': value};
case 'name':
return {'d.name': {like: `%${value}%`}};
}
});
filter = mergeFilters(ctx.args.filter, {where});
let stmts = [];
let stmt;
stmt = new ParameterizedSQL(
`SELECT id, code, name
FROM department;`
);
stmt.merge(conn.makeSuffix(filter));
let itemsIndex = stmts.push(stmt) - 1;
let sql = ParameterizedSQL.join(stmts, ';');
let result = await conn.executeStmt(sql);
return itemsIndex === 0 ? result : result[itemsIndex];
};
};

View File

@ -4,5 +4,4 @@ import './summary';
import './card'; import './card';
import './descriptor'; import './descriptor';
import './basic-data'; import './basic-data';
// import './create';
import './search-panel'; import './search-panel';

View File

@ -9,7 +9,7 @@ class Controller extends Component {
const filter = { const filter = {
fields: ['id', 'name', 'code', 'workerFk', 'isProduction', 'chatName', fields: ['id', 'name', 'code', 'workerFk', 'isProduction', 'chatName',
'isTeleworking', 'notificationEmail', 'hasToRefill', 'hasToSendMai', 'hasToMistake', 'clientFk'], 'isTeleworking', 'notificationEmail', 'hasToRefill', 'hasToSendMail', 'hasToMistake', 'clientFk'],
include: [ include: [
{relation: 'client', {relation: 'client',
scope: { scope: {