-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcast_vote.go
65 lines (53 loc) · 1.68 KB
/
cast_vote.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
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"
)
// CastVote casts a ballot for a given ElectionID. RankedProposalIDs contains the
// ranked candidates in order of preference: first, second, third and so forth. If your
// first choice doesn’t have a chance to win, your ballot counts for your next choice.
type CastVote struct {
VoteID string
ElectionID string
UserID string
RankedProposalIDs []string
}
type castVoteHandler struct {
repository electionrepository.Repository
clock clock.Clock
}
func NewCastVoteHandler(repository electionrepository.Repository, clock clock.Clock) *castVoteHandler {
return &castVoteHandler{
repository: repository,
clock: clock,
}
}
func (h *castVoteHandler) On(ctx context.Context, cmd CastVote, eventRaiser cqrs.EventRaiser) error {
ctx, span := tracer.Start(ctx, "vote.cast-vote")
defer span.End()
occurredAt := int(h.clock.Now().Unix())
sleep.Rand(2 * time.Millisecond)
err := h.repository.SaveVote(ctx, electionrepository.Vote{
VoteID: cmd.VoteID,
ElectionID: cmd.ElectionID,
UserID: cmd.UserID,
RankedProposalIDs: append([]string{}, cmd.RankedProposalIDs...),
SubmittedAt: occurredAt,
})
if err != nil {
return err
}
eventRaiser.Raise(event.VoteWasCast{
VoteID: cmd.VoteID,
ElectionID: cmd.ElectionID,
UserID: cmd.UserID,
RankedProposalIDs: append([]string{}, cmd.RankedProposalIDs...),
OccurredAt: occurredAt,
})
return nil
}