Repository navigation
feat: added admin get/delete/patch apis #89
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 8 commits
3e3c875
c9a2547
7edf91e
806f966
58b9278
b0b9690
59b8536
8ecaf0f
dbf6cf8
b8333e5
ae406a6
c877998
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 |
|---|---|---|
|
|
@@ -24,6 +24,27 @@ type CommentType struct { | |
| // AdminGetComments | ||
| ///////////////////////////////////////////////////////////////////// | ||
|
|
||
| // AdminGetComments fetches all comments made by the logged-in admin. | ||
| func (h *AdminHandler) AdminGetComments(c *gin.Context) { | ||
| emailID, exists := c.Get(middleware.EmailKey) | ||
| if !exists { | ||
| c.JSON(401, gin.H{"error": "permission denied"}) | ||
| return | ||
| } | ||
|
|
||
| var comments []models.Comment | ||
| result := h.DB.Where("email = ?", emailID).Find(&comments) | ||
| if result.Error != nil { | ||
| c.JSON(500, gin.H{"error": "failed to fetch comments"}) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(200, gin.H{ | ||
| "success": "comments fetched", | ||
| "comments": comments, | ||
| }) | ||
| } | ||
|
|
||
|
|
||
| // AdminPost comment allow any admin comment on any type of post. | ||
| // Common for all type of admins and posts. | ||
|
|
@@ -103,3 +124,123 @@ func (h *AdminHandler) AdminPostComment(c *gin.Context) { | |
|
|
||
| c.JSON(201, gin.H{"success": "comment posted!"}) | ||
| } | ||
|
|
||
| // AdminEditComment allows an admin to edit their own comment. | ||
| func (h *AdminHandler) AdminEditComment(c *gin.Context) { | ||
| // verify the admin | ||
| emailID, exists := c.Get(middleware.EmailKey) | ||
| if !exists { | ||
| c.JSON(401, gin.H{"error": "permission denied"}) | ||
| return | ||
| } | ||
|
|
||
| var inputs CommentType | ||
| if err := c.ShouldBindJSON(&inputs); err != nil { | ||
| c.JSON(400, gin.H{"error": "invalid request body"}) | ||
| return | ||
| } | ||
|
|
||
| postType := c.Param("type") | ||
| postIDString := c.Param("id") | ||
| postID, err := strconv.ParseUint(postIDString, 10, 64) | ||
| if err != nil { | ||
| c.JSON(400, gin.H{"error": "invalid post id"}) | ||
| return | ||
| } | ||
|
|
||
| commentIDString := c.Param("comment_id") | ||
| commentID, err := strconv.ParseUint(commentIDString, 10, 64) | ||
| if err != nil { | ||
| c.JSON(400, gin.H{"error": "invalid comment id"}) | ||
| return | ||
|
Owner
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. 500 here as well |
||
| } | ||
|
|
||
| var comment models.Comment | ||
| result := h.DB.Where("id = ?", commentID).Take(&comment) | ||
| if result.Error != nil { | ||
| if errors.Is(result.Error, gorm.ErrRecordNotFound) { | ||
| c.JSON(404, gin.H{"error": "comment not found"}) | ||
| return | ||
| } | ||
| c.JSON(500, gin.H{"error": "internal server error"}) | ||
| return | ||
| } | ||
|
|
||
| if comment.CommentableType != postType || comment.CommentableID != uint(postID) { | ||
| c.JSON(400, gin.H{"error": "comment does not belong to this post"}) | ||
| return | ||
|
Owner
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. can you explain me the significance of this check ? I think it is redundant.
Collaborator
Author
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.
Since comments are modeled as a polymorphic association (CommentableType and CommentableID), comment IDs are unique within the database table but are detached from a strict hierarchy. The route structure matches RESTful sub-resources: /api/admin/comment/:type/:id/:comment_id. Without verifying that comment.CommentableType == postType and comment.CommentableID == postID, a user could issue a request with a valid comment ID (comment_id) but supply parameters in the URL path (:type and :id) that point to a completely different post. This check ensures consistency between the requested URL route and the actual resource being updated/deleted in the database, preventing mismatched resource references. |
||
| } | ||
|
|
||
| // Verify that the comment belongs to this admin | ||
| if comment.Email != emailID { | ||
| c.JSON(403, gin.H{"error": "you are not authorized to edit this comment"}) | ||
| return | ||
| } | ||
|
|
||
| // Update the comment | ||
| comment.Content = inputs.Content | ||
| comment.UpdatedAt = time.Now() | ||
|
|
||
| result = h.DB.Save(&comment) | ||
| if result.Error != nil { | ||
|
Owner
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. Use |
||
| c.JSON(500, gin.H{"error": "failed to update comment"}) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(200, gin.H{"success": "comment updated!"}) | ||
| } | ||
|
|
||
| // AdminDeleteComment allows an admin to delete their own comment. | ||
| func (h *AdminHandler) AdminDeleteComment(c *gin.Context) { | ||
| // verify the admin | ||
| emailID, exists := c.Get(middleware.EmailKey) | ||
| if !exists { | ||
| c.JSON(401, gin.H{"error": "permission denied"}) | ||
| return | ||
| } | ||
|
|
||
| postType := c.Param("type") | ||
| postIDString := c.Param("id") | ||
| postID, err := strconv.ParseUint(postIDString, 10, 64) | ||
| if err != nil { | ||
| c.JSON(400, gin.H{"error": "invalid post id"}) | ||
| return | ||
| } | ||
|
|
||
|
Owner
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. Ditto changes for the delete api. |
||
| commentIDString := c.Param("comment_id") | ||
| commentID, err := strconv.ParseUint(commentIDString, 10, 64) | ||
| if err != nil { | ||
| c.JSON(400, gin.H{"error": "invalid comment id"}) | ||
| return | ||
| } | ||
|
|
||
| var comment models.Comment | ||
| result := h.DB.Where("id = ?", commentID).Take(&comment) | ||
| if result.Error != nil { | ||
| if errors.Is(result.Error, gorm.ErrRecordNotFound) { | ||
| c.JSON(404, gin.H{"error": "comment not found"}) | ||
| return | ||
| } | ||
| c.JSON(500, gin.H{"error": "internal server error"}) | ||
| return | ||
| } | ||
|
|
||
| if comment.CommentableType != postType || comment.CommentableID != uint(postID) { | ||
| c.JSON(400, gin.H{"error": "comment does not belong to this post"}) | ||
| return | ||
| } | ||
|
|
||
| // Verify that the comment belongs to this admin | ||
| if comment.Email != emailID { | ||
| c.JSON(403, gin.H{"error": "you are not authorized to delete this comment"}) | ||
| return | ||
| } | ||
|
|
||
| result = h.DB.Delete(&comment) | ||
| if result.Error != nil { | ||
| c.JSON(500, gin.H{"error": "failed to delete comment"}) | ||
| return | ||
| } | ||
|
|
||
| c.JSON(200, gin.H{"success": "comment deleted!"}) | ||
| } | ||
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.
This is supposed to be a error with status code 500.