This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/api/controllers/functions.js

68 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-03-19 22:59:06 +01:00
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');
const helperQueryNumber = require('../assets/utils/helperQueryNumber');
const getPagesHelper = require('../assets/utils/getPagesHelper');
const Sequelize = require('sequelize');
exports.getFunctions = async (req, res, next) => {
const categoryId = helperQueryNumber(req.query.categoryId, 0);
2020-05-02 15:51:51 +02:00
let { search } = req.query;
try { search = search.toLowerCase(); } catch {};
const options = {
where: {
isOnline: 1,
// Trie par catégorie
... (categoryId !== 0) && { categorieId: categoryId },
// Recherche
... (search != undefined) && {
[Sequelize.Op.or]: [
{ title: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('title')), 'LIKE', `%${search}%`) },
{ slug: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('slug')), 'LIKE', `%${search}%`) },
{ description: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('description')), 'LIKE', `%${search}%`) }
]
}
},
include: [
{ model: Categories, attributes: ["name", "color"] }
],
attributes: {
exclude: ["updatedAt", "utilizationForm", "article", "isOnline"]
},
order: [['createdAt', 'DESC']]
};
return await getPagesHelper({ req, res, next }, Functions, options);
}
2020-03-23 16:50:31 +01:00
exports.getFunctionBySlug = (req, res, next) => {
const { slug } = req.params;
Functions.findOne({
where: { slug, isOnline: 1 },
attributes: {
exclude: ["updatedAt", "isOnline"]
},
include: [
{ model: Categories, attributes: ["name", "color"] }
]
})
.then((result) => {
if (!result) {
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
}
return res.status(200).json(result);
})
.catch((error) => {
console.log(error);
return errorHandling(next, serverError);
2020-03-23 16:50:31 +01:00
});
}
exports.executeFunctionBySlug = (req, res, next) => {
const functionOutput = functionToExecute(req.params.slug);
if (functionOutput !== undefined) {
2020-03-19 22:59:06 +01:00
return functionOutput({ res, next }, req.body);
}
2020-03-19 22:59:06 +01:00
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
}