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