Skip to content

Commit a026438

Browse files
update: requireclubmembership middleware for event rsvp
1 parent ef1b5e0 commit a026438

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

internal/handlers/server.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -179,45 +179,45 @@ func (s *Server) setupRoutes() {
179179
protected.GET("/clubs/:id/poll", postHandler.GetPollPostsByClubID)
180180

181181
protected.POST("/clubs/:id/join", clubHandler.JoinClub)
182-
protected.POST("/clubs/:id/leave", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), clubHandler.LeaveClub)
183-
protected.POST("/clubs/:id/ratings", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), clubHandler.RateClub)
182+
protected.POST("/clubs/:id/leave", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), clubHandler.LeaveClub)
183+
protected.POST("/clubs/:id/ratings", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), clubHandler.RateClub)
184184
protected.GET("/my-clubs", clubHandler.GetMyClubs)
185185

186186
protected.PUT("/clubs/:id/members/:user_id", middleware.RequireClubMembershipWithRoles(clubRepo, "club_admin", "moderator"), clubHandler.UpdateClubMember)
187187
protected.GET("/clubs/:id/members/:user_id", clubHandler.GetClubMember)
188188

189189
protected.POST("/clubs/:id/events", middleware.RequireClubMembershipWithRoles(clubRepo, "club_admin", "moderator"), eventHandler.CreateEvent)
190-
protected.GET("/clubs/:id/events", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), eventHandler.GetClubEvents)
191-
protected.GET("/events/:id", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), eventHandler.GetEvent)
190+
protected.GET("/clubs/:id/events", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), eventHandler.GetClubEvents)
191+
protected.GET("/events/:id", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), eventHandler.GetEvent)
192192
protected.PUT("/events/:id", middleware.RequireClubMembershipWithRoles(clubRepo, "club_admin", "moderator"), eventHandler.UpdateEvent)
193193
protected.DELETE("/events/:id", middleware.RequireClubMembershipWithRoles(clubRepo, "club_admin", "moderator"), eventHandler.DeleteEvent)
194194

195-
protected.POST("/events/:id/rsvp", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), eventHandler.RSVPToEvent)
196-
protected.GET("/events/:id/attendees", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), eventHandler.GetEventAttendees)
195+
protected.POST("/events/:id/rsvp", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), eventHandler.RSVPToEvent)
196+
protected.GET("/events/:id/attendees", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), eventHandler.GetEventAttendees)
197197

198198
protected.POST("/books", middleware.RestrictToRoles("admin", "superuser"), bookHandler.CreateBook)
199199
protected.PUT("/books/:id", middleware.RestrictToRoles("admin", "superuser"), bookHandler.UpdateBook)
200200
protected.DELETE("/books/:id", middleware.RestrictToRoles("admin", "superuser"), bookHandler.DeleteBook)
201201

202-
protected.POST("/posts", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), postHandler.CreatePost)
203-
protected.PUT("/posts/:id", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), postHandler.UpdatePost)
204-
protected.DELETE("/posts/:id", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), postHandler.DeletePost)
205-
protected.GET("/posts/reviews", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), postHandler.GetReviewsByBook)
206-
protected.GET("/posts/filter", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), postHandler.GetPostsByType)
202+
protected.POST("/posts", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), postHandler.CreatePost)
203+
protected.PUT("/posts/:id", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), postHandler.UpdatePost)
204+
protected.DELETE("/posts/:id", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), postHandler.DeletePost)
205+
protected.GET("/posts/reviews", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), postHandler.GetReviewsByBook)
206+
protected.GET("/posts/filter", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), postHandler.GetPostsByType)
207207

208-
protected.POST("/posts/:id/vote", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), postHandler.VoteOnPoll)
209-
protected.POST("/posts/:id/unvote", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), postHandler.RemoveVoteFromPoll)
210-
protected.GET("/posts/:id/poll/votes", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), postHandler.GetUserPollVotes)
208+
protected.POST("/posts/:id/vote", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), postHandler.VoteOnPoll)
209+
protected.POST("/posts/:id/unvote", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), postHandler.RemoveVoteFromPoll)
210+
protected.GET("/posts/:id/poll/votes", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), postHandler.GetUserPollVotes)
211211

212-
protected.POST("/posts/:id/like", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), postHandler.LikePost)
213-
protected.POST("/posts/:id/unlike", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), postHandler.UnlikePost)
212+
protected.POST("/posts/:id/like", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), postHandler.LikePost)
213+
protected.POST("/posts/:id/unlike", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), postHandler.UnlikePost)
214214

215-
protected.POST("/posts/:id/comments", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), commentHandler.CreateComment)
216-
protected.PUT("/comments/:id", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), commentHandler.UpdateComment)
217-
protected.DELETE("/comments/:id", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), commentHandler.DeleteComment)
215+
protected.POST("/posts/:id/comments", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), commentHandler.CreateComment)
216+
protected.PUT("/comments/:id", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), commentHandler.UpdateComment)
217+
protected.DELETE("/comments/:id", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), commentHandler.DeleteComment)
218218

219-
protected.POST("/comments/:id/like", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), commentHandler.LikeComment)
220-
protected.POST("/comments/:id/unlike", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo), commentHandler.UnlikeComment)
219+
protected.POST("/comments/:id/like", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), commentHandler.LikeComment)
220+
protected.POST("/comments/:id/unlike", middleware.RequireClubMembership(clubRepo, postRepo, commentRepo, eventRepo), commentHandler.UnlikeComment)
221221

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

internal/middleware/auth.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func RestrictToRoles(allowedRoles ...string) gin.HandlerFunc {
124124
}
125125
}
126126

127-
func RequireClubMembership(clubRepo repository.ClubRepository, postRepo repository.PostRepository, commentRepo repository.CommentRepository) gin.HandlerFunc {
127+
func RequireClubMembership(clubRepo repository.ClubRepository, postRepo repository.PostRepository, commentRepo repository.CommentRepository, eventRepo repository.EventRepository) gin.HandlerFunc {
128128
return func(c *gin.Context) {
129129
// allow admin and superuser to bypass club membership check
130130
userRoleRaw, exists := c.Get("user_role")
@@ -210,7 +210,26 @@ func RequireClubMembership(clubRepo repository.ClubRepository, postRepo reposito
210210
return
211211
}
212212
clubID = uint(clubID64)
213-
} else {
213+
} else if (strings.Contains(path, "/events/:id/rsvp") || strings.Contains(path, "/events/:id/attendees")) && idParam != "" {
214+
// For RSVP/attendees, get event and extract club_id
215+
eventID, parseErr := strconv.ParseUint(idParam, 10, 32)
216+
if parseErr != nil {
217+
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid event ID"})
218+
c.Abort()
219+
return
220+
}
221+
event, err := eventRepo.GetByID(uint(eventID))
222+
if err != nil {
223+
if err == gorm.ErrRecordNotFound {
224+
c.JSON(http.StatusNotFound, gin.H{"error": "event not found"})
225+
} else {
226+
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to fetch event"})
227+
}
228+
c.Abort()
229+
return
230+
}
231+
clubID = event.ClubID
232+
} else {
214233
// Try to get club_id from request body
215234
bodyBytes, readErr := c.GetRawData()
216235
if readErr != nil {

0 commit comments

Comments
 (0)