We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a42913a commit 0ec6db9Copy full SHA for 0ec6db9
internal/models/comment.go
@@ -52,6 +52,20 @@ type CommentResponse struct {
52
UpdatedAt time.Time `json:"updated_at"`
53
}
54
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
69
func (c *Comment) ToResponse() CommentResponse {
70
return CommentResponse{
71
ID: c.ID,
internal/models/post.go
@@ -71,6 +71,20 @@ type PostResponse struct {
72
73
74
+type PostLikeResponse struct {
75
76
77
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
88
func (p *Post) ToResponse() PostResponse {
89
return PostResponse{
90
ID: p.ID,
internal/repository/comment.go
@@ -78,11 +78,9 @@ func (r *commentRepository) CountLikes(commentID uint) (int64, error) {
return count, nil
-func (r *commentRepository) ListCommentLikes(commentID uint) ([]models.CommentLike, error) {
- var likes []models.CommentLike
+func (r *commentRepository) ListCommentLikes(commentID uint) ([]models.CommentLikeResponse, error) {
+ var likes []models.CommentLikeResponse
if err := r.db.
- Preload("User").
- Preload("Comment").
Where("comment_id = ?", commentID).
Find(&likes).Error; err != nil {
return nil, err
internal/repository/interfaces.go
@@ -63,7 +63,7 @@ type PostRepository interface {
ListAll() ([]models.Post, error)
AddLike(like *models.PostLike) error
RemoveLike(userID, postID uint) error
- ListLikesByPostID(postID uint) ([]models.PostLike, error)
+ ListLikesByPostID(postID uint) ([]models.PostLikeResponse, error)
HasUserLiked(userID, postID uint) (bool, error)
CountLikes(postID uint) (int64, error)
UpdateLikesCount(postID uint, count int) error
@@ -78,7 +78,7 @@ type CommentRepository interface {
ListByUserID(userID uint) ([]models.Comment, error)
LikeComment(like *models.CommentLike) error
UnlikeComment(userID, commentID uint) error
- ListCommentLikes(commentID uint) ([]models.CommentLike, error)
+ ListCommentLikes(commentID uint) ([]models.CommentLikeResponse, error)
HasUserLiked(userID, commentID uint) (bool, error)
CountLikes(commentID uint) (int64, error)
UpdateLikesCount(commentID uint, count int) error
internal/repository/post.go
@@ -99,11 +99,9 @@ func (r *postRepository) CountLikes(postID uint) (int64, error) {
99
100
101
102
-func (r *postRepository) ListLikesByPostID(postID uint) ([]models.PostLike, error) {
103
- var likes []models.PostLike
+func (r *postRepository) ListLikesByPostID(postID uint) ([]models.PostLikeResponse, error) {
+ var likes []models.PostLikeResponse
104
105
106
- Preload("Post").
107
Where("post_id = ?", postID).
108
109
internal/services/comment.go
@@ -217,7 +217,7 @@ func (s *CommentService) UnlikeComment(userID, commentID uint) error {
217
return s.commentRepo.UpdateLikesCount(commentID, int(count))
218
219
220
-func (s *CommentService) ListLikesByCommentID(commentID uint) ([]models.CommentLike, error) {
+func (s *CommentService) ListLikesByCommentID(commentID uint) ([]models.CommentLikeResponse, error) {
221
_, err := s.commentRepo.GetByID(commentID)
222
if err != nil {
223
if err == gorm.ErrRecordNotFound {
internal/services/post.go
@@ -265,7 +265,7 @@ func (s *PostService) UnlikePost(userID, postID uint) error {
265
return s.postRepo.UpdateLikesCount(postID, int(count))
266
267
268
-func (s *PostService) ListLikesByPostID(postID uint) ([]models.PostLike, error) {
+func (s *PostService) ListLikesByPostID(postID uint) ([]models.PostLikeResponse, error) {
269
_, err := s.postRepo.GetByID(postID)
270
271
if errors.Is(err, gorm.ErrRecordNotFound) {
0 commit comments