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

94 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2020-08-03 12:04:07 +02: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) => {
2020-08-03 12:04:07 +02:00
const categoryId = helperQueryNumber(req.query.categoryId, 0)
let { search } = req.query
2020-08-03 14:14:45 +02:00
try {
search = search.toLowerCase()
} catch {}
2020-08-03 12:04:07 +02:00
const options = {
where: {
isOnline: 1,
// Trie par catégorie
2020-08-03 14:14:45 +02:00
...(categoryId !== 0 && { categorieId: categoryId }),
2020-08-03 12:04:07 +02:00
// Recherche
2020-08-03 14:14:45 +02:00
...(search != null && {
2020-08-03 12:04:07 +02:00
[Sequelize.Op.or]: [
2020-08-03 14:14:45 +02:00
{
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}%`
)
}
2020-08-03 12:04:07 +02:00
]
2020-08-03 14:14:45 +02:00
})
2020-08-03 12:04:07 +02:00
},
2020-08-03 14:14:45 +02:00
include: [{ model: Categories, attributes: ['name', 'color'] }],
2020-08-03 12:04:07 +02:00
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) => {
2020-08-03 12:04:07 +02:00
const { slug } = req.params
Functions.findOne({
where: { slug, isOnline: 1 },
attributes: {
exclude: ['updatedAt', 'isOnline']
},
2020-08-03 14:14:45 +02:00
include: [{ model: Categories, attributes: ['name', 'color'] }]
2020-08-03 12:04:07 +02:00
})
2020-08-03 14:14:45 +02:00
.then(result => {
2020-08-03 12:04:07 +02:00
if (!result) {
2020-08-03 14:14:45 +02:00
return errorHandling(next, {
message: "La fonction n'existe pas.",
statusCode: 404
})
2020-08-03 12:04:07 +02:00
}
2020-08-03 14:14:45 +02:00
try {
result.utilizationForm = JSON.parse(result.utilizationForm)
} catch {}
2020-08-03 12:04:07 +02:00
return res.status(200).json(result)
})
2020-08-03 14:14:45 +02:00
.catch(error => {
2020-08-03 12:04:07 +02:00
console.log(error)
return errorHandling(next, serverError)
2020-03-23 16:50:31 +01:00
})
}
exports.executeFunctionBySlug = (req, res, next) => {
2020-08-03 12:04:07 +02:00
const functionOutput = functionToExecute(req.params.slug)
if (functionOutput !== undefined) {
return functionOutput({ res, next }, req.body)
}
2020-08-03 14:14:45 +02:00
return errorHandling(next, {
message: "La fonction n'existe pas.",
statusCode: 404
})
2020-08-03 12:04:07 +02:00
}