2020-05-01 20:41:59 +02:00
|
|
|
const errorHandling = require('../assets/utils/errorHandling');
|
|
|
|
const Comments = require('../models/comments');
|
|
|
|
const Users = require('../models/users');
|
|
|
|
const Functions = require('../models/functions');
|
|
|
|
const getPagesHelper = require('../assets/utils/getPagesHelper');
|
|
|
|
const { serverError } = require('../assets/config/errors');
|
2020-04-09 14:31:33 +02:00
|
|
|
|
2020-05-01 20:41:59 +02:00
|
|
|
exports.getCommentsByFunctionId = async (req, res, next) => {
|
2020-04-09 14:31:33 +02:00
|
|
|
const { functionId } = req.params;
|
2020-05-01 20:41:59 +02:00
|
|
|
const options = {
|
2020-04-09 14:31:33 +02:00
|
|
|
where: { functionId },
|
|
|
|
include: [
|
|
|
|
{ model: Users, attributes: ["name", "logo"] }
|
|
|
|
],
|
|
|
|
order: [['createdAt', 'DESC']]
|
2020-05-01 20:41:59 +02:00
|
|
|
};
|
|
|
|
return await getPagesHelper({ req, res, next }, Comments, options);
|
2020-04-09 14:31:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.postCommentsByFunctionId = async (req, res, next) => {
|
|
|
|
const { functionId } = req.params;
|
|
|
|
const { message } = req.body;
|
|
|
|
try {
|
|
|
|
const resultFunction = await Functions.findOne({ where: { id: functionId } });
|
|
|
|
if (!resultFunction) {
|
|
|
|
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
|
|
|
|
}
|
2020-04-10 20:20:07 +02:00
|
|
|
if (!message) {
|
|
|
|
return errorHandling(next, { message: "Vous ne pouvez pas poster de commentaire vide.", statusCode: 400 });
|
|
|
|
}
|
|
|
|
const comment = await Comments.create({ message, userId: req.userId, functionId });
|
|
|
|
return res.status(201).json(comment);
|
2020-04-09 14:31:33 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return errorHandling(next, serverError);
|
|
|
|
}
|
2020-04-10 20:58:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.deleteCommentById = async (req, res, next) => {
|
|
|
|
const { commentId } = req.query;
|
|
|
|
try {
|
|
|
|
const comment = await Comments.findOne({ where: { userId: req.userId, id: parseInt(commentId) } });
|
|
|
|
if (!comment) {
|
|
|
|
return errorHandling(next, { message: "Le commentaire n'existe pas.", statusCode: 404 });
|
|
|
|
}
|
|
|
|
await comment.destroy();
|
|
|
|
return res.status(200).json({ message: "Le commentaire a bien été supprimé." });
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return errorHandling(next, serverError);
|
|
|
|
}
|
2020-04-09 14:31:33 +02:00
|
|
|
}
|