forked from jsteenb2/mess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make_hours_unavailable.go
52 lines (43 loc) · 1.31 KB
/
make_hours_unavailable.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 MakeHoursUnavailable struct {
Hours []time.Time
}
type MakeHoursUnavailableHandler decorator.CommandHandler[MakeHoursUnavailable]
type makeHoursUnavailableHandler struct {
hourRepo hour.Repository
}
func NewMakeHoursUnavailableHandler(
hourRepo hour.Repository,
logger *logrus.Entry,
metricsClient decorator.MetricsClient,
) MakeHoursUnavailableHandler {
if hourRepo == nil {
panic("hourRepo is nil")
}
return decorator.ApplyCommandDecorators[MakeHoursUnavailable](
makeHoursUnavailableHandler{hourRepo: hourRepo},
logger,
metricsClient,
)
}
func (c makeHoursUnavailableHandler) Handle(ctx context.Context, cmd MakeHoursUnavailable) error {
for _, hourToUpdate := range cmd.Hours {
if err := c.hourRepo.UpdateHour(ctx, hourToUpdate, func(h *hour.Hour) (*hour.Hour, error) {
if err := h.MakeNotAvailable(); err != nil {
return nil, err
}
return h, nil
}); err != nil {
return errors.NewSlugError(err.Error(), "unable-to-update-availability")
}
}
return nil
}