Skip to content

Commit

Permalink
ListsEvents for admin and user with status [IN_PROGRESS]
Browse files Browse the repository at this point in the history
  • Loading branch information
Arakasi21 committed May 21, 2024
1 parent e6c59fe commit f374cbf
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
104 changes: 104 additions & 0 deletions internal/handler/event/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ package event
import (
eventv1 "github.com/ARUMANDESU/uniclubs-protos/gen/go/posts/event"
"github.com/ARUMANDESU/university-clubs-backend/internal/domain"
"github.com/ARUMANDESU/university-clubs-backend/internal/handler/utils"
"github.com/ARUMANDESU/university-clubs-backend/pkg/logger"
"github.com/gin-gonic/gin"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"log/slog"
"net/http"
"strings"
)

func (h *Handler) GetEventHandler(c *gin.Context) {
Expand Down Expand Up @@ -46,3 +48,105 @@ func (h *Handler) GetEventHandler(c *gin.Context) {

c.JSON(http.StatusOK, gin.H{"club": domain.EventProtoToDomain(res)})
}

func (h *Handler) ListEventsHandler(c *gin.Context) {
const op = "EventHandler.ListEventHandler"
log := h.log.With(slog.String("op", op))

query := c.Query("query")

page, err := utils.GetIntFromQuery(c, "page")
if err != nil {
log.Warn("failed to get page query parameter", logger.Err(err))
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

pageSize, err := utils.GetIntFromQuery(c, "page_size")
if err != nil {
log.Warn("failed to get page_size query parameter", logger.Err(err))
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
clubID, err := utils.GetIntFromQuery(c, "club_id")
if err != nil && !strings.Contains(err.Error(), "query parameter must be provided") {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

userID, err := utils.GetIntFromQuery(c, "user_id")
if err != nil && !strings.Contains(err.Error(), "query parameter must be provided") {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

Filter := eventv1.EventFilter{
ClubId: int64(clubID),
UserId: int64(userID),
Status: c.Query("status"),
Tags: strings.Split(c.Query("tags"), ","),
}

res, err := h.eventClient.ListEvents(c, &eventv1.ListEventsRequest{
Query: query,
PageNumber: int32(page),
PageSize: int32(pageSize),
Filter: &Filter,
})
if err != nil {
switch {
case status.Code(err) == codes.NotFound:
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": status.Convert(err).Message()})
case status.Code(err) == codes.InvalidArgument:
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": status.Convert(err).Message()})
default:
log.Error("internal", logger.Err(err))
c.AbortWithStatus(http.StatusInternalServerError)
}
return
}

c.JSON(http.StatusOK, gin.H{"events": domain.ProtoToEventArr(res.Events), "metadata": res.Metadata})
}

func (h *Handler) ListPublishedEventsHandler(c *gin.Context) {
const op = "EventHandler.ListPublishedEventsHandler"
log := h.log.With(slog.String("op", op))

query := c.Query("query")

page, err := utils.GetIntFromQuery(c, "page")
if err != nil {
log.Warn("failed to get page query parameter", logger.Err(err))
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

pageSize, err := utils.GetIntFromQuery(c, "page_size")
if err != nil {
log.Warn("failed to get page_size query parameter", logger.Err(err))
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}

res, err := h.eventClient.ListEvents(c, &eventv1.ListEventsRequest{
Query: query,
PageNumber: int32(page),
PageSize: int32(pageSize),
Filter: &eventv1.EventFilter{Status: "IN_PROGRESS"},
})
if err != nil {
switch {
case status.Code(err) == codes.NotFound:
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": status.Convert(err).Message()})
case status.Code(err) == codes.InvalidArgument:
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": status.Convert(err).Message()})
default:
log.Error("internal", logger.Err(err))
c.AbortWithStatus(http.StatusInternalServerError)
}
return
}

c.JSON(http.StatusOK, gin.H{"events": domain.ProtoToEventArr(res.Events), "metadata": res.Metadata})
}
6 changes: 4 additions & 2 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,15 @@ func (h *Handler) InitRoutes() *gin.Engine {
eventPath := router.Group("/events")
{
eventPath.GET("/:id", h.UsrHandler.GetUserIDMiddleware(), h.EventHandler.GetEventHandler)
eventPathAuth := clubPath.Group("")
eventPath.GET("", h.EventHandler.ListPublishedEventsHandler)

eventPathAuth := eventPath.Group("")
{
eventPathAuth.Use(h.UsrHandler.AuthMiddleware())
eventPath.GET("/admin", h.UsrHandler.RoleAuthMiddleware([]userv1.Role{userv1.Role_DSVR, userv1.Role_ADMIN, userv1.Role_MODER}), h.EventHandler.ListEventsHandler)
eventPath.POST("/:id/upload/files", h.EventHandler.UploadFilesHandler)
eventPath.POST("/:id/upload/images", h.EventHandler.UploadImagesHandler)
}

}

return router
Expand Down

0 comments on commit f374cbf

Please sign in to comment.