-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathvotings.go
More file actions
202 lines (176 loc) · 5.97 KB
/
Copy pathvotings.go
File metadata and controls
202 lines (176 loc) · 5.97 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package api
import (
"fmt"
"net/http"
"go.opentelemetry.io/otel/codes"
"scrumlr.io/server/common"
"scrumlr.io/server/identifiers"
"scrumlr.io/server/logger"
"scrumlr.io/server/votings"
"github.com/go-chi/render"
"github.com/google/uuid"
)
//var tracer trace.Tracer = otel.Tracer("scrumlr.io/server/api")
// Create a new voting on a board
//
// @Summary Create a new voting on a board
// @Description Create a new voting on a board
// @Tags votings
// @Accept json
// @Param Cookie header string true "jwt token to authenticate"
// @Param boardId path string true "id of the board"
// @Param voting body votings.VotingCreateRequest true "voting to create"
// @Produce json
// @Header 201 {string} Location "Path to the created voting"
// @Success 201 {object} votings.Voting
// @Failure 400 {object} common.APIError
// @Failure 403 {object} common.APIError
// @Failure 404 {object} common.APIError
// @Failure 500 {object} common.APIError
// @Router /boards/{boardId}/votings [post]
func (s *Server) createVoting(w http.ResponseWriter, r *http.Request) {
ctx, span := tracer.Start(r.Context(), "scrumlr.votings.api.create")
defer span.End()
log := logger.FromContext(ctx)
board := ctx.Value(identifiers.BoardIdentifier).(uuid.UUID)
var body votings.VotingCreateRequest
if err := render.Decode(r, &body); err != nil {
span.SetStatus(codes.Error, "unable to decode body")
span.RecordError(err)
log.Errorw("Unable to decode body", "err", err)
common.Throw(w, r, common.BadRequestError(err))
return
}
body.Board = board
voting, err := s.votings.Create(ctx, body)
if err != nil {
span.SetStatus(codes.Error, "failed to create voting")
span.RecordError(err)
common.Throw(w, r, mapError(err))
return
}
w.Header().Set("Location", s.buildRelativeURL(fmt.Sprintf("/boards/%s/votings/%s", board, voting.ID)))
render.Status(r, http.StatusCreated)
render.Respond(w, r, voting)
}
// Update a voting on a board to closed
//
// @Summary Update a voting on a board to closed
// @Description Update a voting on a board to closed
// @Tags votings
// @Accept json
// @Param Cookie header string true "jwt token to authenticate"
// @Param boardId path string true "id of the board"
// @Param id path string true "id of the voting"
// @Produce json
// @Success 200 {object} votings.Voting
// @Failure 400 {object} common.APIError
// @Failure 403 {object} common.APIError
// @Failure 404 {object} common.APIError
// @Failure 500 {object} common.APIError
// @Router /boards/{boardId}/votings/{id} [put]
func (s *Server) updateVoting(w http.ResponseWriter, r *http.Request) {
ctx, span := tracer.Start(r.Context(), "scrumlr.votings.api.update")
defer span.End()
board := ctx.Value(identifiers.BoardIdentifier).(uuid.UUID)
id := ctx.Value(identifiers.VotingIdentifier).(uuid.UUID)
var body votings.VotingCloseRequest
if err := render.Decode(r, &body); err != nil {
span.SetStatus(codes.Error, "unable to decode body")
span.RecordError(err)
common.Throw(w, r, common.BadRequestError(err))
return
}
body.ID = id
body.Board = board
notes, err := s.notes.GetAll(ctx, board)
if err != nil {
span.SetStatus(codes.Error, "failed to get notes")
span.RecordError(err)
common.Throw(w, r, mapError(err))
return
}
var affectedNotes []votings.Note
for _, note := range notes {
affectedNotes = append(affectedNotes, votings.Note{
ID: note.ID,
Author: note.Author,
Text: note.Text,
Edited: note.Edited,
Position: votings.NotePosition{
Column: note.Position.Column,
Stack: note.Position.Stack,
Rank: note.Position.Rank,
},
})
}
voting, err := s.votings.Update(ctx, id, board, affectedNotes, body.Status)
if err != nil {
span.SetStatus(codes.Error, "failed to update voting")
span.RecordError(err)
common.Throw(w, r, mapError(err))
return
}
render.Status(r, http.StatusOK)
render.Respond(w, r, voting)
}
// Get a voting on a board
//
// @Summary Get a voting on a board
// @Description Get a voting on a board
// @Tags votings
// @Accept json
// @Param Cookie header string true "jwt token to authenticate"
// @Param boardId path string true "id of the board"
// @Param id path string true "id of the voting"
// @Produce json
// @Success 200 {object} votings.Voting
// @Failure 400 {object} common.APIError
// @Failure 403 {object} common.APIError
// @Failure 404 {object} common.APIError
// @Failure 500 {object} common.APIError
// @Router /boards/{boardId}/votings/{id} [get]
func (s *Server) getVoting(w http.ResponseWriter, r *http.Request) {
ctx, span := tracer.Start(r.Context(), "scrumlr.votings.api.get")
defer span.End()
board := ctx.Value(identifiers.BoardIdentifier).(uuid.UUID)
id := ctx.Value(identifiers.VotingIdentifier).(uuid.UUID)
voting, err := s.votings.Get(ctx, board, id)
if err != nil {
span.SetStatus(codes.Error, "failed to get voting")
span.RecordError(err)
common.Throw(w, r, mapError(err))
return
}
render.Status(r, http.StatusOK)
render.Respond(w, r, voting)
}
// Get all votings on a board
//
// @Summary Get all votings on a board
// @Description Get all votings on a board
// @Tags votings
// @Accept json
// @Param Cookie header string true "jwt token to authenticate"
// @Param boardId path string true "id of the board"
// @Produce json
// @Success 200 {object} []votings.Voting
// @Failure 400 {object} common.APIError
// @Failure 403 {object} common.APIError
// @Failure 404 {object} common.APIError
// @Failure 500 {object} common.APIError
// @Router /boards/{boardId}/votings [get]
func (s *Server) getVotings(w http.ResponseWriter, r *http.Request) {
ctx, span := tracer.Start(r.Context(), "scrumlr.votings.api.update")
defer span.End()
board := ctx.Value(identifiers.BoardIdentifier).(uuid.UUID)
votings, err := s.votings.GetAll(ctx, board)
if err != nil {
span.SetStatus(codes.Error, "failed to get votings")
span.RecordError(err)
common.Throw(w, r, mapError(err))
return
}
render.Status(r, http.StatusOK)
render.Respond(w, r, votings)
}