• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

looplab / eventhorizon / 12622512440

05 Jan 2025 07:58PM UTC coverage: 27.495% (-39.9%) from 67.361%
12622512440

Pull #419

github

web-flow
Merge b3c17d928 into ac3a97277
Pull Request #419: fix(ci): update to up/download-artifact v4

1769 of 6434 relevant lines covered (27.49%)

1.41 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/commandhandler/aggregate/commandhandler.go
1
// Copyright (c) 2014 - The Event Horizon authors.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
package aggregate
16

17
import (
18
        "context"
19
        "errors"
20

21
        eh "github.com/looplab/eventhorizon"
22
)
23

24
// ErrNilAggregateStore is when a dispatcher is created with a nil aggregate store.
25
var ErrNilAggregateStore = errors.New("aggregate store is nil")
26

27
// CommandHandler dispatches commands to an aggregate.
28
//
29
// The dispatch process is as follows:
30
// 1. The handler receives a command.
31
// 2. An aggregate is created or loaded using an aggregate store.
32
// 3. The aggregate's command handler is called.
33
// 4. The aggregate stores events in response to the command.
34
// 5. The new events are stored in the event store.
35
// 6. The events are published on the event bus after a successful store.
36
type CommandHandler struct {
37
        t     eh.AggregateType
38
        store eh.AggregateStore
39
}
40

41
// NewCommandHandler creates a new CommandHandler for an aggregate type.
42
func NewCommandHandler(t eh.AggregateType, store eh.AggregateStore) (*CommandHandler, error) {
×
43
        if store == nil {
×
44
                return nil, ErrNilAggregateStore
×
45
        }
×
46

47
        h := &CommandHandler{
×
48
                t:     t,
×
49
                store: store,
×
50
        }
×
51

×
52
        return h, nil
×
53
}
54

55
// HandleCommand handles a command with the registered aggregate.
56
// Returns ErrAggregateNotFound if no aggregate could be found.
57
func (h *CommandHandler) HandleCommand(ctx context.Context, cmd eh.Command) error {
×
58
        select {
×
59
        case <-ctx.Done():
×
60
                return ctx.Err()
×
61
        default:
×
62
        }
63

64
        if err := eh.CheckCommand(cmd); err != nil {
×
65
                return err
×
66
        }
×
67

68
        a, err := h.store.Load(ctx, h.t, cmd.AggregateID())
×
69
        if err != nil {
×
70
                return err
×
71
        } else if a == nil {
×
72
                return eh.ErrAggregateNotFound
×
73
        }
×
74

75
        if err = a.HandleCommand(ctx, cmd); err != nil {
×
76
                return &eh.AggregateError{Err: err}
×
77
        }
×
78

79
        return h.store.Save(ctx, a)
×
80
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc