diff --git a/api/app/v1/app.go b/api/app/v1/app.go index 98f29ba..328e107 100644 --- a/api/app/v1/app.go +++ b/api/app/v1/app.go @@ -44,20 +44,13 @@ func New() *fiber.App { log.Fatal(err) } - checkCache, err := core.CheckJokesCache(memory) + err = core.SetAllJSONJoke(db, memory) if err != nil { log.Fatalln(err) } - - if !checkCache { - jokes, err := core.GetAllJSONJokes(db) - if err != nil { - log.Fatalln(err) - } - err = memory.Set("jokes", jokes) - if err != nil { - log.Fatalln(err) - } + err = core.SetTotalJoke(db, memory) + if err != nil { + log.Fatalln(err) } app.Use(cors.New()) diff --git a/api/app/v1/core/jokes.go b/api/app/v1/core/joke_getter.go similarity index 80% rename from api/app/v1/core/jokes.go rename to api/app/v1/core/joke_getter.go index 799aad4..9d55b37 100644 --- a/api/app/v1/core/jokes.go +++ b/api/app/v1/core/joke_getter.go @@ -74,6 +74,19 @@ func CheckJokesCache(memory *bigcache.BigCache) (bool, error) { return true, nil } +// CheckTotalJokesCache literally does what the name is for +func CheckTotalJokesCache(memory *bigcache.BigCache) (bool, error) { + _, err := memory.Get("total") + if err != nil { + if err.Error() == "Entry not found" { + return false, nil + } + return false, err + } + + return true, nil +} + // GetCachedJokeByID returns a link string of a certain ID from cache. func GetCachedJokeByID(memory *bigcache.BigCache, id int) (string, error) { jokes, err := memory.Get("jokes") @@ -99,3 +112,16 @@ func GetCachedJokeByID(memory *bigcache.BigCache, id int) (string, error) { return "", nil } + +// GetCachedTotalJokes +func GetCachedTotalJokes(memory *bigcache.BigCache) (int, error) { + total, err := memory.Get("total") + if err != nil { + if err.Error() == "Entry not found" { + return 0, models.ErrNotFound + } + return 0, err + } + + return int(total[0]), nil +} diff --git a/api/app/v1/core/joke_setter.go b/api/app/v1/core/joke_setter.go new file mode 100644 index 0000000..d982a5a --- /dev/null +++ b/api/app/v1/core/joke_setter.go @@ -0,0 +1,57 @@ +package core + +import ( + "jokes-bapak2-api/app/v1/models" + + "github.com/allegro/bigcache/v3" + "github.com/jackc/pgx/v4/pgxpool" + "github.com/pquerna/ffjson/ffjson" +) + +// SetAllJSONJoke fetches jokes data from GetAllJSONJokes then set it to memory cache. +func SetAllJSONJoke(db *pgxpool.Pool, memory *bigcache.BigCache) error { + jokes, err := GetAllJSONJokes(db) + if err != nil { + return err + } + err = memory.Set("jokes", jokes) + if err != nil { + return err + } + return nil +} + +func SetTotalJoke(db *pgxpool.Pool, memory *bigcache.BigCache) error { + check, err := CheckJokesCache(memory) + if err != nil { + return err + } + + if !check { + return models.ErrEmpty + } + + err = SetAllJSONJoke(db, memory) + if err != nil { + return err + } + + jokes, err := memory.Get("jokes") + if err != nil { + return err + } + + var data []models.Joke + err = ffjson.Unmarshal(jokes, &data) + if err != nil { + return err + } + + var total = []byte{byte(len(data))} + err = memory.Set("total", total) + if err != nil { + return err + } + + return nil +} diff --git a/api/app/v1/handler/joke_add.go b/api/app/v1/handler/joke_add.go index c150ed0..71a9136 100644 --- a/api/app/v1/handler/joke_add.go +++ b/api/app/v1/handler/joke_add.go @@ -27,11 +27,11 @@ func AddNewJoke(c *fiber.Ctx) error { return err } - jokes, err := core.GetAllJSONJokes(db) + err = core.SetAllJSONJoke(db, memory) if err != nil { return err } - err = memory.Set("jokes", jokes) + err = core.SetTotalJoke(db, memory) if err != nil { return err } diff --git a/api/app/v1/handler/joke_delete.go b/api/app/v1/handler/joke_delete.go index e3c97d0..e7e2d74 100644 --- a/api/app/v1/handler/joke_delete.go +++ b/api/app/v1/handler/joke_delete.go @@ -40,11 +40,11 @@ func DeleteJoke(c *fiber.Ctx) error { return err } - jokes, err := core.GetAllJSONJokes(db) + err = core.SetAllJSONJoke(db, memory) if err != nil { return err } - err = memory.Set("jokes", jokes) + err = core.SetTotalJoke(db, memory) if err != nil { return err } diff --git a/api/app/v1/handler/joke_total.go b/api/app/v1/handler/joke_total.go index 4fdbb6b..eee7af5 100644 --- a/api/app/v1/handler/joke_total.go +++ b/api/app/v1/handler/joke_total.go @@ -3,30 +3,27 @@ package handler import ( "jokes-bapak2-api/app/v1/core" "jokes-bapak2-api/app/v1/models" + "log" "strconv" "github.com/gofiber/fiber/v2" - "github.com/pquerna/ffjson/ffjson" ) func TotalJokes(c *fiber.Ctx) error { - checkCache, err := core.CheckJokesCache(memory) + checkTotal, err := core.CheckTotalJokesCache(memory) if err != nil { return err } - if !checkCache { - jokes, err := core.GetAllJSONJokes(db) - if err != nil { - return err - } - err = memory.Set("jokes", jokes) + if !checkTotal { + err = core.SetTotalJoke(db, memory) if err != nil { return err } } - jokes, err := memory.Get("jokes") + total, err := memory.Get("total") + log.Println(err) if err != nil { if err.Error() == "Entry not found" { return c.Status(fiber.StatusInternalServerError).JSON(models.Error{ @@ -36,14 +33,7 @@ func TotalJokes(c *fiber.Ctx) error { return err } - var data []models.Joke - err = ffjson.Unmarshal(jokes, &data) - if err != nil { - return err - } - - dataLength := strconv.Itoa(len(data)) return c.Status(fiber.StatusOK).JSON(models.ResponseJoke{ - Message: dataLength, + Message: strconv.Itoa(int(total[0])), }) } diff --git a/api/app/v1/handler/joke_update.go b/api/app/v1/handler/joke_update.go index 958380b..13abf1b 100644 --- a/api/app/v1/handler/joke_update.go +++ b/api/app/v1/handler/joke_update.go @@ -41,12 +41,11 @@ func UpdateJoke(c *fiber.Ctx) error { return err } - jokes, err := core.GetAllJSONJokes(db) + err = core.SetAllJSONJoke(db, memory) if err != nil { return err } - - err = memory.Set("jokes", jokes) + err = core.SetTotalJoke(db, memory) if err != nil { return err }