Skip to content

Commit 7678ba8

Browse files
add has user liked
1 parent 8037d3a commit 7678ba8

File tree

5 files changed

+26
-6
lines changed

5 files changed

+26
-6
lines changed

internal/handlers/post.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,24 @@ func (h *PostHandler) ListPostSummaries(c *gin.Context) {
271271
return
272272
}
273273

274+
useridRaw, exists := c.Get("user_id")
275+
if !exists {
276+
c.JSON(http.StatusUnauthorized, gin.H{"error": "user not authenticated"})
277+
return
278+
}
279+
280+
userID, ok := useridRaw.(uint)
281+
if !ok {
282+
c.JSON(http.StatusInternalServerError, gin.H{"error": "invalid user ID"})
283+
return
284+
}
285+
274286
limitStr := c.DefaultQuery("limit", "20")
275287
offsetStr := c.DefaultQuery("offset", "0")
276288
limit, _ := strconv.Atoi(limitStr)
277289
offset, _ := strconv.Atoi(offsetStr)
278290

279-
posts, err := h.postService.ListPostSummaries(uint(clubID), limit, offset)
291+
posts, err := h.postService.ListPostSummaries(uint(clubID), &userID, limit, offset)
280292
if err != nil {
281293
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
282294
return

internal/models/post.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ type PostSummary struct {
131131
LikesCount int `json:"likes_count" gorm:"column:likes_count"`
132132
CommentsCount int `json:"comments_count" gorm:"column:comments_count"`
133133
ViewsCount int `json:"views_count" gorm:"column:views_count"`
134+
HasUserLiked bool `json:"has_user_liked,omitempty" gorm:"-"`
134135
UserID uint `json:"user_id" gorm:"column:post_user_id"`
135136
ClubID *uint `json:"club_id" gorm:"column:post_club_id"`
136137
User UserSummary `json:"user"`

internal/repository/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ type PostRepository interface {
8080
GetPostsByType(postType string, limit, offset int) ([]models.Post, error)
8181
GetReviewPostsByBookID(bookID uint) ([]models.Post, error)
8282
GetPollPostsByClubID(clubID uint, includeExpired bool) ([]models.Post, error)
83-
ListPostSummaries(clubID uint, limit, offset int) ([]models.PostSummary, error)
83+
ListPostSummaries(clubID uint, userID *uint, limit, offset int) ([]models.PostSummary, error)
8484
}
8585

8686
type CommentRepository interface {

internal/repository/post.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (r *postRepository) ListAll() ([]models.Post, error) {
8181
return posts, nil
8282
}
8383

84-
func (r *postRepository) ListPostSummaries(clubID uint, limit, offset int) ([]models.PostSummary, error) {
84+
func (r *postRepository) ListPostSummaries(clubID uint, userID *uint, limit, offset int) ([]models.PostSummary, error) {
8585
type row struct {
8686
ID uint `gorm:"column:id"`
8787
Title string `gorm:"column:title"`
@@ -130,7 +130,7 @@ func (r *postRepository) ListPostSummaries(clubID uint, limit, offset int) ([]mo
130130
Title: rrow.Title,
131131
Content: rrow.Content,
132132
Type: rrow.Type,
133-
TypeData: []byte(rrow.TypeData),
133+
TypeData: models.PostTypeData(rrow.TypeData),
134134
IsPinned: rrow.IsPinned,
135135
LikesCount: rrow.LikesCount,
136136
CommentsCount: rrow.CommentsCount,
@@ -141,6 +141,13 @@ func (r *postRepository) ListPostSummaries(clubID uint, limit, offset int) ([]mo
141141
UpdatedAt: rrow.UpdatedAt,
142142
}
143143

144+
if userID != nil {
145+
hasLiked, err := r.HasUserLiked(*userID, rrow.ID)
146+
if err == nil {
147+
ps.HasUserLiked = hasLiked
148+
}
149+
}
150+
144151
if rrow.UserID != nil {
145152
ps.User = models.UserSummary{
146153
ID: *rrow.UserID,

internal/services/post.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ func (s *PostService) ListAllPosts() ([]models.PostResponse, error) {
236236
return responses, nil
237237
}
238238

239-
func (s *PostService) ListPostSummaries(clubID uint, limit, offset int) ([]models.PostSummary, error) {
240-
posts, err := s.postRepo.ListPostSummaries(clubID, limit, offset)
239+
func (s *PostService) ListPostSummaries(clubID uint, userID *uint, limit, offset int) ([]models.PostSummary, error) {
240+
posts, err := s.postRepo.ListPostSummaries(clubID, userID, limit, offset)
241241
if err != nil {
242242
return nil, err
243243
}

0 commit comments

Comments
 (0)