-
Notifications
You must be signed in to change notification settings - Fork 0
SUMMA-16: CommentService Frontend implementation #125
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
Changes from all commits
f0354e8
a3ffbb9
064bb0a
f40cc68
24b6efb
d894abc
1021ad1
34341e6
e564057
162167f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,118 @@ | ||||||
| import { | ||||||
| createComment, | ||||||
| deleteComment, | ||||||
| deletePost, | ||||||
| getAllComments, | ||||||
| getComment, | ||||||
| updateComment, | ||||||
| } from "../services/comment.service"; | ||||||
| import { ContentService } from "../services/content.service"; | ||||||
| import { Request, Response } from "express"; | ||||||
| import { getUser } from "../../user/services/user.service"; | ||||||
|
|
||||||
| export async function createCommentController( | ||||||
| req: Request, | ||||||
| res: Response | ||||||
| ): Promise<void> { | ||||||
| const { post_id } = req.params; | ||||||
| const { owner_id, text } = req.body; | ||||||
| try { | ||||||
| const response = await getUser(owner_id); | ||||||
| const creation = await createComment( | ||||||
| post_id, | ||||||
| owner_id, | ||||||
| text, | ||||||
| response?.username | ||||||
| ); | ||||||
| res.status(201).json({ message: "Comment created successfully", creation }); | ||||||
| } catch (error) { | ||||||
| console.log(error); | ||||||
| console.log(post_id, owner_id, text); | ||||||
| res.status(500).json({ error: error }); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export async function getCommentByIdController(req: Request, res: Response) { | ||||||
| const { post_id, comment_id } = req.params; | ||||||
| try { | ||||||
| const comment = await getComment(post_id, comment_id); | ||||||
| console.log(comment); | ||||||
| if (comment) res.status(200).json(comment); | ||||||
| else res.status(404).json(null); | ||||||
| } catch (error) { | ||||||
| res.status(500).json({ error: "Failed to fetch comment" }); | ||||||
| console.log(error); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export async function updateCommentController(req: Request, res: Response) { | ||||||
| const { post_id, comment_id, user_id } = req.params; | ||||||
| const comment = await getComment(post_id, comment_id); | ||||||
| if (comment?.owner_id == user_id) { | ||||||
|
Contributor
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. Critical: Use strict equality for authorization checks. Using loose equality ( Apply this fix: - if (comment?.owner_id == user_id) {
+ if (comment?.owner_id === user_id) {- if (comment?.owner_id == user_id) {
+ if (comment?.owner_id === user_id) {Also applies to: 68-68 🤖 Prompt for AI Agents |
||||||
| const updatedComment = req.body; | ||||||
| try { | ||||||
| await updateComment(post_id, comment_id, updatedComment); | ||||||
| res.status(200).json({ message: "Comment updated successfully" }); | ||||||
| } catch (error) { | ||||||
| res.status(500).json({ error: "Failed to update comment" }); | ||||||
| console.log(error); | ||||||
| } | ||||||
| } else { | ||||||
| res.status(401).json({ error: "You do not have permission to try this." }); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export async function deleteCommentController(req: Request, res: Response) { | ||||||
| const { post_id, comment_id, user_id } = req.params; | ||||||
| const comment = await getComment(post_id, comment_id); | ||||||
| if (comment?.owner_id == user_id) { | ||||||
| try { | ||||||
| await deleteComment(post_id, comment_id); | ||||||
| res.status(200).json({ message: "Comment deleted successfully" }); | ||||||
| } catch (error) { | ||||||
| res.status(500).json({ error: error }); | ||||||
| console.log(error); | ||||||
| } | ||||||
| } else { | ||||||
| res.status(401).json({ error: "You do not have permission to try this." }); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export async function deletePostController(req: Request, res: Response) { | ||||||
| const { post_id, user_id } = req.params; | ||||||
| const post = await ContentService.getContent(post_id); | ||||||
| const creator_id = post?.creatorUID; | ||||||
| if (creator_id == user_id) { | ||||||
|
Contributor
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. Critical: Use strict equality for authorization checks. Same security issue with loose equality in the post deletion authorization check. Apply this fix: - if (creator_id == user_id) {
+ if (creator_id === user_id) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| try { | ||||||
| await deletePost(post_id); | ||||||
| res | ||||||
| .status(200) | ||||||
| .json({ message: "Post (entire comment tree) deleted successfully" }); | ||||||
| } catch (error) { | ||||||
| res.status(500).json({ error: error + " " + post_id }); | ||||||
| console.log(error); | ||||||
| } | ||||||
| } else { | ||||||
| res.status(401).json({ error: "You do not have permission to try this." }); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| export async function getCommentsByPostController( | ||||||
| req: Request, | ||||||
| res: Response | ||||||
| ): Promise<void> { | ||||||
| const { post_id } = req.params; | ||||||
| try { | ||||||
| const comments = await getAllComments(post_id); | ||||||
|
|
||||||
| if (Object.keys(comments).length > 0) { | ||||||
| res.status(200).json(comments); | ||||||
| } else { | ||||||
| res.status(200).json(null); | ||||||
| } | ||||||
| } catch (error) { | ||||||
| console.error("Error fetching comments:", error); | ||||||
| res.status(500).json({ error: "Failed to fetch comments" }); | ||||||
| console.log("GET ALL COMMENTS ERROR: ", error); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { Router } from "express"; | ||
| import { | ||
| updateCommentController, | ||
| createCommentController, | ||
| deleteCommentController, | ||
| getCommentsByPostController, | ||
| getCommentByIdController, | ||
| } from "../controllers/comment.controller"; | ||
| import { authenticateToken } from "../../../shared/middleware/auth"; | ||
|
|
||
| const commentRouter = Router(); | ||
|
|
||
| // Post routes | ||
| commentRouter.post( | ||
| "/:contentId/comment", | ||
| authenticateToken, | ||
| createCommentController | ||
| ); | ||
|
|
||
| // Get routes | ||
| commentRouter.get( | ||
| "/:contentId/comments", | ||
| authenticateToken, | ||
| getCommentsByPostController | ||
| ); | ||
| commentRouter.get( | ||
| "/:contentId/comment/:commentId", | ||
| authenticateToken, | ||
| getCommentByIdController | ||
| ); | ||
|
|
||
| // Put routes | ||
| commentRouter.put( | ||
| "/:contentId/comment/:commentId", | ||
| authenticateToken, | ||
| updateCommentController | ||
| ); | ||
|
|
||
| // Delete routes | ||
| commentRouter.delete( | ||
| "/:contentId/comment/:commentId", | ||
| authenticateToken, | ||
| deleteCommentController | ||
| ); | ||
|
|
||
| export default commentRouter; |
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.
🛠️ Refactor suggestion
Remove console.log statements for production.
Multiple console.log statements are present throughout the file. These should be removed or replaced with proper logging for production environments.
Consider using a proper logging library like Winston or implementing a centralized logging service instead of console.log statements.
Also applies to: 44-44, 58-58, 74-74, 93-93, 116-116
🤖 Prompt for AI Agents