diff --git a/cmd/web/main.go b/cmd/web/main.go index 2383384..e55b0ff 100644 --- a/cmd/web/main.go +++ b/cmd/web/main.go @@ -5,11 +5,12 @@ import ( "flag" "fmt" "go-udemy-web-1/internal/config" - "go-udemy-web-1/internal/driver" "go-udemy-web-1/internal/handlers" "go-udemy-web-1/internal/helpers" "go-udemy-web-1/internal/models" "go-udemy-web-1/internal/render" + "go-udemy-web-1/internal/repository" + "go-udemy-web-1/internal/repository/driverrepo" "log" "net/http" "os" @@ -50,7 +51,7 @@ func main() { log.Fatal(err) } -func run() (*driver.DB, error) { +func run() (*repository.DB, error) { // what am I going to put in the session gob.Register(models.Reservation{}) gob.Register(models.User{}) @@ -103,7 +104,8 @@ func run() (*driver.DB, error) { log.Println("Connecting to database...") dsn := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=%s", *dbHost, *dbPort, *dbName, *dbUser, *dbPass, *dbSSL) - db, err := driver.ConnectSQL(dsn) + dbdriver := driverrepo.NewSqlRepo() + db, err := dbdriver.ConnectSQL(dsn) if err != nil { log.Fatal("Cannot connect to database! Dying...") } diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index 22abf2c..6bedd22 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "go-udemy-web-1/internal/config" - "go-udemy-web-1/internal/driver" "go-udemy-web-1/internal/forms" "go-udemy-web-1/internal/helpers" "go-udemy-web-1/internal/models" @@ -30,7 +29,7 @@ type Repository struct { } // NewRepo creates a new repository -func NewRepo(a *config.AppConfig, db *driver.DB) *Repository { +func NewRepo(a *config.AppConfig, db *repository.DB) *Repository { return &Repository{ App: a, DB: dbrepo.NewPostgresRepo(db.SQL, a), diff --git a/internal/handlers/handlers_test.go b/internal/handlers/handlers_test.go index c6a2686..ac0d811 100644 --- a/internal/handlers/handlers_test.go +++ b/internal/handlers/handlers_test.go @@ -3,8 +3,8 @@ package handlers import ( "context" "encoding/json" - "go-udemy-web-1/internal/driver" "go-udemy-web-1/internal/models" + "go-udemy-web-1/internal/repository" "log" "net/http" "net/http/httptest" @@ -24,7 +24,7 @@ type postData struct { // {{{ Test NewRepo func Test_NewRepo(t *testing.T) { - var db driver.DB + var db repository.DB repo := NewRepo(&app, &db) if reflect.TypeOf(repo).String() != "*handlers.Repository" { diff --git a/internal/repository/dbrepo/dbrepo.go b/internal/repository/dbrepo/dbrepo.go index 7510aa4..425cb02 100644 --- a/internal/repository/dbrepo/dbrepo.go +++ b/internal/repository/dbrepo/dbrepo.go @@ -4,7 +4,9 @@ import ( "database/sql" "go-udemy-web-1/internal/config" "go-udemy-web-1/internal/repository" - "go-udemy-web-1/internal/repository/db" + "go-udemy-web-1/internal/repository/sqlc" + + "github.com/jackc/pgx/v5" ) type postgresDBRepo struct { @@ -14,7 +16,7 @@ type postgresDBRepo struct { type pgcDBRepo struct { App *config.AppConfig - Q *db.Queries + Q *sqlc.Queries } type testDBRepo struct { @@ -35,7 +37,8 @@ func NewTestingRepo(a *config.AppConfig) repository.DatabaseRepo { } } -func NewPgcRepo(q *db.Queries, a *config.AppConfig) repository.DatabaseRepo { +func NewPgcRepo(conn *pgx.Conn, a *config.AppConfig) repository.DatabaseRepo { + q := sqlc.New(conn) return &pgcDBRepo{ App: a, Q: q, diff --git a/internal/repository/dbrepo/pgc.go b/internal/repository/dbrepo/pgc.go index 0583a44..ef32f74 100644 --- a/internal/repository/dbrepo/pgc.go +++ b/internal/repository/dbrepo/pgc.go @@ -4,7 +4,7 @@ import ( "context" "errors" "go-udemy-web-1/internal/models" - "go-udemy-web-1/internal/repository/db" + "go-udemy-web-1/internal/repository/sqlc" "time" "github.com/jackc/pgx/v5/pgtype" @@ -21,7 +21,7 @@ func (m *pgcDBRepo) InsertReservation(res models.Reservation) (int, error) { defer cancel() var newId int32 - newId, err := m.Q.InsertReservation(ctx, db.InsertReservationParams{ + newId, err := m.Q.InsertReservation(ctx, sqlc.InsertReservationParams{ FirstName: res.FirstName, LastName: res.LastName, Email: res.Email, @@ -44,7 +44,7 @@ func (m *pgcDBRepo) InsertRoomRestriction(r models.RoomRestriction) error { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() - err := m.Q.InsertRoomRestriction(ctx, db.InsertRoomRestrictionParams{ + err := m.Q.InsertRoomRestriction(ctx, sqlc.InsertRoomRestrictionParams{ StartDate: pgtype.Date{Time: r.StartDate, Valid: true}, EndDate: pgtype.Date{Time: r.EndDate, Valid: true}, RoomID: int32(r.Room.ID), @@ -64,7 +64,7 @@ func (m *pgcDBRepo) SearchAvailabilityByDatesByRoomID(start, end time.Time, room ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() - numRows, err := m.Q.SearchAvailabilityByDatesByRoomID(ctx, db.SearchAvailabilityByDatesByRoomIDParams{ + numRows, err := m.Q.SearchAvailabilityByDatesByRoomID(ctx, sqlc.SearchAvailabilityByDatesByRoomIDParams{ RoomID: int32(roomID), EndDate: pgtype.Date{Time: start, Valid: true}, StartDate: pgtype.Date{Time: end, Valid: false}, @@ -87,7 +87,7 @@ func (m *pgcDBRepo) SearchAvailabilityForAllRooms(start, end time.Time) ([]model var rooms []models.Room - rows, err := m.Q.SearchAvailabilityForAllRooms(ctx, db.SearchAvailabilityForAllRoomsParams{ + rows, err := m.Q.SearchAvailabilityForAllRooms(ctx, sqlc.SearchAvailabilityForAllRoomsParams{ EndDate: pgtype.Date{Time: start, Valid: true}, StartDate: pgtype.Date{Time: start, Valid: true}, }) @@ -153,7 +153,7 @@ func (m *pgcDBRepo) UpdateUser(u models.User) error { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() - err := m.Q.UpdateUser(ctx, db.UpdateUserParams{ + err := m.Q.UpdateUser(ctx, sqlc.UpdateUserParams{ FirstName: u.FirstName, LastName: u.LastName, Email: u.Email, @@ -288,7 +288,7 @@ func (m *pgcDBRepo) UpdateReservation(r models.Reservation) error { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() - err := m.Q.UpdateReservation(ctx, db.UpdateReservationParams{ + err := m.Q.UpdateReservation(ctx, sqlc.UpdateReservationParams{ ID: int32(r.ID), FirstName: r.FirstName, LastName: r.LastName, @@ -320,7 +320,7 @@ func (m *pgcDBRepo) UpdateProcessedForReservation(id, processed int) error { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() - err := m.Q.UpdateProcessedForReservation(ctx, db.UpdateProcessedForReservationParams{ + err := m.Q.UpdateProcessedForReservation(ctx, sqlc.UpdateProcessedForReservationParams{ Processed: int32(processed), ID: int32(id), }) @@ -358,7 +358,7 @@ func (m *pgcDBRepo) GetRestrictionsForRoomByDate(roomId int, start, end time.Tim var restrictions []models.RoomRestriction - rows, err := m.Q.GetRestrictionsForRoomByDate(ctx, db.GetRestrictionsForRoomByDateParams{ + rows, err := m.Q.GetRestrictionsForRoomByDate(ctx, sqlc.GetRestrictionsForRoomByDateParams{ EndDate: pgtype.Date{Time: end, Valid: true}, StartDate: pgtype.Date{Time: start, Valid: true}, RoomID: int32(roomId), @@ -386,7 +386,7 @@ func (m *pgcDBRepo) InsertBlockForRoom(id int, startDate time.Time) error { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) defer cancel() - err := m.Q.InsertBlockForRoom(ctx, db.InsertBlockForRoomParams{ + err := m.Q.InsertBlockForRoom(ctx, sqlc.InsertBlockForRoomParams{ StartDate: pgtype.Date{Time: startDate, Valid: true}, EndDate: pgtype.Date{Time: startDate.AddDate(0, 0, 1)}, RoomID: int32(id), diff --git a/internal/repository/driverrepo/driverrepo.go b/internal/repository/driverrepo/driverrepo.go new file mode 100644 index 0000000..6ae74fa --- /dev/null +++ b/internal/repository/driverrepo/driverrepo.go @@ -0,0 +1,18 @@ +package driverrepo + +import ( + "go-udemy-web-1/internal/repository" +) + +type ( + sqlDriverRepo struct{} + sqlcDriverRepo struct{} +) + +func NewSqlRepo() repository.DatabaseDriverRepo { + return &sqlDriverRepo{} +} + +// func NewSqlcRepo() repository.DatabaseDriverRepo { +// return &sqlcDriverRepo{} +// } diff --git a/internal/driver/driver.go b/internal/repository/driverrepo/sqldriver.go similarity index 73% rename from internal/driver/driver.go rename to internal/repository/driverrepo/sqldriver.go index 84416b9..cd499b2 100644 --- a/internal/driver/driver.go +++ b/internal/repository/driverrepo/sqldriver.go @@ -1,7 +1,8 @@ -package driver +package driverrepo import ( "database/sql" + "go-udemy-web-1/internal/repository" "time" _ "github.com/jackc/pgconn" @@ -9,12 +10,7 @@ import ( _ "github.com/jackc/pgx/v5/stdlib" ) -// DB holds the database connection pool -type DB struct { - SQL *sql.DB -} - -var dbConn = &DB{} +var dbConn = &repository.DB{} const ( maxOpenDbConn = 10 @@ -23,8 +19,8 @@ const ( ) // ConnectSQL creates SQL pool for Postgres -func ConnectSQL(dsn string) (*DB, error) { - d, err := NewDatabase(dsn) +func (s *sqlDriverRepo) ConnectSQL(dsn string) (*repository.DB, error) { + d, err := newDatabase(dsn) if err != nil { panic(err) } @@ -51,8 +47,8 @@ func testDB(d *sql.DB) error { return nil } -// NewDatabase creates a new datavase for the application -func NewDatabase(dsn string) (*sql.DB, error) { +// newDatabase creates a new database for the application +func newDatabase(dsn string) (*sql.DB, error) { db, err := sql.Open("pgx", dsn) if err != nil { return nil, err diff --git a/internal/repository/repository.go b/internal/repository/repository.go index f191a89..23cb58d 100644 --- a/internal/repository/repository.go +++ b/internal/repository/repository.go @@ -1,8 +1,11 @@ package repository import ( + "database/sql" "go-udemy-web-1/internal/models" "time" + + "github.com/jackc/pgx/v5" ) type DatabaseRepo interface { @@ -27,3 +30,13 @@ type DatabaseRepo interface { InsertBlockForRoom(id int, startDate time.Time) error DeleteBlockByID(id int) error } + +// DB holds the database connection pool +type DB struct { + SQL *sql.DB + PG *pgx.Conn +} + +type DatabaseDriverRepo interface { + ConnectSQL(dsn string) (*DB, error) +} diff --git a/internal/repository/db/db.go b/internal/repository/sqlc/db.go similarity index 97% rename from internal/repository/db/db.go rename to internal/repository/sqlc/db.go index 5b8c8f5..278c210 100644 --- a/internal/repository/db/db.go +++ b/internal/repository/sqlc/db.go @@ -2,7 +2,7 @@ // versions: // sqlc v1.26.0 -package db +package sqlc import ( "context" diff --git a/internal/repository/db/models.go b/internal/repository/sqlc/models.go similarity index 98% rename from internal/repository/db/models.go rename to internal/repository/sqlc/models.go index 606c5b0..fb61b86 100644 --- a/internal/repository/db/models.go +++ b/internal/repository/sqlc/models.go @@ -2,7 +2,7 @@ // versions: // sqlc v1.26.0 -package db +package sqlc import ( "github.com/jackc/pgx/v5/pgtype" diff --git a/internal/repository/db/query.sql.go b/internal/repository/sqlc/query.sql.go similarity index 99% rename from internal/repository/db/query.sql.go rename to internal/repository/sqlc/query.sql.go index 7996268..4a7d06d 100644 --- a/internal/repository/db/query.sql.go +++ b/internal/repository/sqlc/query.sql.go @@ -3,7 +3,7 @@ // sqlc v1.26.0 // source: query.sql -package db +package sqlc import ( "context" diff --git a/sqlc.yml b/sqlc.yml index cd0e8db..e3d2462 100644 --- a/sqlc.yml +++ b/sqlc.yml @@ -5,6 +5,6 @@ sql: schema: "sql/schema.sql" gen: go: - package: "db" - out: "internal/repository/db" + package: "sqlc" + out: "internal/repository/sqlc" sql_package: "pgx/v5"