#6694 Filter postcode by multiple fields #1939
|
@ -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}
|
||||
`);
|
||||
};
|
||||
};
|
|
@ -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`);
|
||||
|
|
Loading…
Reference in New Issue