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')
|
2020-04-22 12:21:02 +02:00
|
|
|
|
2020-05-01 20:41:59 +02:00
|
|
|
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)
|
2020-04-22 12:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|