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

44 lines
748 B
Go
Raw Normal View History

package joke
import (
"errors"
2021-10-30 03:24:53 +00:00
core "jokes-bapak2-api/core/joke"
"strconv"
"github.com/allegro/bigcache/v3"
"github.com/gofiber/fiber/v2"
)
func (d *Dependencies) TotalJokes(c *fiber.Ctx) error {
checkTotal, err := core.CheckTotalJokesCache(d.Memory)
if err != nil {
return err
}
if !checkTotal {
err = core.SetTotalJoke(d.DB, c.Context(), d.Memory)
if err != nil {
return err
}
}
total, err := d.Memory.Get("total")
if err != nil {
if errors.Is(err, bigcache.ErrEntryNotFound) {
2021-08-04 10:14:33 +00:00
return c.
Status(fiber.StatusInternalServerError).
JSON(Error{
2021-08-04 10:14:33 +00:00
Error: "no data found",
})
}
return err
}
2021-08-04 10:14:33 +00:00
return c.
Status(fiber.StatusOK).
JSON(ResponseJoke{
2021-08-04 10:14:33 +00:00
Message: strconv.Itoa(int(total[0])),
})
}