Skip to content

Commit 8917bde

Browse files
update post model and repo
1 parent a6fc945 commit 8917bde

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

internal/models/post.go

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,23 +121,35 @@ type ClubSummary struct {
121121
Name string `json:"name"`
122122
}
123123

124+
type LikeSummary struct {
125+
ID uint `json:"id"`
126+
UserID uint `json:"user_id"`
127+
}
128+
129+
type CommentSummary struct {
130+
ID uint `json:"id"`
131+
UserID uint `json:"user_id"`
132+
}
133+
124134
type PostSummary struct {
125-
ID uint `json:"id" gorm:"column:id"`
126-
Title string `json:"title" gorm:"column:title"`
127-
Content string `json:"content" gorm:"column:content"`
128-
Type string `json:"type" gorm:"column:type"`
129-
TypeData interface{} `json:"type_data,omitempty"`
130-
IsPinned bool `json:"is_pinned" gorm:"column:is_pinned"`
131-
LikesCount int `json:"likes_count" gorm:"column:likes_count"`
132-
CommentsCount int `json:"comments_count" gorm:"column:comments_count"`
133-
ViewsCount int `json:"views_count" gorm:"column:views_count"`
134-
HasUserLiked bool `json:"has_user_liked,omitempty" gorm:"-"`
135-
UserID uint `json:"user_id" gorm:"column:post_user_id"`
136-
ClubID *uint `json:"club_id" gorm:"column:post_club_id"`
137-
User UserSummary `json:"user"`
138-
Club *ClubSummary `json:"club,omitempty"`
139-
CreatedAt time.Time `json:"created_at" gorm:"column:created_at"`
140-
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"`
135+
ID uint `json:"id" gorm:"column:id"`
136+
Title string `json:"title" gorm:"column:title"`
137+
Content string `json:"content" gorm:"column:content"`
138+
Type string `json:"type" gorm:"column:type"`
139+
TypeData interface{} `json:"type_data,omitempty"`
140+
IsPinned bool `json:"is_pinned" gorm:"column:is_pinned"`
141+
LikesCount int `json:"likes_count" gorm:"column:likes_count"`
142+
CommentsCount int `json:"comments_count" gorm:"column:comments_count"`
143+
ViewsCount int `json:"views_count" gorm:"column:views_count"`
144+
HasUserLiked bool `json:"has_user_liked,omitempty" gorm:"-"`
145+
UserID uint `json:"user_id" gorm:"column:post_user_id"`
146+
ClubID *uint `json:"club_id" gorm:"column:post_club_id"`
147+
User UserSummary `json:"user"`
148+
Club *ClubSummary `json:"club,omitempty"`
149+
Likes []LikeSummary `json:"likes,omitempty"`
150+
Comments []CommentSummary `json:"comments,omitempty"`
151+
CreatedAt time.Time `json:"created_at" gorm:"column:created_at"`
152+
UpdatedAt time.Time `json:"updated_at" gorm:"column:updated_at"`
141153
}
142154

143155
func (p *Post) GetReviewData() (*ReviewData, error) {

internal/repository/post.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,10 @@ func (r *postRepository) ListPopularPublicPosts(limit int) ([]models.Post, error
219219
Preload("User").
220220
Preload("Club").
221221
Preload("Comments").
222+
Preload("Comments.User").
222223
Preload("Likes").
224+
Preload("Likes.User").
225+
Preload("Likes.Post").
223226
Joins("JOIN clubs ON posts.club_id = clubs.id").
224227
Where("clubs.is_private = ? AND posts.created_at > ?", false, time.Now().AddDate(0, 0, -30)). // Last 30 days
225228
Order("posts.likes_count DESC, posts.comments_count DESC, posts.created_at DESC").

0 commit comments

Comments
 (0)