-
Notifications
You must be signed in to change notification settings - Fork 2
/
reservation_repository.go
59 lines (47 loc) · 1.44 KB
/
reservation_repository.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import (
"context"
"fmt"
)
type ReservationRepository interface {
CommitPrepared(ctx context.Context, txID string)
RollbackPrepared(ctx context.Context, txID string)
CreatePrepared(ctx context.Context, r *Reservation, txID string) error
}
type defaultReservationRepository struct {
db *DB
}
func NewReservationRepository(db *DB) ReservationRepository {
return &defaultReservationRepository{db: db}
}
func (repo *defaultReservationRepository) CreatePrepared(ctx context.Context, r *Reservation, txID string) error {
tx, err := repo.db.DB().BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("error begin statement %w", err)
}
query := `
INSERT INTO reservation (reservation_id, hotel_id, room_type_id, start_date, end_date, status)
VALUES ($1, $2, $3, $4, $5, $6);
`
args := []interface{}{
r.ReservationID,
r.HotelID,
r.RoomTypeID,
r.StartDate,
r.EndDate,
"pending_pay",
}
if _, err = tx.ExecContext(ctx, query, args...); err != nil {
return err
}
if _, err = tx.ExecContext(ctx, fmt.Sprintf("PREPARE TRANSACTION '%s';", txID)); err != nil {
return err
}
return nil
}
func (repo *defaultReservationRepository) CommitPrepared(ctx context.Context, txID string) {
repo.db.DB().ExecContext(ctx, fmt.Sprintf("COMMIT PREPARED '%s';", txID))
}
func (repo *defaultReservationRepository) RollbackPrepared(ctx context.Context, txID string) {
repo.db.DB().ExecContext(ctx, fmt.Sprintf("ROLLBACK PREPARED '%s';", txID))
}