Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor! #20

Merged
merged 5 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions backend/cmd/server/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package main

import (
"log"

"github.com/GenerateNU/nightlife/internal/config"
"github.com/GenerateNU/nightlife/internal/db"
"github.com/GenerateNU/nightlife/internal/middleware"
"github.com/GenerateNU/nightlife/internal/router"
"github.com/gofiber/fiber/v2"
"log"
)

func main() {
Expand All @@ -18,7 +19,7 @@ func main() {
}

// test the database connection
_, err = db.ConnectSupabaseDB()
conn, err := db.ConnectSupabaseDB()
if err != nil {
log.Fatalf("Unable to load environment variables necessary for application")
}
Expand All @@ -30,7 +31,7 @@ func main() {
middleware.UseMiddleware(app)

// Hello Group
router.InitializeRoutes(app, cfg)
router.InitializeRoutes(app, cfg, conn)

//Run app
log.Fatal(app.Listen(":8080"))
Expand Down
2 changes: 2 additions & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/nedpals/postgrest-go v0.1.3 // indirect
github.com/pkg/errors v0.8.1 // indirect
)

require (
Expand All @@ -24,6 +25,7 @@ require (
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx v3.6.2+incompatible
github.com/klauspost/compress v1.17.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrU
github.com/jackc/pgtype v1.8.1-0.20210724151600-32e20a603178/go.mod h1:C516IlIV9NKqfsMCXTdChteoXmwgUceqaLfjg2e3NlM=
github.com/jackc/pgtype v1.14.0 h1:y+xUdabmyMkJLyApYuPj38mW+aAIqCe5uuBB51rH3Vw=
github.com/jackc/pgtype v1.14.0/go.mod h1:LUMuVrfsFfdKGLw+AFFVv6KtHOFMwRgDDzBt76IqCA4=
github.com/jackc/pgx v3.6.2+incompatible h1:2zP5OD7kiyR3xzRYMhOcXVvkDZsImVXfj+yIyTQf3/o=
github.com/jackc/pgx v3.6.2+incompatible/go.mod h1:0ZGrqGqkRlliWnWB4zKnWtjbSWbGkVEFm4TeybAXq+I=
github.com/jackc/pgx/v4 v4.0.0-20190420224344-cc3461e65d96/go.mod h1:mdxmSJJuR08CZQyj1PVQBHy9XOp5p8/SHH6a0psbY9Y=
github.com/jackc/pgx/v4 v4.0.0-20190421002000-1b8f0016e912/go.mod h1:no/Y67Jkk/9WuGR0JG/JseM9irFbnEPbuWV2EELPNuM=
github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQnOEnf1dqHGpw7JmHqHc1NxDoalibchSk9/RWuDc=
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ func ConnectSupabaseDB() (*pgx.Conn, error) {
}

fmt.Println("Successfully connected to the database!")
return conn, nil
return conn, err
wyattchris marked this conversation as resolved.
Show resolved Hide resolved
}
7 changes: 3 additions & 4 deletions backend/internal/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"github.com/GenerateNU/nightlife/internal/config"
hello "github.com/GenerateNU/nightlife/internal/schema/hello/routes"
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v4"
)

func InitializeRoutes(app *fiber.App, cfg *config.Config) {

func InitializeRoutes(app *fiber.App, cfg *config.Config, conn *pgx.Conn) {
// Add each route group here
hello.RouteHelloGroup(app, cfg)

hello.RouteHelloGroup(app, cfg, conn)
}
11 changes: 8 additions & 3 deletions backend/internal/schema/hello/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
"github.com/GenerateNU/nightlife/internal/config"
hello "github.com/GenerateNU/nightlife/internal/schema/hello/transactions"
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v4"
)

// Create HelloGroup fiber route group
func RouteHelloGroup(app *fiber.App, config *config.Config) {
func RouteHelloGroup(app *fiber.App, config *config.Config, db *pgx.Conn) {

// Create Protected Grouping
protected := app.Group("/hello_protected")
Expand All @@ -22,7 +23,11 @@ func RouteHelloGroup(app *fiber.App, config *config.Config) {
unprotected := app.Group("/hello")

//Endpoints
protected.Get("/world", hello.RetHelloWorld)
unprotected.Get("/world", hello.RetHelloWorld)
protected.Get("/world", func(c *fiber.Ctx) error {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you create a struct with the db as a field, you can simplify this.
struct example
register route example

return hello.RetHelloWorld(c, db)
})
unprotected.Get("/world", func(c *fiber.Ctx) error {
return hello.RetHelloWorld(c, db)
})

}
3 changes: 2 additions & 1 deletion backend/internal/schema/hello/transactions/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import (
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v4"
)

func RetHelloWorld(ctx *fiber.Ctx) error {
func RetHelloWorld(ctx *fiber.Ctx, db *pgx.Conn) error {

Check failure on line 8 in backend/internal/schema/hello/transactions/transactions.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'db' seems to be unused, consider removing or renaming it as _ (revive)
Copy link
Member

@garrettladley garrettladley Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by doing the struct field approach, you will resolve this error. also, is this really a "transaction"? i would consider this a handler.

return ctx.SendString("Hello, World!")
}
Loading