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

50 lines
906 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"
"github.com/pquerna/ffjson/ffjson"
2021-07-17 14:37:01 +00:00
)
func TotalJokes(c *fiber.Ctx) error {
checkCache, err := core.CheckJokesCache(memory)
if err != nil {
return err
}
2021-07-17 14:37:01 +00:00
if !checkCache {
jokes, err := core.GetAllJSONJokes(db)
if err != nil {
return err
}
err = memory.Set("jokes", jokes)
if err != nil {
return err
}
2021-07-17 14:37:01 +00:00
}
jokes, err := memory.Get("jokes")
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
}
var data []models.Joke
err = ffjson.Unmarshal(jokes, &data)
2021-07-17 14:37:01 +00:00
if err != nil {
return err
}
dataLength := strconv.Itoa(len(data))
return c.Status(fiber.StatusOK).JSON(models.ResponseJoke{
Message: dataLength,
})
}