📦 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) {
@ -50,3 +50,23 @@ exports.deleteCommentById = async (req, res, next) => {
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);
}
}

View File

@ -4,7 +4,10 @@ const isAuth = require('../middlewares/isAuth');
const CommentsRouter = Router();
CommentsRouter.route('/')
CommentsRouter.route('/:commentId')
// Modifier un commentaire
.put(isAuth, commentsController.putCommentsById)
// Supprime un commentaire
.delete(isAuth, commentsController.deleteCommentById);

View File

@ -14,7 +14,7 @@ const CommentCard = forwardRef((props, ref) => {
props.manageComment.setLoadingComments(true);
if (isAuth && user.token != undefined) {
try {
await api.delete(`/comments?commentId=${props.id}`, { headers: { 'Authorization': user.token } });
await api.delete(`/comments/${props.id}`, { headers: { 'Authorization': user.token } });
const newCommentsData = { ...props.manageComment.commentsData };
const commentIndex = newCommentsData.rows.findIndex((value) => value.id === props.id);
newCommentsData.rows.splice(commentIndex, 1);

View File

@ -4,9 +4,6 @@ import ReactMarkdown from 'react-markdown/with-html';
import HeadTag from '../components/HeadTag';
const About = (props) => {
console.log(props);
return (
<Fragment>
<HeadTag