2021-08-03 18:14:32 +00:00
|
|
|
package joke
|
|
|
|
|
|
|
|
import (
|
2021-09-26 19:13:38 +00:00
|
|
|
"errors"
|
2021-10-30 03:24:53 +00:00
|
|
|
core "jokes-bapak2-api/core/joke"
|
2021-08-03 18:14:32 +00:00
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
"github.com/allegro/bigcache/v3"
|
2021-08-03 18:14:32 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
func (d *Dependencies) TotalJokes(c *fiber.Ctx) error {
|
|
|
|
checkTotal, err := core.CheckTotalJokesCache(d.Memory)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkTotal {
|
2021-10-18 08:31:17 +00:00
|
|
|
err = core.SetTotalJoke(d.DB, c.Context(), d.Memory)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
total, err := d.Memory.Get("total")
|
2021-08-03 18:14:32 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-09-26 19:13:38 +00:00
|
|
|
if errors.Is(err, bigcache.ErrEntryNotFound) {
|
2021-08-04 10:14:33 +00:00
|
|
|
return c.
|
|
|
|
Status(fiber.StatusInternalServerError).
|
2021-09-26 19:13:38 +00:00
|
|
|
JSON(Error{
|
2021-08-04 10:14:33 +00:00
|
|
|
Error: "no data found",
|
|
|
|
})
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-08-04 10:14:33 +00:00
|
|
|
return c.
|
|
|
|
Status(fiber.StatusOK).
|
2021-09-26 19:13:38 +00:00
|
|
|
JSON(ResponseJoke{
|
2022-05-11 14:30:44 +00:00
|
|
|
Message: string(total),
|
2021-08-04 10:14:33 +00:00
|
|
|
})
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|