-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adding comments creation - deletion
- Loading branch information
1 parent
0477088
commit a3faff4
Showing
12 changed files
with
625 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package api | ||
|
||
import ( | ||
"gcstatus/internal/domain" | ||
"gcstatus/internal/errors" | ||
"gcstatus/internal/resources" | ||
"gcstatus/internal/usecases" | ||
"gcstatus/internal/utils" | ||
"gcstatus/pkg/s3" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
type CommentHandler struct { | ||
userService *usecases.UserService | ||
commentService *usecases.CommentService | ||
} | ||
|
||
func NewCommentHandler( | ||
userServuce *usecases.UserService, | ||
commentService *usecases.CommentService, | ||
) *CommentHandler { | ||
return &CommentHandler{ | ||
userService: userServuce, | ||
commentService: commentService, | ||
} | ||
} | ||
|
||
func (h *CommentHandler) Create(c *gin.Context) { | ||
user, err := utils.Auth(c, h.userService.GetUserByID) | ||
if err != nil { | ||
RespondWithError(c, http.StatusUnauthorized, "Unauthorized: "+err.Error()) | ||
return | ||
} | ||
|
||
var request struct { | ||
ParentID *uint `json:"parent_id"` | ||
Comment string `json:"comment" binding:"required"` | ||
CommentableID uint `json:"commentable_id" binding:"required"` | ||
CommentableType string `json:"commentable_type" binding:"required"` | ||
} | ||
|
||
if err := c.ShouldBindJSON(&request); err != nil { | ||
RespondWithError(c, http.StatusUnprocessableEntity, "Invalid request data") | ||
return | ||
} | ||
|
||
commentable := domain.Commentable{ | ||
UserID: user.ID, | ||
Comment: request.Comment, | ||
CommentableID: request.CommentableID, | ||
CommentableType: request.CommentableType, | ||
ParentID: request.ParentID, | ||
} | ||
|
||
comment, err := h.commentService.Create(commentable) | ||
if err != nil { | ||
RespondWithError(c, http.StatusInternalServerError, "Failed to create comment.") | ||
return | ||
} | ||
|
||
transformedComment := resources.TransformCommentable(*comment, s3.GlobalS3Client, user.ID) | ||
|
||
response := resources.Response{ | ||
Data: transformedComment, | ||
} | ||
|
||
c.JSON(http.StatusCreated, response) | ||
} | ||
|
||
func (h *CommentHandler) Delete(c *gin.Context) { | ||
commentIDStr := c.Param("id") | ||
user, err := utils.Auth(c, h.userService.GetUserByID) | ||
if err != nil { | ||
RespondWithError(c, http.StatusUnauthorized, "Unauthorized: "+err.Error()) | ||
return | ||
} | ||
|
||
commentID, err := strconv.ParseUint(commentIDStr, 10, 32) | ||
if err != nil { | ||
RespondWithError(c, http.StatusBadRequest, "Invalid comment ID: "+err.Error()) | ||
return | ||
} | ||
|
||
if err := h.commentService.Delete(uint(commentID), user.ID); err != nil { | ||
if httpErr, ok := err.(*errors.HttpError); ok { | ||
RespondWithError(c, httpErr.Code, httpErr.Error()) | ||
} else { | ||
RespondWithError(c, http.StatusInternalServerError, "Failed to delete comment: "+err.Error()) | ||
} | ||
return | ||
} | ||
|
||
c.JSON(http.StatusOK, gin.H{"message": "Your comment was successfully removed!"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package db | ||
|
||
import ( | ||
"gcstatus/internal/domain" | ||
"gcstatus/internal/ports" | ||
|
||
"gorm.io/gorm" | ||
) | ||
|
||
type CommentRepositoryMySQL struct { | ||
db *gorm.DB | ||
} | ||
|
||
func NewCommentRepositoryMySQL(db *gorm.DB) ports.CommentRepository { | ||
return &CommentRepositoryMySQL{db: db} | ||
} | ||
|
||
func (h *CommentRepositoryMySQL) Create(commentable domain.Commentable) (*domain.Commentable, error) { | ||
if err := h.db.Create(&commentable).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := h.db.Preload("User").Preload("Replies.User").Preload("Hearts").First(&commentable, commentable.ID).Error; err != nil { | ||
return nil, err | ||
} | ||
|
||
return &commentable, nil | ||
} | ||
|
||
func (h *CommentRepositoryMySQL) Delete(id uint) error { | ||
if err := h.db.Delete(&domain.Commentable{}, id).Error; err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (h *CommentRepositoryMySQL) FindByID(id uint) (*domain.Commentable, error) { | ||
var comment domain.Commentable | ||
if err := h.db.First(&comment, id).Error; err != nil { | ||
return nil, err | ||
} | ||
return &comment, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package ports | ||
|
||
import "gcstatus/internal/domain" | ||
|
||
type CommentRepository interface { | ||
FindByID(id uint) (*domain.Commentable, error) | ||
Create(commentable domain.Commentable) (*domain.Commentable, error) | ||
Delete(id uint) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package usecases | ||
|
||
import ( | ||
"gcstatus/internal/domain" | ||
"gcstatus/internal/errors" | ||
"gcstatus/internal/ports" | ||
"net/http" | ||
) | ||
|
||
type CommentService struct { | ||
repo ports.CommentRepository | ||
} | ||
|
||
func NewCommentService(repo ports.CommentRepository) *CommentService { | ||
return &CommentService{repo: repo} | ||
} | ||
|
||
func (h *CommentService) Create(commentable domain.Commentable) (*domain.Commentable, error) { | ||
return h.repo.Create(commentable) | ||
} | ||
|
||
func (h *CommentService) Delete(id uint, userID uint) error { | ||
comment, err := h.repo.FindByID(id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if comment.UserID != userID { | ||
return errors.NewHttpError(http.StatusForbidden, "This comment does not belongs to you user!") | ||
} | ||
|
||
if err := h.repo.Delete(id); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.