Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OT 83 UPDATE COMMENT #128

Merged
merged 4 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 17 additions & 2 deletions controllers/commentsController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
const { Comments } = require('../models');
const CommentDao = require('../dao/comment');

class CommentsControllers {
static async modifyComment(req, res) {
try {
const { id } = req.params;
const { body } = req.body;

const resultUpdate = await CommentDao.updateComment({ id }, { body });

return res.status(200).json({
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: Este return lo haría fuera del try

resultUpdate,
});
} catch (error) {
return res.status(500).json({
msg: `Error while updating comment`,
});
}
}
}

module.exports = CommentsControllers;
module.exports = CommentsControllers;
22 changes: 22 additions & 0 deletions dao/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const db = require('../models');
const { Comment } = db;

class CommentDao {
static async updateComment(where, data) {
try {
const commentUpdated = await Comment.update(data, {
where: where,
});

if (commentUpdated[0] < 1) {
return 'Comment not found';
}

return 'Comment updated succesfully';
} catch (error) {
throw new Error('Error Comment not updated');
}
}
}

module.exports = CommentDao;
63 changes: 62 additions & 1 deletion middlewares/verify-role.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { response } = require('express');
const { User, Role } = require('../models');
const { User, Role, Comment } = require('../models');
const Token = require('../helpers/Token');

class RoleMiddleware {
Expand Down Expand Up @@ -84,6 +84,67 @@ class RoleMiddleware {

return next();
}
static async isOwnerOfComment(req, res, next) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: isCommentOwner en vez de isOwnerOfComment

const authToken = req.headers['authorization'];
if (!authToken) {
return res.status(500).json({
msg: 'There is no token in request',
});
}

const idComment = parseInt(req.params.id);
let userDecrypted, user, comment;

try {
userDecrypted = Token.decryptJWT(req, res);
user = await User.findOne({
where: { email: userDecrypted.email },
attributes: ['id', 'firstName'],
include: {
model: Role,
attributes: ['name'],
},
});

if (!user) {
return res.status(400).json({
msg: 'User not valid',
});
}
} catch (error) {
return res.status(500).json({
msg: 'Error while searching USER in db',
});
}

try {
comment = await Comment.findOne({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Esta lógica la podría tener el DAO o el controlador de comentarios, para centralizar lógica y mantener encapsulación.

where: { id: idComment },
attributes: ['user_id'],
});
if (!comment) {
return res.status(404).json({
msg: 'Comment not fount',
});
}
if (user.Role.name === 'Admin') {
return next();
}
if (user.id === comment.user_id) {
return next();
}

return res.status(400).json({
msg: 'User not owner of comment ',
});
} catch (error) {
console.log(error);
return res.status(500).json({
error,
msg: 'Eror while searching Comment in db',
});
}
}
}

module.exports = RoleMiddleware;
12 changes: 12 additions & 0 deletions routes/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express');
const router = express.Router();
const CommentController = require('../controllers/commentsController');
const verifyRole = require('../middlewares/verify-role');

router.put(
'/:id',
verifyRole.isOwnerOfComment,
CommentController.modifyComment
);

module.exports = router;
2 changes: 2 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const slidesRouter = require('./slides');
const backoffice = require('./backoffice');
const testimonialsRouter = require('./testimonials');
const membersRouter = require('./members');
const comments = require('./comment');
/* GET home page. */
router.get('/', function (req, res, next) {
res.render('index', { title: 'Express' });
Expand All @@ -27,4 +28,5 @@ router.use('/slides', slidesRouter);
router.use('/backoffice', backoffice);
router.use('/testimonials', testimonialsRouter);
router.use('/members', membersRouter);
router.use('/comments', comments);
module.exports = router;