-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvotings.go
149 lines (118 loc) · 4.24 KB
/
votings.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2022, Direct Decisions Go client AUTHORS.
// All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package directdecisions
import (
"context"
"net/http"
"net/url"
)
// VotingsService provides information and methods to manage votings and ballots.
type VotingsService service
// Voting holds information about votings and ballots.
type Voting struct {
ID string `json:"id"`
Choices []string `json:"choices,omitempty"`
}
// Voting returns a specific voting referenced by its ID.
func (s *VotingsService) Voting(ctx context.Context, votingID string) (v *Voting, err error) {
err = s.client.request(ctx, http.MethodGet, "v1/votings/"+url.PathEscape(votingID), nil, &v)
return v, err
}
// Create adds a new voting with a provided choices.
func (s *VotingsService) Create(ctx context.Context, choices []string) (v *Voting, err error) {
type createVotingRequest struct {
Choices []string `json:"choices"`
}
err = s.client.request(ctx, http.MethodPost, "v1/votings", createVotingRequest{
Choices: choices,
}, &v)
return v, err
}
// Set adds, moves or removes a choice in a voting.
func (s *VotingsService) Set(ctx context.Context, votingID, choice string, index int) (choices []string, err error) {
type setChoiceRequest struct {
Choice string `json:"choice"`
Index int `json:"index"`
}
type setChoiceResponse struct {
Choices []string `json:"choices"`
}
var response *setChoiceResponse
if err = s.client.request(ctx, http.MethodPost, "v1/votings/"+url.PathEscape(votingID)+"/choices", setChoiceRequest{
Choice: choice,
Index: index,
}, &response); err != nil {
return nil, err
}
return response.Choices, nil
}
// Delete removes a voting referenced by its ID.
func (s *VotingsService) Delete(ctx context.Context, votingID string) (err error) {
return s.client.request(ctx, http.MethodDelete, "v1/votings/"+url.PathEscape(votingID), nil, nil)
}
func (s *VotingsService) Ballot(ctx context.Context, votingID, voterID string) (ballot map[string]int, err error) {
type ballotResponse struct {
Ballot map[string]int `json:"ballot"`
}
var response *ballotResponse
if err = s.client.request(ctx, http.MethodGet, "v1/votings/"+url.PathEscape(votingID)+"/ballots/"+url.PathEscape(voterID), nil, &response); err != nil {
return nil, err
}
return response.Ballot, nil
}
func (s *VotingsService) Vote(ctx context.Context, votingID, voterID string, ballot map[string]int) (revoted bool, err error) {
type voteRequest struct {
Ballot map[string]int `json:"ballot"`
}
type voteResponse struct {
Revoted bool `json:"revoted"`
}
var response *voteResponse
if err = s.client.request(ctx, http.MethodPost, "v1/votings/"+url.PathEscape(votingID)+"/ballots/"+url.PathEscape(voterID), voteRequest{
Ballot: ballot,
}, &response); err != nil {
return false, err
}
return response.Revoted, nil
}
func (s *VotingsService) Unvote(ctx context.Context, votingID, voterID string) error {
return s.client.request(ctx, http.MethodDelete, "v1/votings/"+url.PathEscape(votingID)+"/ballots/"+url.PathEscape(voterID), nil, nil)
}
type Result struct {
Choice string
Index int
Wins int
Percentage float64
Strength int
Advantage int
}
type computeResultsAPIResponse struct {
Results []Result `json:"results"`
Tie bool `json:"tie"`
Duels []Duel `json:"duels"`
}
func (s *VotingsService) Results(ctx context.Context, votingID string) (results []Result, tie bool, err error) {
var response *computeResultsAPIResponse
if err = s.client.request(ctx, http.MethodGet, "v1/votings/"+url.PathEscape(votingID)+"/results", nil, &response); err != nil {
return nil, false, err
}
return response.Results, response.Tie, nil
}
type Duel struct {
Left ChoiceStrength
Right ChoiceStrength
}
type ChoiceStrength struct {
Choice string
Index int
Strength int
}
func (s *VotingsService) Duels(ctx context.Context, votingID string) (results []Result, duels []Duel, tie bool, err error) {
var response *computeResultsAPIResponse
if err = s.client.request(ctx, http.MethodGet, "v1/votings/"+url.PathEscape(votingID)+"/results/duels", nil, &response); err != nil {
return nil, nil, false, err
}
return response.Results, response.Duels, response.Tie, nil
}