-
Notifications
You must be signed in to change notification settings - Fork 14
/
goal.go
127 lines (114 loc) · 3.83 KB
/
goal.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright 2024 The Bucketeer Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package command
import (
"context"
pb "github.com/golang/protobuf/proto" // nolint:staticcheck
domainevent "github.com/bucketeer-io/bucketeer/pkg/domainevent/domain"
"github.com/bucketeer-io/bucketeer/pkg/experiment/domain"
"github.com/bucketeer-io/bucketeer/pkg/pubsub/publisher"
eventproto "github.com/bucketeer-io/bucketeer/proto/event/domain"
proto "github.com/bucketeer-io/bucketeer/proto/experiment"
)
type goalCommandHandler struct {
editor *eventproto.Editor
goal *domain.Goal
publisher publisher.Publisher
environmentNamespace string
}
func NewGoalCommandHandler(
editor *eventproto.Editor,
goal *domain.Goal,
p publisher.Publisher,
environmentNamespace string,
) Handler {
return &goalCommandHandler{
editor: editor,
goal: goal,
publisher: p,
environmentNamespace: environmentNamespace,
}
}
func (h *goalCommandHandler) Handle(ctx context.Context, cmd Command) error {
switch c := cmd.(type) {
case *proto.CreateGoalCommand:
return h.create(ctx, c)
case *proto.RenameGoalCommand:
return h.rename(ctx, c)
case *proto.ChangeDescriptionGoalCommand:
return h.changeDescription(ctx, c)
case *proto.ArchiveGoalCommand:
return h.archive(ctx, c)
case *proto.DeleteGoalCommand:
return h.delete(ctx, c)
default:
return ErrUnknownCommand
}
}
func (h *goalCommandHandler) create(ctx context.Context, cmd *proto.CreateGoalCommand) error {
return h.send(ctx, eventproto.Event_GOAL_CREATED, &eventproto.GoalCreatedEvent{
Id: h.goal.Id,
Name: h.goal.Name,
Description: h.goal.Description,
Deleted: h.goal.Deleted,
CreatedAt: h.goal.CreatedAt,
UpdatedAt: h.goal.UpdatedAt,
})
}
func (h *goalCommandHandler) rename(ctx context.Context, cmd *proto.RenameGoalCommand) error {
if err := h.goal.Rename(cmd.Name); err != nil {
return err
}
return h.send(ctx, eventproto.Event_GOAL_RENAMED, &eventproto.GoalRenamedEvent{
Id: h.goal.Id,
Name: cmd.Name,
})
}
func (h *goalCommandHandler) changeDescription(ctx context.Context, cmd *proto.ChangeDescriptionGoalCommand) error {
if err := h.goal.ChangeDescription(cmd.Description); err != nil {
return err
}
return h.send(ctx, eventproto.Event_GOAL_DESCRIPTION_CHANGED, &eventproto.GoalDescriptionChangedEvent{
Id: h.goal.Id,
Description: cmd.Description,
})
}
func (h *goalCommandHandler) archive(ctx context.Context, cmd *proto.ArchiveGoalCommand) error {
if err := h.goal.SetArchived(); err != nil {
return err
}
return h.send(ctx, eventproto.Event_GOAL_ARCHIVED, &eventproto.GoalArchivedEvent{
Id: h.goal.Id,
})
}
func (h *goalCommandHandler) delete(ctx context.Context, cmd *proto.DeleteGoalCommand) error {
if err := h.goal.SetDeleted(); err != nil {
return err
}
return h.send(ctx, eventproto.Event_GOAL_DELETED, &eventproto.GoalDeletedEvent{
Id: h.goal.Id,
})
}
func (h *goalCommandHandler) send(ctx context.Context, eventType eventproto.Event_Type, event pb.Message) error {
e, err := domainevent.NewEvent(h.editor, eventproto.Event_GOAL, h.goal.Id, eventType, event, h.environmentNamespace)
if err != nil {
return err
}
// TODO: more reliable
// TODO: add metrics
if err := h.publisher.Publish(ctx, e); err != nil {
return err
}
return nil
}