-
Notifications
You must be signed in to change notification settings - Fork 8
/
schedule_training.go
50 lines (41 loc) · 1.21 KB
/
schedule_training.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
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 ScheduleTraining struct {
Hour time.Time
}
type ScheduleTrainingHandler decorator.CommandHandler[ScheduleTraining]
type scheduleTrainingHandler struct {
hourRepo hour.Repository
}
func NewScheduleTrainingHandler(
hourRepo hour.Repository,
logger *logrus.Entry,
metricsClient decorator.MetricsClient,
) ScheduleTrainingHandler {
if hourRepo == nil {
panic("nil hourRepo")
}
return decorator.ApplyCommandDecorators[ScheduleTraining](
scheduleTrainingHandler{hourRepo: hourRepo},
logger,
metricsClient,
)
}
func (h scheduleTrainingHandler) Handle(ctx context.Context, cmd ScheduleTraining) error {
if err := h.hourRepo.UpdateHour(ctx, cmd.Hour, func(h *hour.Hour) (*hour.Hour, error) {
if err := h.ScheduleTraining(); err != nil {
return nil, err
}
return h, nil
}); err != nil {
return errors.NewSlugError(err.Error(), "unable-to-update-availability")
}
return nil
}