Skip to content

Commit 0ec6db9

Browse files
add missing post and comment fields in response, harden like responses
1 parent a42913a commit 0ec6db9

File tree

7 files changed

+36
-12
lines changed

7 files changed

+36
-12
lines changed

internal/models/comment.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ type CommentResponse struct {
5252
UpdatedAt time.Time `json:"updated_at"`
5353
}
5454

55+
type CommentLikeResponse struct {
56+
ID uint `json:"id"`
57+
User UserResponse `json:"user"`
58+
CreatedAt time.Time `json:"created_at"`
59+
}
60+
61+
func (cl CommentLike) ToResponse() CommentLikeResponse {
62+
return CommentLikeResponse{
63+
ID: cl.ID,
64+
User: cl.User.ToResponse(),
65+
CreatedAt: cl.CreatedAt,
66+
}
67+
}
68+
5569
func (c *Comment) ToResponse() CommentResponse {
5670
return CommentResponse{
5771
ID: c.ID,

internal/models/post.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ type PostResponse struct {
7171
UpdatedAt time.Time `json:"updated_at"`
7272
}
7373

74+
type PostLikeResponse struct {
75+
ID uint `json:"id"`
76+
User UserResponse `json:"user"`
77+
CreatedAt time.Time `json:"created_at"`
78+
}
79+
80+
func (pl PostLike) ToResponse() PostLikeResponse {
81+
return PostLikeResponse{
82+
ID: pl.ID,
83+
User: pl.User.ToResponse(),
84+
CreatedAt: pl.CreatedAt,
85+
}
86+
}
87+
7488
func (p *Post) ToResponse() PostResponse {
7589
return PostResponse{
7690
ID: p.ID,

internal/repository/comment.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,9 @@ func (r *commentRepository) CountLikes(commentID uint) (int64, error) {
7878
return count, nil
7979
}
8080

81-
func (r *commentRepository) ListCommentLikes(commentID uint) ([]models.CommentLike, error) {
82-
var likes []models.CommentLike
81+
func (r *commentRepository) ListCommentLikes(commentID uint) ([]models.CommentLikeResponse, error) {
82+
var likes []models.CommentLikeResponse
8383
if err := r.db.
84-
Preload("User").
85-
Preload("Comment").
8684
Where("comment_id = ?", commentID).
8785
Find(&likes).Error; err != nil {
8886
return nil, err

internal/repository/interfaces.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type PostRepository interface {
6363
ListAll() ([]models.Post, error)
6464
AddLike(like *models.PostLike) error
6565
RemoveLike(userID, postID uint) error
66-
ListLikesByPostID(postID uint) ([]models.PostLike, error)
66+
ListLikesByPostID(postID uint) ([]models.PostLikeResponse, error)
6767
HasUserLiked(userID, postID uint) (bool, error)
6868
CountLikes(postID uint) (int64, error)
6969
UpdateLikesCount(postID uint, count int) error
@@ -78,7 +78,7 @@ type CommentRepository interface {
7878
ListByUserID(userID uint) ([]models.Comment, error)
7979
LikeComment(like *models.CommentLike) error
8080
UnlikeComment(userID, commentID uint) error
81-
ListCommentLikes(commentID uint) ([]models.CommentLike, error)
81+
ListCommentLikes(commentID uint) ([]models.CommentLikeResponse, error)
8282
HasUserLiked(userID, commentID uint) (bool, error)
8383
CountLikes(commentID uint) (int64, error)
8484
UpdateLikesCount(commentID uint, count int) error

internal/repository/post.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,9 @@ func (r *postRepository) CountLikes(postID uint) (int64, error) {
9999
return count, nil
100100
}
101101

102-
func (r *postRepository) ListLikesByPostID(postID uint) ([]models.PostLike, error) {
103-
var likes []models.PostLike
102+
func (r *postRepository) ListLikesByPostID(postID uint) ([]models.PostLikeResponse, error) {
103+
var likes []models.PostLikeResponse
104104
if err := r.db.
105-
Preload("User").
106-
Preload("Post").
107105
Where("post_id = ?", postID).
108106
Find(&likes).Error; err != nil {
109107
return nil, err

internal/services/comment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func (s *CommentService) UnlikeComment(userID, commentID uint) error {
217217
return s.commentRepo.UpdateLikesCount(commentID, int(count))
218218
}
219219

220-
func (s *CommentService) ListLikesByCommentID(commentID uint) ([]models.CommentLike, error) {
220+
func (s *CommentService) ListLikesByCommentID(commentID uint) ([]models.CommentLikeResponse, error) {
221221
_, err := s.commentRepo.GetByID(commentID)
222222
if err != nil {
223223
if err == gorm.ErrRecordNotFound {

internal/services/post.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func (s *PostService) UnlikePost(userID, postID uint) error {
265265
return s.postRepo.UpdateLikesCount(postID, int(count))
266266
}
267267

268-
func (s *PostService) ListLikesByPostID(postID uint) ([]models.PostLike, error) {
268+
func (s *PostService) ListLikesByPostID(postID uint) ([]models.PostLikeResponse, error) {
269269
_, err := s.postRepo.GetByID(postID)
270270
if err != nil {
271271
if errors.Is(err, gorm.ErrRecordNotFound) {

0 commit comments

Comments
 (0)