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

100 lines
2.7 KiB
JavaScript
Raw Permalink Normal View History

2020-08-03 12:04:07 +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')
exports.getCommentsByFunctionId = async (req, res, next) => {
2020-08-03 12:04:07 +02:00
const { functionId } = req.params
const options = {
where: { functionId },
2020-08-03 14:14:45 +02:00
include: [{ model: Users, attributes: ['name', 'logo'] }],
2020-08-03 12:04:07 +02:00
order: [['createdAt', 'DESC']]
}
return await getPagesHelper({ req, res, next }, Comments, options)
}
exports.postCommentsByFunctionId = async (req, res, next) => {
2020-08-03 12:04:07 +02:00
const { functionId } = req.params
const { message } = req.body
try {
2020-08-03 14:14:45 +02:00
const resultFunction = await Functions.findOne({
where: { id: functionId }
})
2020-08-03 12:04:07 +02:00
if (!resultFunction) {
2020-08-03 14:14:45 +02:00
return errorHandling(next, {
message: "La fonction n'existe pas.",
statusCode: 404
})
}
2020-08-03 12:04:07 +02:00
if (!message) {
2020-08-03 14:14:45 +02:00
return errorHandling(next, {
message: 'Vous ne pouvez pas poster de commentaire vide.',
statusCode: 400
})
2020-08-03 12:04:07 +02:00
}
2020-08-03 14:14:45 +02:00
const comment = await Comments.create({
message,
userId: req.userId,
functionId
})
2020-08-03 12:04:07 +02:00
return res.status(201).json(comment)
} catch (error) {
console.log(error)
return errorHandling(next, serverError)
}
}
exports.deleteCommentById = async (req, res, next) => {
2020-08-03 12:04:07 +02:00
const { commentId } = req.params
try {
2020-08-03 14:14:45 +02:00
const comment = await Comments.findOne({
where: { userId: req.userId, id: parseInt(commentId) }
})
2020-08-03 12:04:07 +02:00
if (!comment) {
2020-08-03 14:14:45 +02:00
return errorHandling(next, {
message: "Le commentaire n'existe pas.",
statusCode: 404
})
}
2020-08-03 12:04:07 +02:00
await comment.destroy()
2020-08-03 14:14:45 +02:00
return res
.status(200)
.json({ message: 'Le commentaire a bien été supprimé.' })
2020-08-03 12:04:07 +02:00
} catch (error) {
console.log(error)
return errorHandling(next, serverError)
}
2020-05-04 17:19:51 +02:00
}
exports.putCommentsById = async (req, res, next) => {
2020-08-03 12:04:07 +02:00
const { commentId } = req.params
const { message } = req.body
if (!message) {
2020-08-03 14:14:45 +02:00
return errorHandling(next, {
message: 'Vous ne pouvez pas poster de commentaire vide.',
statusCode: 400
})
2020-08-03 12:04:07 +02:00
}
try {
2020-08-03 14:14:45 +02:00
const comment = await Comments.findOne({
where: { userId: req.userId, id: parseInt(commentId) }
})
2020-08-03 12:04:07 +02:00
if (!comment) {
2020-08-03 14:14:45 +02:00
return errorHandling(next, {
message: "Le commentaire n'existe pas.",
statusCode: 404
})
2020-05-04 17:19:51 +02:00
}
2020-08-03 12:04:07 +02:00
comment.message = message
await comment.save()
2020-08-03 14:14:45 +02:00
return res
.status(200)
.json({ message: 'Le commentaire a bien été modifié.' })
2020-08-03 12:04:07 +02:00
} catch (error) {
console.log(error)
return errorHandling(next, serverError)
}
}