2021-07-17 14:37:01 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"jokes-bapak2-api/app/v1/core"
|
|
|
|
"jokes-bapak2-api/app/v1/models"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TotalJokes(c *fiber.Ctx) error {
|
2021-07-19 10:21:08 +00:00
|
|
|
checkTotal, err := core.CheckTotalJokesCache(memory)
|
2021-07-18 05:28:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-17 14:37:01 +00:00
|
|
|
|
2021-07-19 10:21:08 +00:00
|
|
|
if !checkTotal {
|
|
|
|
err = core.SetTotalJoke(db, memory)
|
2021-07-18 05:28:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-07-17 14:37:01 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 10:21:08 +00:00
|
|
|
total, err := memory.Get("total")
|
2021-07-19 18:35:04 +00:00
|
|
|
|
2021-07-18 05:28:24 +00:00
|
|
|
if err != nil {
|
|
|
|
if err.Error() == "Entry not found" {
|
|
|
|
return c.Status(fiber.StatusInternalServerError).JSON(models.Error{
|
|
|
|
Error: "no data found",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return err
|
2021-07-17 14:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.Status(fiber.StatusOK).JSON(models.ResponseJoke{
|
2021-07-19 10:21:08 +00:00
|
|
|
Message: strconv.Itoa(int(total[0])),
|
2021-07-17 14:37:01 +00:00
|
|
|
})
|
|
|
|
}
|