-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_proposal.go
63 lines (52 loc) · 1.41 KB
/
make_proposal.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
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"
)
// MakeProposal instantiates a new proposal for a given ElectionID.
type MakeProposal struct {
ElectionID string
ProposalID string
OwnerUserID string
Name string
Description string
}
type makeProposalHandler struct {
repository electionrepository.Repository
clock clock.Clock
}
func NewMakeProposalHandler(repository electionrepository.Repository, clock clock.Clock) *makeProposalHandler {
return &makeProposalHandler{
repository: repository,
clock: clock,
}
}
func (h *makeProposalHandler) On(ctx context.Context, cmd MakeProposal, eventRaiser cqrs.EventRaiser) error {
proposedAt := int(h.clock.Now().Unix())
sleep.Rand(2 * time.Millisecond)
err := h.repository.SaveProposal(ctx, electionrepository.Proposal{
ElectionID: cmd.ElectionID,
ProposalID: cmd.ProposalID,
OwnerUserID: cmd.OwnerUserID,
Name: cmd.Name,
Description: cmd.Description,
ProposedAt: proposedAt,
})
if err != nil {
return err
}
eventRaiser.Raise(event.ProposalWasMade{
ElectionID: cmd.ElectionID,
ProposalID: cmd.ProposalID,
OwnerUserID: cmd.OwnerUserID,
Name: cmd.Name,
Description: cmd.Description,
ProposedAt: proposedAt,
})
return nil
}