-
Notifications
You must be signed in to change notification settings - Fork 4
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
OT 83 UPDATE COMMENT #128
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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({ | ||
resultUpdate, | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ | ||
msg: `Error while updating comment`, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
module.exports = CommentsControllers; | ||
module.exports = CommentsControllers; |
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; |
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 { | ||
|
@@ -84,6 +84,67 @@ class RoleMiddleware { | |
|
||
return next(); | ||
} | ||
static async isOwnerOfComment(req, res, next) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
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; |
There was a problem hiding this comment.
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