Skip to content

Commit 2f2eff1

Browse files
update public endpoints
1 parent 2436400 commit 2f2eff1

File tree

5 files changed

+102
-11
lines changed

5 files changed

+102
-11
lines changed

internal/handlers/post.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,26 @@ func (h *PostHandler) ListAllPosts(c *gin.Context) {
252252
c.JSON(http.StatusOK, gin.H{"posts": posts})
253253
}
254254

255+
func (h *PostHandler) ListPublicPosts(c *gin.Context) {
256+
posts, err := h.postService.ListPublicPosts()
257+
if err != nil {
258+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
259+
return
260+
}
261+
262+
c.JSON(http.StatusOK, gin.H{"posts": posts})
263+
}
264+
265+
func (h *PostHandler) ListPopularPublicPosts(c *gin.Context) {
266+
posts, err := h.postService.ListPopularPublicPosts(20)
267+
if err != nil {
268+
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
269+
return
270+
}
271+
272+
c.JSON(http.StatusOK, gin.H{"posts": posts})
273+
}
274+
255275
// @Summary Like a post
256276
// @Description Like a post by its ID
257277
// @Tags Posts

internal/handlers/server.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,26 @@ func (s *Server) setupRoutes() {
114114
{
115115
api.POST("/auth/register", userHandler.Register)
116116
api.POST("/auth/login", userHandler.Login)
117+
117118
api.GET("/clubs", clubHandler.GetAllClubs)
118119
api.GET("/clubs/:id", clubHandler.GetClub)
120+
api.GET("/clubs/:id/members", clubHandler.ListClubMembers)
119121
api.GET("/clubs/:id/ratings", clubHandler.ListClubRatings)
122+
123+
api.GET("/posts/public", postHandler.ListPublicPosts)
124+
api.GET("/posts/popular", postHandler.ListPopularPublicPosts)
125+
126+
api.GET("/books", bookHandler.ListBooks)
127+
api.GET("/books/:id", bookHandler.GetBookByID)
128+
129+
api.GET("/posts/:id/likes", postHandler.ListLikesByPostID)
130+
api.GET("/posts", postHandler.ListAllPosts)
131+
api.GET("/posts/:id", postHandler.GetPostByID)
132+
133+
api.GET("/posts/:id/comments", commentHandler.ListCommentsByPostID)
134+
api.GET("/users/:id/comments", commentHandler.ListCommentsByUserID)
135+
api.GET("/comments/:id", commentHandler.GetCommentByID)
136+
api.GET("/comments/:id/likes", commentHandler.ListLikesByCommentID)
120137
}
121138

122139
protected := api.Group("/")
@@ -135,8 +152,7 @@ func (s *Server) setupRoutes() {
135152
protected.POST("/clubs/:id/join", clubHandler.JoinClub)
136153
protected.POST("/clubs/:id/leave", middleware.RequireClubMembership(clubRepo), clubHandler.LeaveClub)
137154
protected.POST("/clubs/:id/ratings", middleware.RequireClubMembership(clubRepo), clubHandler.RateClub)
138-
139-
protected.GET("/clubs/:id/members", clubHandler.ListClubMembers)
155+
140156
protected.PUT("/clubs/:id/members/:user_id", middleware.RequireClubMembershipWithRoles(clubRepo, "club_admin", "moderator"), clubHandler.UpdateClubMember)
141157
protected.GET("/clubs/:id/members/:user_id", clubHandler.GetClubMember)
142158

@@ -150,31 +166,22 @@ func (s *Server) setupRoutes() {
150166
protected.GET("/events/:id/attendees", middleware.RequireClubMembership(clubRepo), eventHandler.GetEventAttendees)
151167

152168
protected.POST("/books", middleware.RestrictToRoles("admin", "superuser"), bookHandler.CreateBook)
153-
protected.GET("/books/:id", bookHandler.GetBookByID)
154169
protected.PUT("/books/:id", middleware.RestrictToRoles("admin", "superuser"), bookHandler.UpdateBook)
155170
protected.DELETE("/books/:id", middleware.RestrictToRoles("admin", "superuser"), bookHandler.DeleteBook)
156-
protected.GET("/books", bookHandler.ListBooks)
157171

158172
protected.POST("/posts", middleware.RequireClubMembership(clubRepo), postHandler.CreatePost)
159-
protected.GET("/posts/:id", postHandler.GetPostByID)
160173
protected.PUT("/posts/:id", middleware.RequireClubMembership(clubRepo), postHandler.UpdatePost)
161174
protected.DELETE("/posts/:id", middleware.RequireClubMembership(clubRepo), postHandler.DeletePost)
162-
protected.GET("/posts", postHandler.ListAllPosts)
163175

164176
protected.POST("/posts/:id/like", middleware.RequireClubMembership(clubRepo), postHandler.LikePost)
165177
protected.POST("/posts/:id/unlike", middleware.RequireClubMembership(clubRepo), postHandler.UnlikePost)
166-
protected.GET("/posts/:id/likes", postHandler.ListLikesByPostID)
167178

168179
protected.POST("/posts/:id/comments", middleware.RequireClubMembership(clubRepo), commentHandler.CreateComment)
169-
protected.GET("/comments/:id", commentHandler.GetCommentByID)
170180
protected.PUT("/comments/:id", middleware.RequireClubMembership(clubRepo), commentHandler.UpdateComment)
171181
protected.DELETE("/comments/:id", middleware.RequireClubMembership(clubRepo), commentHandler.DeleteComment)
172-
protected.GET("/posts/:id/comments", commentHandler.ListCommentsByPostID)
173-
protected.GET("/users/:id/comments", commentHandler.ListCommentsByUserID)
174182

175183
protected.POST("/comments/:id/like", middleware.RequireClubMembership(clubRepo), commentHandler.LikeComment)
176184
protected.POST("/comments/:id/unlike", middleware.RequireClubMembership(clubRepo), commentHandler.UnlikeComment)
177-
protected.GET("/comments/:id/likes", commentHandler.ListLikesByCommentID)
178185

179186
protected.POST("/users/:id/reading/sync", middleware.AuthorizeSelf(), readingHandler.SyncUserStats)
180187
protected.POST("/users/:id/reading/start", middleware.AuthorizeSelf(), readingHandler.StartReading)

internal/repository/interfaces.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ type PostRepository interface {
6363
ListByUserID(userID uint) ([]models.Post, error)
6464
ListByClubID(clubID uint) ([]models.Post, error)
6565
ListAll() ([]models.Post, error)
66+
ListPublicPosts() ([]models.Post, error)
67+
ListPopularPublicPosts(limit int) ([]models.Post, error)
6668
AddLike(like *models.PostLike) error
6769
RemoveLike(userID, postID uint) error
6870
ListLikesByPostID(postID uint) ([]models.PostLikeResponse, error)

internal/repository/post.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package repository
22

33
import (
4+
"time"
5+
46
"github.com/nevzattalhaozcan/forgotten/internal/models"
57
"gorm.io/gorm"
68
)
@@ -76,6 +78,40 @@ func (r *postRepository) ListAll() ([]models.Post, error) {
7678
return posts, nil
7779
}
7880

81+
func (r *postRepository) ListPublicPosts() ([]models.Post, error) {
82+
var posts []models.Post
83+
if err := r.db.
84+
Preload("User").
85+
Preload("Club"). // Add this to show which club the post belongs to
86+
Preload("Comments").
87+
Preload("Likes").
88+
Joins("JOIN clubs ON posts.club_id = clubs.id").
89+
Where("clubs.is_private = ?", false). // Fix: false for public clubs
90+
Order("posts.likes_count DESC, posts.created_at DESC"). // Popular first
91+
Limit(20). // Limit for homepage
92+
Find(&posts).Error; err != nil {
93+
return nil, err
94+
}
95+
return posts, nil
96+
}
97+
98+
func (r *postRepository) ListPopularPublicPosts(limit int) ([]models.Post, error) {
99+
var posts []models.Post
100+
if err := r.db.
101+
Preload("User").
102+
Preload("Club").
103+
Preload("Comments").
104+
Preload("Likes").
105+
Joins("JOIN clubs ON posts.club_id = clubs.id").
106+
Where("clubs.is_private = ? AND posts.created_at > ?", false, time.Now().AddDate(0, 0, -30)). // Last 30 days
107+
Order("posts.likes_count DESC, posts.comments_count DESC, posts.created_at DESC").
108+
Limit(limit).
109+
Find(&posts).Error; err != nil {
110+
return nil, err
111+
}
112+
return posts, nil
113+
}
114+
79115
func (r *postRepository) AddLike(like *models.PostLike) error {
80116
return r.db.Create(like).Error
81117
}

internal/services/post.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,32 @@ func (s *PostService) ListAllPosts() ([]models.PostResponse, error) {
182182
return responses, nil
183183
}
184184

185+
func (s *PostService) ListPublicPosts() ([]models.PostResponse, error) {
186+
posts, err := s.postRepo.ListPublicPosts()
187+
if err != nil {
188+
return nil, err
189+
}
190+
191+
var responses []models.PostResponse
192+
for _, post := range posts {
193+
responses = append(responses, post.ToResponse())
194+
}
195+
return responses, nil
196+
}
197+
198+
func (s *PostService) ListPopularPublicPosts(limit int) ([]models.PostResponse, error) {
199+
posts, err := s.postRepo.ListPopularPublicPosts(limit)
200+
if err != nil {
201+
return nil, err
202+
}
203+
204+
var responses []models.PostResponse
205+
for _, post := range posts {
206+
responses = append(responses, post.ToResponse())
207+
}
208+
return responses, nil
209+
}
210+
185211
func (s *PostService) LikePost(userID, postID uint) error {
186212
_, err := s.userRepo.GetByID(userID)
187213
if err != nil {

0 commit comments

Comments
 (0)