refactor: documentation & tidy up some code

This commit is contained in:
Reinaldy Rafli 2021-07-17 22:06:43 +07:00
parent 8ac21a1046
commit ccaead16ea
4 changed files with 11 additions and 3 deletions

View File

@ -24,12 +24,15 @@ $ go build main.go
| [gofiber/fiber](https://github.com/gofiber/fiber) | `v2.14.0` | Framework | | [gofiber/fiber](https://github.com/gofiber/fiber) | `v2.14.0` | Framework |
| [jackc/pgx](https://github.com/jackc/pgx) | `v4.11.0` | Database | | [jackc/pgx](https://github.com/jackc/pgx) | `v4.11.0` | Database |
| [go-redis/redis](https://github.com/go-redis/redis) | `v8.11.0` | Cache | | [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 | | [joho/godotenv](https://github.com/joho/godotenv) | `v1.3.0` | Config |
| [getsentry/sentry-go](https://github.com/getsentry/sentry-go) | `v0.11.0` | Logging | | [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 | | [aldy505/phc-crypto](https://github.com/aldy505/phc-crypto) | `v1.1.0` | Utils |
| [Masterminds/squirrel](https://github.com/Masterminds/squirrel ) | `v1.5.0` | Utils | | [Masterminds/squirrel](https://github.com/Masterminds/squirrel ) | `v1.5.0` | Utils |
| [aldy505/bob](https://github.com/aldy505/bob) | `v0.0.1` | 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 ## Directory structure
@ -38,6 +41,7 @@ $ go build main.go
├── app ├── app
│ └── v1 │ └── v1
│ ├── app.go - v1 application entry point │ ├── app.go - v1 application entry point
│ ├── core - Pure business logic
│ ├── handler - Route handler │ ├── handler - Route handler
│ ├── middleware - App middleware handler │ ├── middleware - App middleware handler
│ ├── models - Output and input schema │ ├── models - Output and input schema

View File

@ -11,7 +11,8 @@ import (
"github.com/patrickmn/go-cache" "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) { func GetAllJSONJokes(db *pgxpool.Pool) ([]byte, error) {
var jokes []models.Joke var jokes []models.Joke
results, err := db.Query(context.Background(), "SELECT \"id\",\"link\" FROM \"jokesbapak2\" ORDER BY \"id\"") 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 return data, nil
} }
// GetRandomJokeFromCache // GetRandomJokeFromCache returns a link string of a random joke from cache.
func GetRandomJokeFromCache(memory *cache.Cache) (string, error) { func GetRandomJokeFromCache(memory *cache.Cache) (string, error) {
jokes, found := memory.Get("jokes") jokes, found := memory.Get("jokes")
if !found { if !found {
@ -62,6 +63,7 @@ func CheckJokesCache(memory *cache.Cache) bool {
return found return found
} }
// GetCachedJokeByID returns a link string of a certain ID from cache.
func GetCachedJokeByID(memory *cache.Cache, id int) (string, error) { func GetCachedJokeByID(memory *cache.Cache, id int) (string, error) {
jokes, found := memory.Get("jokes") jokes, found := memory.Get("jokes")
if !found { if !found {

View File

@ -22,6 +22,7 @@ func AddNewJoke(c *fiber.Ctx) error {
return err return err
} }
// TODO: Implement solution if the link provided already exists.
_, err = db.Query(context.Background(), sql, args...) _, err = db.Query(context.Background(), sql, args...)
if err != nil { if err != nil {
return err return err

View File

@ -9,5 +9,6 @@ import (
func Health(app *fiber.App) *fiber.App { func Health(app *fiber.App) *fiber.App {
// Health check // Health check
app.Get("/health", handler.Health) app.Get("/health", handler.Health)
return app return app
} }