Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 49 additions & 9 deletions controllers/comments.controller.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,71 @@
const { request, response} = require('express');
const { createComment } = require('../services/comment');
const { request, response } = require('express');
const { createComment, findId, deleteOne } = require('../services/comment');

const getComments = ( req = request, res = response )=> {
const getComments = (req = request, res = response) => {

res.status(200).json({ msg: 'getComments'});
res.status(200).json({ msg: 'getComments' });

}

const newComment = async(req = request, res = response ) => {
const newComment = async (req = request, res = response) => {

const { post_id, user_id, body } = req.body;

try {
const comment = await createComment({ post_id, user_id, body })
const comment = await createComment({ post_id, user_id, body })

res.json({ error: false, message: 'El comentario se ah creado exitosamente', comment});
res.json({ error: false, message: 'El comentario se ah creado exitosamente', comment });

} catch (error) {

res.status(500).json({ error: true, message: 'Error en el servidor, Comuniquese con el administrador', comment: null});
res.status(500).json({ error: true, message: 'Error en el servidor, Comuniquese con el administrador', comment: null });
}

};

const deleteComment = async (req, res) => {

const id = parseInt(req.params.id)
const user = req.user;

try {

const deletedComment = await deleteOne(user, id);
console.log("DeletedComment: " + deletedComment);

if (deletedComment == null) {
return res.status(400).json({
message: "No se encontro el comentario"
})
}

if (deletedComment != 1) {
return res.status(400).json({
message: deletedComment
})
}

return res.status(200).json({
message: "Deleted",
id: id
})

} catch (error) {

console.log(error)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No dejar console.log() utilizar en el desarrollo, luego remover para hacer el PR.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No dejar console.log() utilizar en el desarrollo, luego remover para hacer el PR.

return res.status(500).json({
error: true,
message: "An error has ocurred"
})

}

}



module.exports = {
getComments,
newComment,
deleteComment
}
30 changes: 30 additions & 0 deletions middleware/checkCommentOwner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const getComment = require('../services/comment').findId;

const checkCommentOwner = async (req, res, next) => {

const id = req.params.id;
const userId = req.user.id;
const userRoleId = req.user.roleId;

const comment = await getComment(id);
const commentUserId = comment.user_id;

// console.log("userId:" + userId);
// console.log("userRoleId:" + userRoleId);

if ((userId === commentUserId) || userRoleId === 1) {

return next();

} else {

return res.status(401).json({
error: true,
message: "Insufficient permissions",
});
}


}
Comment on lines +3 to +28

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Segun lo charlado, este middleware no es necesario, utilizar el que implementó Gabriel


module.exports = checkCommentOwner;
7 changes: 6 additions & 1 deletion routes/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ const { Router } = require('express');
const router = Router();

const verifyToken = require('../middleware/verifyToken');
const checkOwnership = require('../middleware/checkOwnership')
const validatorHandler = require('../middleware/validatorHandler');
const commentsFields = require('../helpers/checkCommentsFields');
const { newComment } = require('../controllers/comments.controller');
const { newComment, deleteComment } = require('../controllers/comments.controller');


router.post('/', verifyToken, validatorHandler(commentsFields), newComment );

router.delete('/:id',verifyToken, deleteComment );






Expand Down
41 changes: 38 additions & 3 deletions services/comment.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
const { Comment:DB } = require('../models')
const { Comment: DB } = require('../models')
const db = require('../models');

const createComment = async( data ) => {
const createComment = async (data) => {

const comment = new DB( data );
const comment = new DB(data);

await comment.save();

return comment;

};

const findId = async (id) => {

const comment = await db.Comment.findByPk(id);

return comment;

}

const deleteOne = async (user, id) => {

const commentUserId = await db.Comment.findByPk(id);

if (commentUserId === null) {
return null;
} else {
console.log("Existe el comentario");
console.log("idComment: " + commentUserId.dataValues.user_id)
console.log("idUser: " + user.roleId)

if (commentUserId.dataValues.user_id === user.id || user.roleId === 1) {

const comment = await db.Comment.destroy({ where: { id } });

return comment;

} else {
return "No tiene permisos para eliminar este comentario";
}
}
}


module.exports = {
createComment,
findId,
deleteOne
}