Skip to content

Commit 0898f28

Browse files
add club rating routs
1 parent 2db4108 commit 0898f28

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

internal/handlers/club.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,4 +438,70 @@ func (h *ClubHandler) GetClubMember(c *gin.Context) {
438438
}
439439

440440
c.JSON(http.StatusOK, gin.H{"member": member})
441+
}
442+
443+
func (h *ClubHandler) RateClub(c *gin.Context) {
444+
clubIDParam := c.Param("id")
445+
clubID64, err := strconv.ParseUint(clubIDParam, 10, 32)
446+
if err != nil {
447+
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid club ID"})
448+
return
449+
}
450+
clubID := uint(clubID64)
451+
452+
userIDRaw, ok := c.Get("user_id")
453+
if !ok {
454+
c.JSON(http.StatusUnauthorized, gin.H{"error": "user not authenticated"})
455+
return
456+
}
457+
userID, ok := userIDRaw.(uint)
458+
if !ok {
459+
c.JSON(http.StatusInternalServerError, gin.H{"error": "invalid user ID"})
460+
return
461+
}
462+
463+
var req models.RateClubRequest
464+
if err := c.ShouldBindJSON(&req); err != nil {
465+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
466+
return
467+
}
468+
469+
if err := h.validator.Struct(&req); err != nil {
470+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
471+
return
472+
}
473+
474+
resp, err := h.clubService.RateClub(userID, clubID, &req)
475+
if err != nil {
476+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
477+
return
478+
}
479+
480+
c.JSON(http.StatusOK, gin.H{
481+
"message": "club rated successfully",
482+
"rating": resp,
483+
})
484+
}
485+
486+
func (h *ClubHandler) ListClubRatings(c *gin.Context) {
487+
clubIDParam := c.Param("id")
488+
clubID64, err := strconv.ParseUint(clubIDParam, 10, 32)
489+
if err != nil {
490+
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid club ID"})
491+
return
492+
}
493+
clubID := uint(clubID64)
494+
495+
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "20"))
496+
offset, _ := strconv.Atoi(c.DefaultQuery("offset", "0"))
497+
498+
ratings, err := h.clubService.ListClubRatings(clubID, limit, offset)
499+
if err != nil {
500+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
501+
return
502+
}
503+
504+
c.JSON(http.StatusOK, gin.H{
505+
"ratings": ratings,
506+
})
441507
}

internal/handlers/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func (s *Server) setupRoutes() {
108108
api.POST("/auth/login", userHandler.Login)
109109
api.GET("/clubs", clubHandler.GetAllClubs)
110110
api.GET("/clubs/:id", clubHandler.GetClub)
111+
api.GET("/clubs/:id/ratings", clubHandler.ListClubRatings)
111112
}
112113

113114
protected := api.Group("/")
@@ -125,6 +126,7 @@ func (s *Server) setupRoutes() {
125126

126127
protected.POST("/clubs/:id/join", clubHandler.JoinClub)
127128
protected.POST("/clubs/:id/leave", clubHandler.LeaveClub)
129+
protected.POST("/clubs/:id/ratings", clubHandler.RateClub)
128130

129131
protected.GET("/clubs/:id/members", clubHandler.ListClubMembers)
130132
protected.PUT("/clubs/:id/members/:user_id", clubHandler.UpdateClubMember)

0 commit comments

Comments
 (0)