Repository navigation
feat: added admin get/delete/patch apis - #89
Conversation
|
@divyansh-v15-06 remove docs/ dir for now, api naming refactoring is planned so these docs would not make sense after that. And also update these apis in the frontend app/ dir. |
|
done |
ayush00git
left a comment
There was a problem hiding this comment.
No API is binded to the app/ dir.
|
@ayush00git check for the changes now |
|
@divyansh-v15-06 wire the three new apis in the frontend app/ directory. |
e.GET("/api/admin/comments", middleware.IsAuthenticated(), h.AdminGetComments)
e.PATCH("/api/admin/comment/:type/:id/:comment_id", middleware.IsAuthenticated(), h.AdminEditComment)
e.DELETE("/api/admin/comment/:type/:id/:comment_id", middleware.IsAuthenticated(), h.AdminDeleteComment)These APis need to be wired to the frontend |
|
@ayush00git i have done the task in new commit check it |
You've only defined the APIs there's no any wiring up of APIs in the frontend. |
…used imports/variables
|
@ayush00git my bad never pushed the commit |
did you tested all three apis along with frontend ? |
| postIDString := c.Param("id") | ||
| postID, err := strconv.ParseUint(postIDString, 10, 64) | ||
| if err != nil { | ||
| c.JSON(400, gin.H{"error": "invalid post id"}) |
There was a problem hiding this comment.
This is supposed to be a error with status code 500.
postID, err := strconv.ParseUint(postIDString, 10, 64)
if err != nil {
c.JSON(500, gin.H{"error": "failed parsing the post id"})
return
}| commentID, err := strconv.ParseUint(commentIDString, 10, 64) | ||
| if err != nil { | ||
| c.JSON(400, gin.H{"error": "invalid comment id"}) | ||
| return |
|
|
||
| if comment.CommentableType != postType || comment.CommentableID != uint(postID) { | ||
| c.JSON(400, gin.H{"error": "comment does not belong to this post"}) | ||
| return |
There was a problem hiding this comment.
can you explain me the significance of this check ? I think it is redundant.
There was a problem hiding this comment.
can you explain me the significance of this check ? I think it is redundant.
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.
| comment.UpdatedAt = time.Now() | ||
|
|
||
| result = h.DB.Save(&comment) | ||
| if result.Error != nil { |
There was a problem hiding this comment.
Use h.DB.Model(&interface).Updates(comment) for PATCH type requests.
| c.JSON(400, gin.H{"error": "invalid post id"}) | ||
| return | ||
| } | ||
|
|
There was a problem hiding this comment.
Ditto changes for the delete api.
… use Updates for PATCH, and keep delete API aligned
|
@ayush00git did the changes , checked the front end alignment with apis, its good to go |
Description
This PR resolves Issue #41 by implementing the missing API endpoints for editing, deleting, and fetching admin comments. As requested, these endpoints exclusively handle the comment logic and do not interact with or change the status of the posts.
Changes Included:
Added AdminEditComment Handler (PATCH):
Endpoint: PATCH /api/admin/comment/:type/:id/:comment_id
Allows admins to update the text content of their own comments.
Includes authorization checks to verify that the logged-in admin is the author of the comment (matched via email).
Added AdminDeleteComment Handler (DELETE):
Endpoint: DELETE /api/admin/comment/:type/:id/:comment_id
Allows admins to delete their own comments.
Includes the same authorization checks to prevent admins from deleting comments authored by others.
Added AdminGetComments Handler (GET):
Endpoint: GET /api/admin/comments
Fetches all comments authored by the currently logged-in admin across all posts. This resolves the AdminGetComments TODO left in the code.
Routing Updates: Successfully wired up the PATCH, DELETE, and GET endpoints in routes/admin.go and verified they are protected by the IsAuthenticated middleware.
Testing:
Passed the entire existing backend test suite (go test -v ./test/...).
Verified that comment ownership logic correctly restricts access to PATCH and DELETE actions.