forked from jsteenb2/mess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_hours_available.go
52 lines (43 loc) · 1.29 KB
/
make_hours_available.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
package command
import (
"context"
"time"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/decorator"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/errors"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/trainer/domain/hour"
"github.com/sirupsen/logrus"
)
type MakeHoursAvailable struct {
Hours []time.Time
}
type MakeHoursAvailableHandler decorator.CommandHandler[MakeHoursAvailable]
type makeHoursAvailableHandler struct {
hourRepo hour.Repository
}
func NewMakeHoursAvailableHandler(
hourRepo hour.Repository,
logger *logrus.Entry,
metricsClient decorator.MetricsClient,
) MakeHoursAvailableHandler {
if hourRepo == nil {
panic("hourRepo is nil")
}
return decorator.ApplyCommandDecorators[MakeHoursAvailable](
makeHoursAvailableHandler{hourRepo: hourRepo},
logger,
metricsClient,
)
}
func (c makeHoursAvailableHandler) Handle(ctx context.Context, cmd MakeHoursAvailable) error {
for _, hourToUpdate := range cmd.Hours {
if err := c.hourRepo.UpdateHour(ctx, hourToUpdate, func(h *hour.Hour) (*hour.Hour, error) {
if err := h.MakeAvailable(); err != nil {
return nil, err
}
return h, nil
}); err != nil {
return errors.NewSlugError(err.Error(), "unable-to-update-availability")
}
}
return nil
}