Skip to content

Commit

Permalink
feature:GetById Categories and Carousel (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
RianIhsan authored Dec 10, 2023
2 parents d5be7f9 + 749f369 commit 39263c2
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 0 deletions.
21 changes: 21 additions & 0 deletions module/feature/carousel/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,27 @@ func (h *CarouselHandler) GetAllCarousels() echo.HandlerFunc {
}
}

func (h *CarouselHandler) GetCarouselById() echo.HandlerFunc {
return func(c echo.Context) error {
currentUser := c.Get("CurrentUser").(*entities.UserModels)
if currentUser.Role != "admin" {
return response.SendStatusForbiddenResponse(c, "Tidak diizinkan: Anda tidak memiliki izin")
}

id := c.Param("id")
carouselId, err := strconv.ParseUint(id, 10, 64)
if err != nil {
return response.SendBadRequestResponse(c, "Format ID yang Anda masukkan tidak sesuai")
}
getCarousel, err := h.service.GetCarouselById(carouselId)
if err != nil {
return response.SendStatusInternalServerResponse(c, "Gagal mendapatkan detail carousel: "+err.Error())
}

return response.SendSuccessResponse(c, "Data carousel", getCarousel)
}
}

func (h *CarouselHandler) CreateCarousel() echo.HandlerFunc {
return func(c echo.Context) error {
currentUser := c.Get("CurrentUser").(*entities.UserModels)
Expand Down
1 change: 1 addition & 0 deletions module/feature/carousel/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type ServiceCarouselInterface interface {
}
type HandlerCarouselInterface interface {
GetAllCarousels() echo.HandlerFunc
GetCarouselById() echo.HandlerFunc
CreateCarousel() echo.HandlerFunc
DeleteCarousel() echo.HandlerFunc
UpdateCarousel() echo.HandlerFunc
Expand Down
22 changes: 22 additions & 0 deletions module/feature/category/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ func (h *CategoryHandler) GetAllCategory() echo.HandlerFunc {
}
}

func (h *CategoryHandler) GetCategoryById() echo.HandlerFunc {
return func(c echo.Context) error {
currentUser := c.Get("CurrentUser").(*entities.UserModels)
if currentUser.Role != "admin" {
return response.SendStatusForbiddenResponse(c, "Tidak diizinkan: Anda tidak memiliki izin")
}

id := c.Param("id")
categoryID, err := strconv.ParseUint(id, 10, 64)
if err != nil {
return response.SendBadRequestResponse(c, "Format ID yang Anda masukkan tidak sesuai")
}

getCategory, err := h.service.GetCategoryById(categoryID)
if err != nil {
return response.SendStatusInternalServerResponse(c, "Gagal mendapatkan detail kategori: "+err.Error())
}

return response.SendSuccessResponse(c, "Data kategori", getCategory)
}
}

func (h *CategoryHandler) UpdateCategoryById() echo.HandlerFunc {
return func(c echo.Context) error {
currentUser := c.Get("CurrentUser").(*entities.UserModels)
Expand Down
1 change: 1 addition & 0 deletions module/feature/category/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type ServiceCategoryInterface interface {
type HandlerCategoryInterface interface {
CreateCategory() echo.HandlerFunc
GetAllCategory() echo.HandlerFunc
GetCategoryById() echo.HandlerFunc
UpdateCategoryById() echo.HandlerFunc
DeleteCategoryById() echo.HandlerFunc
}
3 changes: 3 additions & 0 deletions routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func RouteChallenge(e *echo.Echo, h challenge.HandlerChallengeInterface, jwtServ
func RouteCategory(e *echo.Echo, h category.HandlerCategoryInterface, jwtService utils.JWTInterface, userService users.ServiceUserInterface) {
categoriesGroup := e.Group("/api/v1/categories")
categoriesGroup.GET("", h.GetAllCategory(), middlewares.AuthMiddleware(jwtService, userService))
categoriesGroup.GET("/:id", h.GetCategoryById(), middlewares.AuthMiddleware(jwtService, userService))
categoriesGroup.POST("", h.CreateCategory(), middlewares.AuthMiddleware(jwtService, userService))
categoriesGroup.PUT("/:id", h.UpdateCategoryById(), middlewares.AuthMiddleware(jwtService, userService))
categoriesGroup.DELETE("/:id", h.DeleteCategoryById(), middlewares.AuthMiddleware(jwtService, userService))
Expand All @@ -114,9 +115,11 @@ func RouteCategory(e *echo.Echo, h category.HandlerCategoryInterface, jwtService
func RouteCarousel(e *echo.Echo, h carousel.HandlerCarouselInterface, jwtService utils.JWTInterface, userService users.ServiceUserInterface) {
carouselsGroup := e.Group("/api/v1/carousel")
carouselsGroup.GET("", h.GetAllCarousels(), middlewares.AuthMiddleware(jwtService, userService))
carouselsGroup.GET("/:id", h.GetCarouselById(), middlewares.AuthMiddleware(jwtService, userService))
carouselsGroup.POST("", h.CreateCarousel(), middlewares.AuthMiddleware(jwtService, userService))
carouselsGroup.PUT("/:id", h.UpdateCarousel(), middlewares.AuthMiddleware(jwtService, userService))
carouselsGroup.DELETE("/:id", h.DeleteCarousel(), middlewares.AuthMiddleware(jwtService, userService))

}

func RouteAddress(e *echo.Echo, h address.HandlerAddressInterface, jwtService utils.JWTInterface, userService users.ServiceUserInterface) {
Expand Down

0 comments on commit 39263c2

Please sign in to comment.