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

46 lines
1.6 KiB
JavaScript
Raw Normal View History

const errorHandling = require('../assets/utils/errorHandling');
const { serverError } = require('../assets/config/errors');
const Quotes = require('../models/quotes');
const Users = require('../models/users');
const helperQueryNumber = require('../assets/utils/helperQueryNumber');
exports.getQuotes = (req, res, next) => {
const page = helperQueryNumber(req.query.page, 1);
const limit = helperQueryNumber(req.query.limit, 10);
const offset = (page - 1) * limit;
Quotes.findAndCountAll({
limit,
offset,
where: {
isValidated: 1,
},
include: [
{ model: Users, attributes: ["name", "logo"] }
],
attributes: {
exclude: ["isValidated"]
},
order: [['createdAt', 'DESC']]
})
.then((result) => {
const { count, rows } = result;
const hasMore = (page * limit) < count;
return res.status(200).json({ totalItems: count, hasMore, rows });
})
.catch((error) => {
console.log(error);
return errorHandling(next, serverError);
});
}
exports.postQuote = (req, res, next) => {
const { quote, author } = req.body;
Quotes.create({ quote, author, userId: req.userId })
.then((_result) => {
return res.status(200).json({ message: "La citation a bien été ajoutée, elle est en attente de confirmation d'un administrateur." });
})
.catch((error) => {
console.log(error);
return errorHandling(next, serverError);
});
}