jokes-bapak2/api/handler/joke/joke_delete.go

52 lines
886 B
Go
Raw Normal View History

package joke
import (
2021-10-30 03:24:53 +00:00
core "jokes-bapak2-api/core/joke"
"jokes-bapak2-api/core/validator"
"strconv"
"github.com/gofiber/fiber/v2"
)
func (d *Dependencies) DeleteJoke(c *fiber.Ctx) error {
id, err := strconv.Atoi(c.Params("id"))
2021-09-27 10:10:19 +00:00
if err != nil {
return err
}
validate, err := validator.JokeIDExists(d.DB, c.Context(), id)
if err != nil {
return err
}
if validate {
return c.
Status(fiber.StatusNotAcceptable).
JSON(Error{
Error: "specified joke id does not exists",
})
}
err = core.DeleteSingleJoke(d.DB, c.Context(), id)
if err != nil {
return err
}
err = core.SetAllJSONJoke(d.DB, c.Context(), d.Memory)
if err != nil {
return err
}
err = core.SetTotalJoke(d.DB, c.Context(), d.Memory)
if err != nil {
return err
}
2021-08-04 10:14:33 +00:00
return c.
Status(fiber.StatusOK).
JSON(ResponseJoke{
Message: "specified joke id has been deleted",
2021-08-04 10:14:33 +00:00
})
}