From ccaead16ea11e8c3c1d5ba269b80ec0802090a84 Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Sat, 17 Jul 2021 22:06:43 +0700 Subject: [PATCH] refactor: documentation & tidy up some code --- api/README.md | 6 +++++- api/app/v1/core/jokes.go | 6 ++++-- api/app/v1/handler/joke_add.go | 1 + api/app/v1/routes/health.go | 1 + 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/api/README.md b/api/README.md index 457db08..8e0497d 100644 --- a/api/README.md +++ b/api/README.md @@ -24,12 +24,15 @@ $ go build main.go | [gofiber/fiber](https://github.com/gofiber/fiber) | `v2.14.0` | Framework | | [jackc/pgx](https://github.com/jackc/pgx) | `v4.11.0` | Database | | [go-redis/redis](https://github.com/go-redis/redis) | `v8.11.0` | Cache | +| [patrickmn/go-cache](https://github.com/patrickmn/go-cache) | `v2.1.0+incompatible` | Cache | | [joho/godotenv](https://github.com/joho/godotenv) | `v1.3.0` | Config | | [getsentry/sentry-go](https://github.com/getsentry/sentry-go) | `v0.11.0` | Logging | | [aldy505/phc-crypto](https://github.com/aldy505/phc-crypto) | `v1.1.0` | Utils | | [Masterminds/squirrel](https://github.com/Masterminds/squirrel ) | `v1.5.0` | Utils | | [aldy505/bob](https://github.com/aldy505/bob) | `v0.0.1` | Utils | -| [gojek/heimdall](github.com/gojek/heimdall) | `v7.0.2` | Utils | +| [gojek/heimdall](https://github.com/gojek/heimdall) | `v7.0.2` | Utils | +| [georgysavva/scany](https://github.com/georgysavva/scany) | `v0.2.9` | Utils | +| [stretchr/testify](https://github.com/stretchr/testify) | `v1.5.1` | Tests | ## Directory structure @@ -38,6 +41,7 @@ $ go build main.go ├── app │ └── v1 │ ├── app.go - v1 application entry point +│ ├── core - Pure business logic │ ├── handler - Route handler │ ├── middleware - App middleware handler │ ├── models - Output and input schema diff --git a/api/app/v1/core/jokes.go b/api/app/v1/core/jokes.go index bea84a9..bb7cfa9 100644 --- a/api/app/v1/core/jokes.go +++ b/api/app/v1/core/jokes.go @@ -11,7 +11,8 @@ import ( "github.com/patrickmn/go-cache" ) -// GetAllJSONJokes +// 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. func GetAllJSONJokes(db *pgxpool.Pool) ([]byte, error) { var jokes []models.Joke results, err := db.Query(context.Background(), "SELECT \"id\",\"link\" FROM \"jokesbapak2\" ORDER BY \"id\"") @@ -32,7 +33,7 @@ func GetAllJSONJokes(db *pgxpool.Pool) ([]byte, error) { return data, nil } -// GetRandomJokeFromCache +// GetRandomJokeFromCache returns a link string of a random joke from cache. func GetRandomJokeFromCache(memory *cache.Cache) (string, error) { jokes, found := memory.Get("jokes") if !found { @@ -62,6 +63,7 @@ func CheckJokesCache(memory *cache.Cache) bool { return found } +// GetCachedJokeByID returns a link string of a certain ID from cache. func GetCachedJokeByID(memory *cache.Cache, id int) (string, error) { jokes, found := memory.Get("jokes") if !found { diff --git a/api/app/v1/handler/joke_add.go b/api/app/v1/handler/joke_add.go index 830d161..732fa00 100644 --- a/api/app/v1/handler/joke_add.go +++ b/api/app/v1/handler/joke_add.go @@ -22,6 +22,7 @@ func AddNewJoke(c *fiber.Ctx) error { return err } + // TODO: Implement solution if the link provided already exists. _, err = db.Query(context.Background(), sql, args...) if err != nil { return err diff --git a/api/app/v1/routes/health.go b/api/app/v1/routes/health.go index 1843a58..7c4dfdd 100644 --- a/api/app/v1/routes/health.go +++ b/api/app/v1/routes/health.go @@ -9,5 +9,6 @@ import ( func Health(app *fiber.App) *fiber.App { // Health check app.Get("/health", handler.Health) + return app }