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

151 lines
2.9 KiB
Go
Raw Normal View History

2021-07-09 06:11:11 +00:00
package handler
import (
"context"
2021-07-14 18:17:01 +00:00
"io/ioutil"
"strconv"
2021-07-09 06:11:11 +00:00
"time"
"jokes-bapak2-api/app/v1/core"
2021-07-14 18:17:01 +00:00
"jokes-bapak2-api/app/v1/models"
"jokes-bapak2-api/app/v1/utils"
2021-07-09 06:11:11 +00:00
"github.com/gofiber/fiber/v2"
)
func TodayJoke(c *fiber.Ctx) error {
// check from redis if today's joke already exists
// send the joke if exists
// get a new joke if it's not, then send it.
2021-07-14 18:17:01 +00:00
var joke models.Today
err := redis.MGet(context.Background(), "today:link", "today:date", "today:image", "today:contentType").Scan(&joke)
2021-07-09 06:11:11 +00:00
if err != nil {
return err
}
2021-07-14 18:17:01 +00:00
eq, err := utils.IsToday(joke.Date)
2021-07-09 06:11:11 +00:00
if err != nil {
return err
}
if eq {
2021-07-14 18:17:01 +00:00
c.Set("Content-Type", joke.ContentType)
return c.Status(fiber.StatusOK).Send([]byte(joke.Image))
2021-07-09 06:11:11 +00:00
} else {
var link string
2021-07-14 18:17:01 +00:00
err := db.QueryRow(context.Background(), "SELECT link FROM jokesbapak2 ORDER BY random() LIMIT 1").Scan(&link)
if err != nil {
return err
}
response, err := client.Get(link, nil)
if err != nil {
return err
}
data, err := ioutil.ReadAll(response.Body)
2021-07-09 06:11:11 +00:00
if err != nil {
return err
}
2021-07-14 18:17:01 +00:00
2021-07-09 12:13:19 +00:00
now := time.Now().UTC().Format(time.RFC3339)
2021-07-14 18:17:01 +00:00
err = redis.MSet(context.Background(), map[string]interface{}{
"today:link": link,
"today:date": now,
"today:image": string(data),
"today:contentType": response.Header.Get("content-type"),
}).Err()
2021-07-09 06:11:11 +00:00
if err != nil {
return err
}
2021-07-14 18:17:01 +00:00
c.Set("Content-Type", response.Header.Get("content-type"))
return c.Status(fiber.StatusOK).Send(data)
2021-07-09 06:11:11 +00:00
}
}
func SingleJoke(c *fiber.Ctx) error {
checkCache, err := core.CheckJokesCache(memory)
if err != nil {
return err
}
if !checkCache {
jokes, err := core.GetAllJSONJokes(db)
if err != nil {
return err
}
err = memory.Set("jokes", jokes)
if err != nil {
return err
}
}
link, err := core.GetRandomJokeFromCache(memory)
2021-07-14 18:17:01 +00:00
if err != nil {
return err
}
// Get image data
response, err := client.Get(link, nil)
if err != nil {
return err
}
data, err := ioutil.ReadAll(response.Body)
2021-07-09 06:11:11 +00:00
if err != nil {
return err
}
2021-07-14 18:17:01 +00:00
c.Set("Content-Type", response.Header.Get("content-type"))
return c.Status(fiber.StatusOK).Send(data)
2021-07-09 06:11:11 +00:00
}
func JokeByID(c *fiber.Ctx) error {
checkCache, err := core.CheckJokesCache(memory)
if err != nil {
return err
}
if !checkCache {
jokes, err := core.GetAllJSONJokes(db)
if err != nil {
return err
2021-07-14 18:17:01 +00:00
}
err = memory.Set("jokes", jokes)
if err != nil {
return err
}
}
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
2021-07-09 06:11:11 +00:00
return err
}
link, err := core.GetCachedJokeByID(memory, id)
if err != nil {
return err
}
2021-07-09 06:11:11 +00:00
if link == "" {
2021-07-09 12:13:19 +00:00
return c.Status(fiber.StatusNotFound).Send([]byte("Requested ID was not found."))
2021-07-09 06:11:11 +00:00
}
2021-07-14 18:17:01 +00:00
// Get image data
response, err := client.Get(link, nil)
if err != nil {
return err
}
data, err := ioutil.ReadAll(response.Body)
if err != nil {
return err
}
c.Set("Content-Type", response.Header.Get("content-type"))
return c.Status(fiber.StatusOK).Send(data)
2021-07-09 06:11:11 +00:00
}