Skip to content

Commit

Permalink
feat: user handler delete
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jan 8, 2024
1 parent 35e2242 commit e31bf92
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
42 changes: 40 additions & 2 deletions src/app/handler/user/user.handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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
}
11 changes: 9 additions & 2 deletions src/app/router/user.router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
3 changes: 2 additions & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit e31bf92

Please sign in to comment.