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

39 lines
684 B
Go
Raw Normal View History

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)
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)
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")
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
})
}