From f374cbf4f31aab2a75deb4e882ffe27497770f7d Mon Sep 17 00:00:00 2001 From: Arakasi21 Date: Tue, 21 May 2024 19:40:23 +0500 Subject: [PATCH] ListsEvents for admin and user with status [IN_PROGRESS] --- internal/handler/event/info.go | 104 +++++++++++++++++++++++++++++++++ internal/handler/handler.go | 6 +- 2 files changed, 108 insertions(+), 2 deletions(-) diff --git a/internal/handler/event/info.go b/internal/handler/event/info.go index 330ca74..cb5bb3e 100644 --- a/internal/handler/event/info.go +++ b/internal/handler/event/info.go @@ -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) { @@ -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}) +} diff --git a/internal/handler/handler.go b/internal/handler/handler.go index d36187f..ca89385 100755 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -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