-
Notifications
You must be signed in to change notification settings - Fork 0
/
commence_election.go
60 lines (49 loc) · 1.45 KB
/
commence_election.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
package election
import (
"context"
"time"
"github.com/inklabs/cqrs"
"github.com/inklabs/cqrs/pkg/clock"
"github.com/inklabs/vote/event"
"github.com/inklabs/vote/internal/electionrepository"
"github.com/inklabs/vote/pkg/sleep"
)
// CommenceElection instantiates a new open election that is ready for proposals and voting.
type CommenceElection struct {
ElectionID string
OrganizerUserID string
Name string
Description string
}
type commenceElectionHandler struct {
repository electionrepository.Repository
clock clock.Clock
}
func NewCommenceElectionHandler(repository electionrepository.Repository, clock clock.Clock) *commenceElectionHandler {
return &commenceElectionHandler{
repository: repository,
clock: clock,
}
}
func (h *commenceElectionHandler) On(ctx context.Context, cmd CommenceElection, eventRaiser cqrs.EventRaiser) error {
occurredAt := int(h.clock.Now().Unix())
sleep.Rand(2 * time.Millisecond)
err := h.repository.SaveElection(ctx, electionrepository.Election{
ElectionID: cmd.ElectionID,
OrganizerUserID: cmd.OrganizerUserID,
Name: cmd.Name,
Description: cmd.Description,
CommencedAt: occurredAt,
})
if err != nil {
return err
}
eventRaiser.Raise(event.ElectionHasCommenced{
ElectionID: cmd.ElectionID,
OrganizerUserID: cmd.OrganizerUserID,
Name: cmd.Name,
Description: cmd.Description,
OccurredAt: occurredAt,
})
return nil
}