Finish adding sqlc and fix some bugs
This commit is contained in:
parent
459a4e5c7d
commit
dd49a1e687
@ -104,7 +104,7 @@ func run() (*repository.DB, error) {
|
|||||||
log.Println("Connecting to database...")
|
log.Println("Connecting to database...")
|
||||||
dsn := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=%s",
|
dsn := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=%s",
|
||||||
*dbHost, *dbPort, *dbName, *dbUser, *dbPass, *dbSSL)
|
*dbHost, *dbPort, *dbName, *dbUser, *dbPass, *dbSSL)
|
||||||
dbdriver := driverrepo.NewSqlRepo()
|
dbdriver := driverrepo.NewSqlcRepo()
|
||||||
db, err := dbdriver.ConnectSQL(dsn)
|
db, err := dbdriver.ConnectSQL(dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Cannot connect to database! Dying...")
|
log.Fatal("Cannot connect to database! Dying...")
|
||||||
|
@ -32,7 +32,7 @@ type Repository struct {
|
|||||||
func NewRepo(a *config.AppConfig, db *repository.DB) *Repository {
|
func NewRepo(a *config.AppConfig, db *repository.DB) *Repository {
|
||||||
return &Repository{
|
return &Repository{
|
||||||
App: a,
|
App: a,
|
||||||
DB: dbrepo.NewPostgresRepo(db.SQL, a),
|
DB: dbrepo.NewPgcRepo(db.PG, a),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,11 +28,12 @@ func (m *pgcDBRepo) InsertReservation(res models.Reservation) (int, error) {
|
|||||||
Phone: res.Phone,
|
Phone: res.Phone,
|
||||||
StartDate: pgtype.Date{Time: res.StartDate, Valid: true},
|
StartDate: pgtype.Date{Time: res.StartDate, Valid: true},
|
||||||
EndDate: pgtype.Date{Time: res.EndDate, Valid: true},
|
EndDate: pgtype.Date{Time: res.EndDate, Valid: true},
|
||||||
RoomID: int32(res.Room.ID),
|
RoomID: int32(res.RoomID),
|
||||||
CreatedAt: pgtype.Timestamp{Time: res.CreatedAt, Valid: true},
|
CreatedAt: pgtype.Timestamp{Time: time.Now(), Valid: true},
|
||||||
UpdatedAt: pgtype.Timestamp{Time: res.UpdatedAt, Valid: true},
|
UpdatedAt: pgtype.Timestamp{Time: time.Now(), Valid: true},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
m.App.ErrorLog.Println(err)
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,13 +48,14 @@ func (m *pgcDBRepo) InsertRoomRestriction(r models.RoomRestriction) error {
|
|||||||
err := m.Q.InsertRoomRestriction(ctx, sqlc.InsertRoomRestrictionParams{
|
err := m.Q.InsertRoomRestriction(ctx, sqlc.InsertRoomRestrictionParams{
|
||||||
StartDate: pgtype.Date{Time: r.StartDate, Valid: true},
|
StartDate: pgtype.Date{Time: r.StartDate, Valid: true},
|
||||||
EndDate: pgtype.Date{Time: r.EndDate, Valid: true},
|
EndDate: pgtype.Date{Time: r.EndDate, Valid: true},
|
||||||
RoomID: int32(r.Room.ID),
|
RoomID: int32(r.RoomID),
|
||||||
ReservationID: pgtype.Int4{Int32: int32(r.ID), Valid: true},
|
ReservationID: pgtype.Int4{Int32: int32(r.ReservationID), Valid: true},
|
||||||
RestrictionID: int32(r.RestrictionID),
|
RestrictionID: int32(r.RestrictionID),
|
||||||
CreatedAt: pgtype.Timestamp{Time: r.CreatedAt, Valid: true},
|
CreatedAt: pgtype.Timestamp{Time: time.Now(), Valid: true},
|
||||||
UpdatedAt: pgtype.Timestamp{Time: r.UpdatedAt, Valid: true},
|
UpdatedAt: pgtype.Timestamp{Time: time.Now(), Valid: true},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
m.App.ErrorLog.Println(err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -74,10 +76,10 @@ func (m *pgcDBRepo) SearchAvailabilityByDatesByRoomID(start, end time.Time, room
|
|||||||
}
|
}
|
||||||
|
|
||||||
if numRows == 0 {
|
if numRows == 0 {
|
||||||
return true, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return false, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SearchAvailabilityForAllRooms returns a slice of rooms, if any, for given date range
|
// SearchAvailabilityForAllRooms returns a slice of rooms, if any, for given date range
|
||||||
@ -158,7 +160,7 @@ func (m *pgcDBRepo) UpdateUser(u models.User) error {
|
|||||||
LastName: u.LastName,
|
LastName: u.LastName,
|
||||||
Email: u.Email,
|
Email: u.Email,
|
||||||
AccessLevel: int32(u.AccessLevel),
|
AccessLevel: int32(u.AccessLevel),
|
||||||
UpdatedAt: pgtype.Timestamp{Time: u.UpdatedAt, Valid: true},
|
UpdatedAt: pgtype.Timestamp{Time: time.Now(), Valid: true},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -294,7 +296,7 @@ func (m *pgcDBRepo) UpdateReservation(r models.Reservation) error {
|
|||||||
LastName: r.LastName,
|
LastName: r.LastName,
|
||||||
Email: r.Email,
|
Email: r.Email,
|
||||||
Phone: r.Phone,
|
Phone: r.Phone,
|
||||||
UpdatedAt: pgtype.Timestamp{Time: r.UpdatedAt, Valid: true},
|
UpdatedAt: pgtype.Timestamp{Time: time.Now(), Valid: true},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -9,10 +9,12 @@ type (
|
|||||||
sqlcDriverRepo struct{}
|
sqlcDriverRepo struct{}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var dbConn = &repository.DB{}
|
||||||
|
|
||||||
func NewSqlRepo() repository.DatabaseDriverRepo {
|
func NewSqlRepo() repository.DatabaseDriverRepo {
|
||||||
return &sqlDriverRepo{}
|
return &sqlDriverRepo{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// func NewSqlcRepo() repository.DatabaseDriverRepo {
|
func NewSqlcRepo() repository.DatabaseDriverRepo {
|
||||||
// return &sqlcDriverRepo{}
|
return &sqlcDriverRepo{}
|
||||||
// }
|
}
|
||||||
|
48
internal/repository/driverrepo/sqlcdriver.go
Normal file
48
internal/repository/driverrepo/sqlcdriver.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package driverrepo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"go-udemy-web-1/internal/repository"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *sqlcDriverRepo) ConnectSQL(dsn string) (*repository.DB, error) {
|
||||||
|
ctx := context.Background()
|
||||||
|
c, err := s.newConn(ctx, dsn)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dbConn.PG = c
|
||||||
|
|
||||||
|
err = s.testDB(ctx, c)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return dbConn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// testDB tries to ping the database
|
||||||
|
func (s *sqlcDriverRepo) testDB(ctx context.Context, c *pgx.Conn) error {
|
||||||
|
err := c.Ping(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// newDatabase creates a new database for the application
|
||||||
|
func (s *sqlcDriverRepo) newConn(ctx context.Context, dsn string) (*pgx.Conn, error) {
|
||||||
|
conn, err := pgx.Connect(ctx, dsn)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ping test the conn
|
||||||
|
if err = conn.Ping(ctx); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn, err
|
||||||
|
}
|
@ -10,8 +10,6 @@ import (
|
|||||||
_ "github.com/jackc/pgx/v5/stdlib"
|
_ "github.com/jackc/pgx/v5/stdlib"
|
||||||
)
|
)
|
||||||
|
|
||||||
var dbConn = &repository.DB{}
|
|
||||||
|
|
||||||
const (
|
const (
|
||||||
maxOpenDbConn = 10
|
maxOpenDbConn = 10
|
||||||
maxIdleDbConn = 5
|
maxIdleDbConn = 5
|
||||||
@ -20,7 +18,7 @@ const (
|
|||||||
|
|
||||||
// ConnectSQL creates SQL pool for Postgres
|
// ConnectSQL creates SQL pool for Postgres
|
||||||
func (s *sqlDriverRepo) ConnectSQL(dsn string) (*repository.DB, error) {
|
func (s *sqlDriverRepo) ConnectSQL(dsn string) (*repository.DB, error) {
|
||||||
d, err := newDatabase(dsn)
|
d, err := s.newDatabase(dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -31,7 +29,7 @@ func (s *sqlDriverRepo) ConnectSQL(dsn string) (*repository.DB, error) {
|
|||||||
|
|
||||||
dbConn.SQL = d
|
dbConn.SQL = d
|
||||||
|
|
||||||
err = testDB(d)
|
err = s.testDB(d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -39,7 +37,7 @@ func (s *sqlDriverRepo) ConnectSQL(dsn string) (*repository.DB, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// testDB tries to ping the database
|
// testDB tries to ping the database
|
||||||
func testDB(d *sql.DB) error {
|
func (s *sqlDriverRepo) testDB(d *sql.DB) error {
|
||||||
err := d.Ping()
|
err := d.Ping()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -48,7 +46,7 @@ func testDB(d *sql.DB) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// newDatabase creates a new database for the application
|
// newDatabase creates a new database for the application
|
||||||
func newDatabase(dsn string) (*sql.DB, error) {
|
func (s *sqlDriverRepo) newDatabase(dsn string) (*sql.DB, error) {
|
||||||
db, err := sql.Open("pgx", dsn)
|
db, err := sql.Open("pgx", dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -292,8 +292,8 @@ WHERE
|
|||||||
`
|
`
|
||||||
|
|
||||||
type GetRestrictionsForRoomByDateParams struct {
|
type GetRestrictionsForRoomByDateParams struct {
|
||||||
EndDate pgtype.Date
|
|
||||||
StartDate pgtype.Date
|
StartDate pgtype.Date
|
||||||
|
EndDate pgtype.Date
|
||||||
RoomID int32
|
RoomID int32
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -307,7 +307,7 @@ type GetRestrictionsForRoomByDateRow struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) GetRestrictionsForRoomByDate(ctx context.Context, arg GetRestrictionsForRoomByDateParams) ([]GetRestrictionsForRoomByDateRow, error) {
|
func (q *Queries) GetRestrictionsForRoomByDate(ctx context.Context, arg GetRestrictionsForRoomByDateParams) ([]GetRestrictionsForRoomByDateRow, error) {
|
||||||
rows, err := q.db.Query(ctx, getRestrictionsForRoomByDate, arg.EndDate, arg.StartDate, arg.RoomID)
|
rows, err := q.db.Query(ctx, getRestrictionsForRoomByDate, arg.StartDate, arg.EndDate, arg.RoomID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -211,9 +211,9 @@ SELECT
|
|||||||
FROM
|
FROM
|
||||||
room_restrictions
|
room_restrictions
|
||||||
WHERE
|
WHERE
|
||||||
$1 < end_date
|
@start_date < end_date
|
||||||
AND $2 >= start_date
|
AND @end_date >= start_date
|
||||||
AND room_id = $3;
|
AND room_id = @room_id;
|
||||||
|
|
||||||
-- name: InsertBlockForRoom :exec
|
-- name: InsertBlockForRoom :exec
|
||||||
INSERT INTO
|
INSERT INTO
|
||||||
|
Loading…
Reference in New Issue
Block a user