2021-10-18 08:31:17 +00:00
|
|
|
package joke
|
2021-07-17 09:06:33 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-09-26 19:13:38 +00:00
|
|
|
"errors"
|
2021-10-30 03:24:53 +00:00
|
|
|
"jokes-bapak2-api/core/schema"
|
2021-07-17 09:06:33 +00:00
|
|
|
"math/rand"
|
2021-10-18 08:31:17 +00:00
|
|
|
"strconv"
|
2021-07-17 09:06:33 +00:00
|
|
|
|
2021-10-18 08:31:17 +00:00
|
|
|
"github.com/Masterminds/squirrel"
|
2021-07-18 05:28:24 +00:00
|
|
|
"github.com/allegro/bigcache/v3"
|
2021-07-17 09:06:33 +00:00
|
|
|
"github.com/georgysavva/scany/pgxscan"
|
2021-10-18 08:31:17 +00:00
|
|
|
"github.com/jackc/pgx"
|
2021-07-17 09:06:33 +00:00
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
2021-07-18 05:28:24 +00:00
|
|
|
"github.com/pquerna/ffjson/ffjson"
|
2021-07-17 09:06:33 +00:00
|
|
|
)
|
|
|
|
|
2021-07-17 15:06:43 +00:00
|
|
|
// GetAllJSONJokes fetch the database for all the jokes then output it as a JSON []byte.
|
|
|
|
// Keep in mind, you will need to store it to memory yourself.
|
2021-10-18 08:31:17 +00:00
|
|
|
func GetAllJSONJokes(db *pgxpool.Pool, ctx context.Context) ([]byte, error) {
|
|
|
|
conn, err := db.Acquire(ctx)
|
2021-07-17 09:06:33 +00:00
|
|
|
if err != nil {
|
2021-10-18 08:31:17 +00:00
|
|
|
return []byte{}, err
|
2021-07-17 09:06:33 +00:00
|
|
|
}
|
2021-09-27 10:10:19 +00:00
|
|
|
defer conn.Release()
|
2021-07-17 09:06:33 +00:00
|
|
|
|
2021-10-18 08:31:17 +00:00
|
|
|
var jokes []schema.Joke
|
2021-11-01 12:32:33 +00:00
|
|
|
results, err := conn.Query(ctx, "SELECT \"id\",\"link\" FROM \"jokesbapak2\" ORDER BY \"id\"")
|
2021-09-27 10:10:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-08-04 05:56:14 +00:00
|
|
|
defer results.Close()
|
|
|
|
|
2021-07-17 09:06:33 +00:00
|
|
|
err = pgxscan.ScanAll(&jokes, results)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-18 05:28:24 +00:00
|
|
|
data, err := ffjson.Marshal(jokes)
|
2021-07-17 09:06:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2021-10-18 08:31:17 +00:00
|
|
|
// Only return a link
|
|
|
|
func GetRandomJokeFromDB(db *pgxpool.Pool, ctx context.Context) (string, error) {
|
|
|
|
conn, err := db.Acquire(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-11-08 12:39:29 +00:00
|
|
|
defer conn.Release()
|
2021-10-18 08:31:17 +00:00
|
|
|
|
|
|
|
var link string
|
2021-11-01 12:32:33 +00:00
|
|
|
err = conn.QueryRow(ctx, "SELECT link FROM jokesbapak2 ORDER BY random() LIMIT 1").Scan(&link)
|
2021-10-18 08:31:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return link, nil
|
|
|
|
}
|
|
|
|
|
2021-07-17 15:06:43 +00:00
|
|
|
// GetRandomJokeFromCache returns a link string of a random joke from cache.
|
2021-07-18 05:28:24 +00:00
|
|
|
func GetRandomJokeFromCache(memory *bigcache.BigCache) (string, error) {
|
|
|
|
jokes, err := memory.Get("jokes")
|
|
|
|
if err != nil {
|
2021-09-26 19:13:38 +00:00
|
|
|
if errors.Is(err, bigcache.ErrEntryNotFound) {
|
2021-10-18 08:31:17 +00:00
|
|
|
return "", schema.ErrNotFound
|
2021-07-18 05:28:24 +00:00
|
|
|
}
|
|
|
|
return "", err
|
2021-07-17 09:06:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 08:31:17 +00:00
|
|
|
var data []schema.Joke
|
2021-07-18 05:28:24 +00:00
|
|
|
err = ffjson.Unmarshal(jokes, &data)
|
2021-07-17 09:06:33 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
|
2021-07-18 05:28:24 +00:00
|
|
|
// Return an error if the database is empty
|
2021-07-17 10:34:02 +00:00
|
|
|
dataLength := len(data)
|
|
|
|
if dataLength == 0 {
|
2021-10-18 08:31:17 +00:00
|
|
|
return "", schema.ErrEmpty
|
2021-07-17 10:34:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
random := rand.Intn(dataLength)
|
2021-07-17 09:06:33 +00:00
|
|
|
joke := data[random].Link
|
|
|
|
|
|
|
|
return joke, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckJokesCache checks if there is some value inside jokes cache.
|
2021-07-18 05:28:24 +00:00
|
|
|
func CheckJokesCache(memory *bigcache.BigCache) (bool, error) {
|
|
|
|
_, err := memory.Get("jokes")
|
|
|
|
if err != nil {
|
2021-09-26 19:13:38 +00:00
|
|
|
if errors.Is(err, bigcache.ErrEntryNotFound) {
|
2021-07-18 05:28:24 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
2021-07-17 09:06:33 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 10:21:08 +00:00
|
|
|
// CheckTotalJokesCache literally does what the name is for
|
|
|
|
func CheckTotalJokesCache(memory *bigcache.BigCache) (bool, error) {
|
|
|
|
_, err := memory.Get("total")
|
|
|
|
if err != nil {
|
2021-09-26 19:13:38 +00:00
|
|
|
if errors.Is(err, bigcache.ErrEntryNotFound) {
|
2021-07-19 10:21:08 +00:00
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2021-07-17 15:06:43 +00:00
|
|
|
// GetCachedJokeByID returns a link string of a certain ID from cache.
|
2021-07-18 05:28:24 +00:00
|
|
|
func GetCachedJokeByID(memory *bigcache.BigCache, id int) (string, error) {
|
|
|
|
jokes, err := memory.Get("jokes")
|
|
|
|
if err != nil {
|
2021-09-26 19:13:38 +00:00
|
|
|
if errors.Is(err, bigcache.ErrEntryNotFound) {
|
2021-10-18 08:31:17 +00:00
|
|
|
return "", schema.ErrNotFound
|
2021-07-18 05:28:24 +00:00
|
|
|
}
|
|
|
|
return "", err
|
2021-07-17 09:06:33 +00:00
|
|
|
}
|
|
|
|
|
2021-10-18 08:31:17 +00:00
|
|
|
var data []schema.Joke
|
2021-07-18 05:28:24 +00:00
|
|
|
err = ffjson.Unmarshal(jokes, &data)
|
2021-07-17 09:06:33 +00:00
|
|
|
if err != nil {
|
2021-11-08 15:55:07 +00:00
|
|
|
return "", err
|
2021-07-17 09:06:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is a simple solution, might convert it to goroutines and channels sometime soon.
|
|
|
|
for _, v := range data {
|
|
|
|
if v.ID == id {
|
|
|
|
return v.Link, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
}
|
2021-07-19 10:21:08 +00:00
|
|
|
|
|
|
|
// GetCachedTotalJokes
|
|
|
|
func GetCachedTotalJokes(memory *bigcache.BigCache) (int, error) {
|
|
|
|
total, err := memory.Get("total")
|
|
|
|
if err != nil {
|
2021-09-26 19:13:38 +00:00
|
|
|
if errors.Is(err, bigcache.ErrEntryNotFound) {
|
2021-10-18 08:31:17 +00:00
|
|
|
return 0, schema.ErrNotFound
|
2021-07-19 10:21:08 +00:00
|
|
|
}
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-11-08 15:55:07 +00:00
|
|
|
i, err := strconv.Atoi(string(total))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2021-07-19 10:21:08 +00:00
|
|
|
|
2021-11-08 15:55:07 +00:00
|
|
|
return i, nil
|
2021-07-19 10:21:08 +00:00
|
|
|
}
|
2021-10-18 08:31:17 +00:00
|
|
|
|
|
|
|
func CheckJokeExists(db *pgxpool.Pool, ctx context.Context, id string) (bool, error) {
|
|
|
|
conn, err := db.Acquire(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
|
|
|
var query = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)
|
|
|
|
|
|
|
|
sql, args, err := query.
|
|
|
|
Select("id").
|
|
|
|
From("jokesbapak2").
|
|
|
|
Where(squirrel.Eq{"id": id}).
|
|
|
|
ToSql()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var jokeID int
|
2021-11-01 12:32:33 +00:00
|
|
|
err = conn.QueryRow(ctx, sql, args...).Scan(&jokeID)
|
2021-11-08 15:55:07 +00:00
|
|
|
if err != nil && errors.Is(err, pgx.ErrNoRows) {
|
2021-10-18 08:31:17 +00:00
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return strconv.Itoa(jokeID) == id, nil
|
|
|
|
}
|