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/quotes.js

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2020-08-03 12:04:07 +02:00
const errorHandling = require('../assets/utils/errorHandling')
const { serverError, requiredFields } = require('../assets/config/errors')
const Quotes = require('../models/quotes')
const Users = require('../models/users')
const getPagesHelper = require('../assets/utils/getPagesHelper')
exports.getQuotes = async (req, res, next) => {
2020-08-03 12:04:07 +02:00
const options = {
where: {
isValidated: 1
},
2020-08-03 14:14:45 +02:00
include: [{ model: Users, attributes: ['name', 'logo'] }],
2020-08-03 12:04:07 +02:00
attributes: {
exclude: ['isValidated']
},
order: [['createdAt', 'DESC']]
}
return await getPagesHelper({ req, res, next }, Quotes, options)
}
exports.postQuote = (req, res, next) => {
2020-08-03 12:04:07 +02:00
const { quote, author } = req.body
// S'il n'y a pas les champs obligatoire
if (!(quote && author)) {
return errorHandling(next, requiredFields)
}
Quotes.create({ quote, author, userId: req.userId })
2020-08-03 14:14:45 +02:00
.then(_result => {
return res
.status(200)
.json({
message:
"La citation a bien été ajoutée, elle est en attente de confirmation d'un administrateur."
})
2020-08-03 12:04:07 +02:00
})
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)
})
}