2021-08-03 18:14:32 +00:00
|
|
|
package joke
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2021-09-27 10:10:19 +00:00
|
|
|
"jokes-bapak2-api/app/core"
|
|
|
|
"jokes-bapak2-api/app/utils"
|
2021-08-03 18:14:32 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
func (d *Dependencies) TodayJoke(c *fiber.Ctx) error {
|
2021-08-03 18:14:32 +00:00
|
|
|
// check from handler.Redis if today's joke already exists
|
|
|
|
// send the joke if exists
|
|
|
|
// get a new joke if it's not, then send it.
|
2021-09-26 19:13:38 +00:00
|
|
|
var joke Today
|
2021-09-27 10:10:19 +00:00
|
|
|
err := d.Redis.MGet(*d.Context, "today:link", "today:date", "today:image", "today:contentType").Scan(&joke)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
eq, err := utils.IsToday(joke.Date)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if eq {
|
|
|
|
c.Set("Content-Type", joke.ContentType)
|
|
|
|
return c.Status(fiber.StatusOK).Send([]byte(joke.Image))
|
2021-09-28 06:34:02 +00:00
|
|
|
}
|
2021-08-03 18:14:32 +00:00
|
|
|
|
2021-09-28 06:34:02 +00:00
|
|
|
conn, err := d.DB.Acquire(*d.Context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
var link string
|
|
|
|
err = conn.QueryRow(*d.Context, "SELECT link FROM jokesbapak2 ORDER BY random() LIMIT 1").Scan(&link)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-03 18:14:32 +00:00
|
|
|
|
2021-09-28 06:34:02 +00:00
|
|
|
response, err := d.HTTP.Get(link, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-03 18:14:32 +00:00
|
|
|
|
2021-09-28 06:34:02 +00:00
|
|
|
data, err := ioutil.ReadAll(response.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-03 18:14:32 +00:00
|
|
|
|
2021-09-28 06:34:02 +00:00
|
|
|
now := time.Now().UTC().Format(time.RFC3339)
|
|
|
|
err = d.Redis.MSet(*d.Context, map[string]interface{}{
|
|
|
|
"today:link": link,
|
|
|
|
"today:date": now,
|
|
|
|
"today:image": string(data),
|
|
|
|
"today:contentType": response.Header.Get("content-type"),
|
|
|
|
}).Err()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 06:34:02 +00:00
|
|
|
c.Set("Content-Type", response.Header.Get("content-type"))
|
|
|
|
return c.Status(fiber.StatusOK).Send(data)
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
func (d *Dependencies) SingleJoke(c *fiber.Ctx) error {
|
|
|
|
checkCache, err := core.CheckJokesCache(d.Memory)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkCache {
|
2021-09-27 10:10:19 +00:00
|
|
|
jokes, err := core.GetAllJSONJokes(d.DB, d.Context)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-26 19:13:38 +00:00
|
|
|
err = d.Memory.Set("jokes", jokes)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
link, err := core.GetRandomJokeFromCache(d.Memory)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get image data
|
2021-09-26 19:13:38 +00:00
|
|
|
response, err := d.HTTP.Get(link, nil)
|
2021-08-03 18:14:32 +00:00
|
|
|
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-09-26 19:13:38 +00:00
|
|
|
func (d *Dependencies) JokeByID(c *fiber.Ctx) error {
|
|
|
|
checkCache, err := core.CheckJokesCache(d.Memory)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !checkCache {
|
2021-09-27 10:10:19 +00:00
|
|
|
jokes, err := core.GetAllJSONJokes(d.DB, d.Context)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-26 19:13:38 +00:00
|
|
|
err = d.Memory.Set("jokes", jokes)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := strconv.Atoi(c.Params("id"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
link, err := core.GetCachedJokeByID(d.Memory, id)
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if link == "" {
|
2021-08-04 10:14:33 +00:00
|
|
|
return c.
|
|
|
|
Status(fiber.StatusNotFound).
|
|
|
|
Send([]byte("Requested ID was not found."))
|
2021-08-03 18:14:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get image data
|
2021-09-26 19:13:38 +00:00
|
|
|
response, err := d.HTTP.Get(link, nil)
|
2021-08-03 18:14:32 +00:00
|
|
|
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)
|
|
|
|
}
|