backend: Ajout de Sequelize, GET /functions

TODO: Trier par catégorie et chercher les fonctions
This commit is contained in:
Divlo
2020-03-20 16:49:45 +01:00
parent 19dfe6ae8d
commit c9f817930c
16 changed files with 400 additions and 11 deletions

View File

@ -0,0 +1,13 @@
const errorHandling = require('../assets/utils/errorHandling');
const Categories = require('../models/categories');
exports.getCategories = (_req, res, next) => {
Categories.findAll()
.then((result) => {
res.status(200).json(result);
})
.catch((error) => {
console.log(error);
errorHandling(next, serverError);
});
}

View File

@ -1,6 +1,39 @@
const errorHandling = require('../assets/utils/errorHandling');
const { serverError } = require('../assets/config/errors');
const Functions = require('../models/functions');
const Categories = require('../models/categories');
const functionToExecute = require('../assets/functions/functionObject');
exports.getFunctions = (req, res, next) => {
// TODO: Trier et chercher par catégories
let page = 1;
let limit = 10;
if (req.query.page && !isNaN(req.query.page)) {
page = parseInt(req.query.page);
}
if (req.query.limit && !isNaN(req.query.limit)) {
limit = parseInt(req.query.limit);
}
const offset = (page - 1) * limit;
Functions.findAndCountAll({
limit,
offset,
where: { isOnline: 1 },
include: [
{ model: Categories, attributes: ["name", "color"] }
]
})
.then((result) => {
const { count, rows } = result;
const hasMore = (page * limit) < count;
res.status(200).json({ totalItems: count, hasMore, rows });
})
.catch((error) => {
console.log(error);
errorHandling(next, serverError);
});
}
exports.executeFunctionName = (req, res, next) => {
const functionOutput = functionToExecute(req.params.functionName);
if (functionOutput !== undefined) {