Added controller with sql

This commit is contained in:
Jorge Padawan 2021-02-08 08:41:26 +01:00
parent 7408d22b66
commit 1adaeca615
1 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,44 @@
const ParameterizedSQL = require('loopback-connector').ParameterizedSQL;
module.exports = Self => {
Self.remoteMethod('getTravelDaysDuration', {
description: 'Return the total days of travel',
accessType: 'READ',
accepts: {
arg: 'id',
type: 'number',
required: true,
description: 'The travel id',
http: {source: 'path'}
},
returns: {
type: 'number',
root: true
},
http: {
path: `/:id/getTravelDaysDuration`,
verb: 'GET'
}
});
Self.getTravelDaysDuration = async id => {
let stmt;
stmt = new ParameterizedSQL(`
SELECT
ROUND(
AVG(
DATEDIFF(landed , shipped)
)
)
FROM travel
WHERE agencyFk = ?
GROUP BY agencyFK`, [
id
]);
let result = await Self.rawStmt(stmt);
return result;
};
};