refs #6694 feat: move filter remote method to own file

This commit is contained in:
Javier Segarra 2024-01-17 14:15:21 +01:00
parent 30b505aa8f
commit 71f387f626
2 changed files with 35 additions and 32 deletions

View File

@ -0,0 +1,34 @@
module.exports = Self => {
Self.remoteMethod('filter', {
description:
'Find all postcodes of the model matched by postcode, town, province or country.',
accessType: 'READ',
returns: {
type: ['object'],
root: true,
},
http: {
path: `/filter`,
verb: 'GET',
},
});
Self.filter = async(ctx, filter, options) => {
const {Postcode} = Self.app.models;
let {value} = ctx.where;
let limit = ctx.where?.limit ?? 30;
return await Postcode.rawSql(`
SELECT pc.code, t.name as town, p.name as province , c.country
FROM postCode pc
JOIN town t on t.id = pc.townFk
JOIN province p on p.id = t.provinceFk
JOIN country c on c.id = p.countryFk
WHERE
pc.code like '%${value}%'
or t.name like '%${value}%'
or p.name like '%${value}%'
or c.country like '%${value}%'
LIMIT ${limit}
`);
};
};

View File

@ -1,38 +1,7 @@
let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethod('filter', {
description:
'Find all postcodes of the model matched by postcode, town, province or country.',
accessType: 'READ',
returns: {
type: ['object'],
root: true,
},
http: {
path: `/filter`,
verb: 'GET',
},
});
Self.filter = async(ctx, filter, options) => {
const {Postcode} = Self.app.models;
let {value} = ctx.where;
let limit = ctx.where?.limit ?? 30;
return await Postcode.rawSql(`
SELECT pc.code, t.name as town, p.name as province , c.country
FROM postCode pc
JOIN town t on t.id = pc.townFk
JOIN province p on p.id = t.provinceFk
JOIN country c on c.id = p.countryFk
WHERE
pc.code like '%${value}%'
or t.name like '%${value}%'
or p.name like '%${value}%'
or c.country like '%${value}%'
LIMIT ${limit}
`);
};
require('../methods/postcode/filter.js')(Self);
Self.rewriteDbError(function(err) {
if (err.code === 'ER_DUP_ENTRY')
return new UserError(`This postcode already exists`);