Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 10 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ go 1.22.6
require (
github.com/gin-gonic/gin v1.10.0
github.com/google/uuid v1.6.0
github.com/jackc/pgtype v1.14.0
github.com/jackc/pgx/v4 v4.18.3
github.com/jackc/pgconn v1.14.3
github.com/jackc/pgtype v1.14.4
github.com/jackc/pgx/v5 v5.7.1
)

require (
Expand All @@ -21,26 +22,27 @@ require (
github.com/go-playground/validator/v10 v10.20.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle v1.3.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/protobuf v1.34.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
68 changes: 51 additions & 17 deletions go.sum

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions internal/hbooking/repository/create_booking.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"github.com/google/uuid"
"github.com/jackc/pgconn"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"

"github.com/spatecon/hbooking/internal/hbooking/domain"
)

type Booking struct {
ID uuid.UUID
BookingID uuid.UUID
WorkshopID int64
ClientID string
BeginAt pgtype.Timestamp
Expand All @@ -25,7 +25,7 @@ type Booking struct {

func ConvertToPGBooking(b *domain.Booking) *Booking {
return &Booking{
ID: b.ID,
BookingID: b.ID,
WorkshopID: b.WorkshopID,
ClientID: b.ClientID,
BeginAt: pgtype.Timestamp{Time: b.BeginAt.UTC(), Status: pgtype.Present},
Expand All @@ -41,7 +41,7 @@ func ConvertFromPGBooking(b *Booking) (*domain.Booking, error) {
}

return &domain.Booking{
ID: b.ID,
ID: b.BookingID,
WorkshopID: b.WorkshopID,
ClientID: b.ClientID,
BeginAt: b.BeginAt.Time.In(tz),
Expand Down Expand Up @@ -134,7 +134,7 @@ func (r *Repository) CreateBooking(ctx context.Context, booking *domain.Booking)
pgBooking.EndAt,
pgBooking.ClientID,
pgBooking.ClientTimezone,
).Scan(&pgBooking.ID)
).Scan(&pgBooking.BookingID)
if err != nil {
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) && pgErr.ConstraintName == "workshop_bookings_workshop_id_begin_at_end_at_overlap" {
Expand Down
34 changes: 11 additions & 23 deletions internal/hbooking/repository/list_bookings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"fmt"

"github.com/jackc/pgx/v5"

"github.com/spatecon/hbooking/internal/hbooking/domain"
)

Expand All @@ -18,33 +20,19 @@ func (r *Repository) ListBookings(ctx context.Context, workshopID int64) ([]*dom
if err != nil {
return nil, err
}
defer rows.Close()

bookings := make([]*domain.Booking, 0)
for rows.Next() {
var (
b Booking
booking *domain.Booking
)
err = rows.Scan(
&b.ID,
&b.WorkshopID,
&b.ClientID,
&b.BeginAt,
&b.EndAt,
&b.ClientTimezone,
)
if err != nil {
return nil, fmt.Errorf("failed to scan booking: %w", err)
}

booking, err = ConvertFromPGBooking(&b)
bookings, err := pgx.CollectRows(rows, pgx.RowToStructByName[Booking])
if err != nil {
return nil, fmt.Errorf("failed to collect rows booking: %w", err)
}

bookingsDomain := make([]*domain.Booking, len(bookings))
for i, b := range bookings {
bookingsDomain[i], err = ConvertFromPGBooking(&b)
if err != nil {
return nil, fmt.Errorf("failed to convert booking: %w", err)
}

bookings = append(bookings, booking)
}

return bookings, nil
return bookingsDomain, nil
}
2 changes: 1 addition & 1 deletion internal/hbooking/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package repository
import (
"log/slog"

"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v5/pgxpool"
)

type Repository struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os/signal"

"github.com/gin-gonic/gin"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v5/pgxpool"

"github.com/spatecon/hbooking/internal/hbooking/handlers"
"github.com/spatecon/hbooking/internal/hbooking/repository"
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/app/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"time"

"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v5/pgxpool"
)

func (a *App) initDBConn() error {
Expand All @@ -22,7 +22,7 @@ func (a *App) initDBConn() error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

db, err := pgxpool.ConnectConfig(ctx, config)
db, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
return err
}
Expand Down