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

44 lines
732 B
Go
Raw Normal View History

package joke
import (
"errors"
"jokes-bapak2-api/app/v1/core"
"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, 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])),
})
}