📦 NEW: backend PUT /comments

This commit is contained in:
Divlo
2020-05-04 17:19:51 +02:00
parent 6848c790ed
commit ea5ee96845
5 changed files with 86 additions and 66 deletions

View File

@ -37,7 +37,7 @@ exports.postCommentsByFunctionId = async (req, res, next) => {
}
exports.deleteCommentById = async (req, res, next) => {
const { commentId } = req.query;
const { commentId } = req.params;
try {
const comment = await Comments.findOne({ where: { userId: req.userId, id: parseInt(commentId) } });
if (!comment) {
@ -49,4 +49,24 @@ exports.deleteCommentById = async (req, res, next) => {
console.log(error);
return errorHandling(next, serverError);
}
}
exports.putCommentsById = async (req, res, next) => {
const { commentId } = req.params;
const { message } = req.body;
if (!message) {
return errorHandling(next, { message: "Vous ne pouvez pas poster de commentaire vide.", statusCode: 400 });
}
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 });
}
comment.message = message;
await comment.save();
return res.status(200).json({ message: "Le commentaire a bien été modifié." });
} catch (error) {
console.log(error);
return errorHandling(next, serverError);
}
}