-
Notifications
You must be signed in to change notification settings - Fork 2
Se agrega servicio, controller y ruta, middleware para dueño de Comment #55
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
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "Endpoint-Eliminaci\u00F3n-Comentarios"
Changes from all commits
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,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) | ||
|
Collaborator
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. 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 | ||
| } | ||
| 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
Collaborator
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. Segun lo charlado, este middleware no es necesario, utilizar el que implementó Gabriel |
||
|
|
||
| module.exports = checkCommentOwner; | ||
| 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 | ||
| } |
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.
No dejar console.log() utilizar en el desarrollo, luego remover para hacer el PR.