-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathservice.go
40 lines (31 loc) · 1.13 KB
/
service.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
package main
import (
"context"
"github.com/google/uuid"
)
type Service interface {
CreateReservation(context.Context, *Reservation) error
}
type defaultService struct {
inventoryRepository InventoryRepository
reservationRepository ReservationRepository
}
func NewService(inventoryRepository InventoryRepository, reservationRepository ReservationRepository) Service {
return &defaultService{inventoryRepository: inventoryRepository, reservationRepository: reservationRepository}
}
func (d *defaultService) CreateReservation(ctx context.Context, reservation *Reservation) error {
inventoryTxID := uuid.NewString()
reservationTxID := uuid.NewString()
_, err := d.inventoryRepository.UpdatePrepared(ctx, reservation, inventoryTxID)
if err != nil {
d.inventoryRepository.RollbackPrepared(ctx, inventoryTxID)
return err
}
if err = d.reservationRepository.CreatePrepared(ctx, reservation, reservationTxID); err != nil {
d.inventoryRepository.RollbackPrepared(ctx, inventoryTxID)
return err
}
d.inventoryRepository.CommitPrepared(ctx, inventoryTxID)
d.reservationRepository.CommitPrepared(ctx, reservationTxID)
return nil
}