From e31bf92c990bbf283e7e5bd326b03b02a6c3f7d6 Mon Sep 17 00:00:00 2001 From: Idhibhat Pankam Date: Mon, 8 Jan 2024 10:02:35 +0700 Subject: [PATCH] feat: user handler delete --- src/app/handler/user/user.handler.go | 42 ++++++++++++++++++++++++++-- src/app/router/user.router.go | 11 ++++++-- src/main.go | 3 +- 3 files changed, 51 insertions(+), 5 deletions(-) diff --git a/src/app/handler/user/user.handler.go b/src/app/handler/user/user.handler.go index db905cd..0c3c208 100644 --- a/src/app/handler/user/user.handler.go +++ b/src/app/handler/user/user.handler.go @@ -33,7 +33,7 @@ func NewHandler(service user.Service, validate validator.IDtoValidator) *Handler // @Failure 500 {object} dto.ResponseInternalErr "Internal service error" // @Failure 503 {object} dto.ResponseServiceDownErr "Service is down" // @Router /v1/users/{id} [get] -func (h *Handler) FindOne(c *router.FiberCtx) { +func (h *Handler) FindOne(c router.IContext) { id, err := c.ID() if err != nil { c.JSON(http.StatusBadRequest, dto.ResponseErr{ @@ -70,7 +70,7 @@ func (h *Handler) FindOne(c *router.FiberCtx) { // @Failure 500 {object} dto.ResponseInternalErr "Internal service error" // @Failure 503 {object} dto.ResponseServiceDownErr "Service is down" // @Router /v1/users [put] -func (h *Handler) Update(c *router.FiberCtx) { +func (h *Handler) Update(c router.IContext) { usrId := c.UserID() request := &dto.UpdateUserRequest{} @@ -111,3 +111,41 @@ func (h *Handler) Update(c *router.FiberCtx) { }) return } + +// Delete is a function that deletes user in database +// @Summary deletes user +// @Description Returns successful status if user is successfully deleted +// @Param id path string true "user id" +// @Tags user +// @Accept json +// @Produce json +// @Success 201 {object} bool +// @Success 201 {object} dto.DeleteUserResponse +// @Failure 400 {object} dto.ResponseBadRequestErr "Invalid request body" +// @Failure 500 {object} dto.ResponseInternalErr "Internal service error" +// @Failure 503 {object} dto.ResponseServiceDownErr "Service is down" +// @Router /v1/users/{id} [delete] +func (h *Handler) Delete(c router.IContext) { + id, err := c.ID() + if err != nil { + c.JSON(http.StatusBadRequest, dto.ResponseErr{ + StatusCode: http.StatusBadRequest, + Message: err.Error(), + Data: nil, + }) + return + } + + res, errRes := h.service.Delete(id) + if errRes != nil { + c.JSON(errRes.StatusCode, errRes) + return + } + + c.JSON(http.StatusOK, dto.ResponseSuccess{ + StatusCode: http.StatusOK, + Message: userconst.DeleteUserSuccessMessage, + Data: res, + }) + return +} diff --git a/src/app/router/user.router.go b/src/app/router/user.router.go index f506484..f2da894 100644 --- a/src/app/router/user.router.go +++ b/src/app/router/user.router.go @@ -2,16 +2,23 @@ package router import "github.com/gofiber/fiber/v2" -func (r *FiberRouter) GetUser(path string, h func(ctx *FiberCtx)) { +func (r *FiberRouter) GetUser(path string, h func(ctx IContext)) { r.user.Get(path, func(c *fiber.Ctx) error { h(NewFiberCtx(c)) return nil }) } -func (r *FiberRouter) PutUser(path string, h func(ctx *FiberCtx)) { +func (r *FiberRouter) PutUser(path string, h func(ctx IContext)) { r.user.Put(path, func(c *fiber.Ctx) error { h(NewFiberCtx(c)) return nil }) } + +func (r *FiberRouter) DeleteUser(path string, h func(ctx IContext)) { + r.user.Delete(path, func(c *fiber.Ctx) error { + h(NewFiberCtx(c)) + return nil + }) +} diff --git a/src/main.go b/src/main.go index 2d354c7..c5cc95e 100644 --- a/src/main.go +++ b/src/main.go @@ -103,7 +103,7 @@ func main() { authService := authSvc.NewService(authClient) authHandler := authHdr.NewHandler(authService, userService, v) - authGuard := guard.NewAuthGuard(authService, auth.ExcludePath, conf.App, auth.VersionList) + authGuard := guard.NewAuthGuard(authService, auth.ExcludePath, auth.AdminPath, conf.App, auth.VersionList) imageClient := imageProto.NewImageServiceClient(fileConn) imageService := imageSvc.NewService(imageClient) @@ -120,6 +120,7 @@ func main() { r.GetUser("/:id", userHandler.FindOne) r.PutUser("/", userHandler.Update) + r.DeleteUser("/:id", userHandler.Delete) r.PostAuth("/signup", authHandler.Signup) r.PostAuth("/signin", authHandler.SignIn)