Skip to content

Commit

Permalink
Connecting to postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
tsawler committed Apr 5, 2021
1 parent db0c3ed commit 7c71f5b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 6 deletions.
21 changes: 16 additions & 5 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"github.com/alexedwards/scs/v2"
"github.com/tsawler/bookings-app/internal/config"
"github.com/tsawler/bookings-app/internal/driver"
"github.com/tsawler/bookings-app/internal/handlers"
"github.com/tsawler/bookings-app/internal/helpers"
"github.com/tsawler/bookings-app/internal/models"
Expand All @@ -24,10 +25,11 @@ var errorLog *log.Logger

// main is the main function
func main() {
err := run()
db, err := run()
if err != nil {
log.Fatal(err)
}
defer db.SQL.Close()

fmt.Println(fmt.Sprintf("Staring application on port %s", portNumber))

Expand All @@ -42,7 +44,7 @@ func main() {
}
}

func run() error {
func run() (*driver.DB, error) {
// what am I going to put in the session
gob.Register(models.Reservation{})

Expand All @@ -64,19 +66,28 @@ func run() error {

app.Session = session

// connect to database
log.Println("Connecting to database...")
db, err := driver.ConnectSQL("host=localhost port=5432 dbname=bookings user=tcs password=")
if err != nil {
log.Fatal("Cannot connect to database! Dying...")
}

log.Println("Connected to database!")

tc, err := render.CreateTemplateCache()
if err != nil {
log.Fatal("cannot create template cache")
return err
return nil, err
}

app.TemplateCache = tc
app.UseCache = false

repo := handlers.NewRepo(&app)
repo := handlers.NewRepo(&app, db)
handlers.NewHandlers(repo)
render.NewTemplates(&app)
helpers.NewHelpers(&app)

return nil
return db, nil
}
7 changes: 6 additions & 1 deletion internal/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"encoding/json"
"fmt"
"github.com/tsawler/bookings-app/internal/config"
"github.com/tsawler/bookings-app/internal/driver"
"github.com/tsawler/bookings-app/internal/forms"
"github.com/tsawler/bookings-app/internal/helpers"
"github.com/tsawler/bookings-app/internal/models"
"github.com/tsawler/bookings-app/internal/render"
"github.com/tsawler/bookings-app/internal/repository"
"github.com/tsawler/bookings-app/internal/repository/dbrepo"
"net/http"
)

Expand All @@ -17,12 +20,14 @@ var Repo *Repository
// Repository is the repository type
type Repository struct {
App *config.AppConfig
DB repository.DatabaseRepo
}

// NewRepo creates a new repository
func NewRepo(a *config.AppConfig) *Repository {
func NewRepo(a *config.AppConfig, db *driver.DB) *Repository {
return &Repository{
App: a,
DB: dbrepo.NewPostgresRepo(db.SQL, a),
}
}

Expand Down
19 changes: 19 additions & 0 deletions internal/repository/dbrepo/dbrepo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dbrepo

import (
"database/sql"
"github.com/tsawler/bookings-app/internal/config"
"github.com/tsawler/bookings-app/internal/repository"
)

type postgresDBRepo struct {
App *config.AppConfig
DB *sql.DB
}

func NewPostgresRepo(conn *sql.DB, a *config.AppConfig) repository.DatabaseRepo {
return &postgresDBRepo{
App: a,
DB: conn,
}
}
5 changes: 5 additions & 0 deletions internal/repository/dbrepo/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dbrepo

func (m *postgresDBRepo) AllUsers() bool {
return true
}
5 changes: 5 additions & 0 deletions internal/repository/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package repository

type DatabaseRepo interface {
AllUsers() bool
}

0 comments on commit 7c71f5b

Please sign in to comment.