-
Notifications
You must be signed in to change notification settings - Fork 3
/
sprint.go
295 lines (243 loc) · 7.85 KB
/
sprint.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package v1
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
retroModels "github.com/iReflect/reflect-app/apps/retrospective/models"
retroSerializers "github.com/iReflect/reflect-app/apps/retrospective/serializers"
retrospectiveServices "github.com/iReflect/reflect-app/apps/retrospective/services"
"github.com/iReflect/reflect-app/constants"
)
// SprintController ...
type SprintController struct {
SprintService retrospectiveServices.SprintService
PermissionService retrospectiveServices.PermissionService
TrailService retrospectiveServices.TrailService
}
// Routes for Sprints
func (ctrl SprintController) Routes(r *gin.RouterGroup) {
r.GET("/", ctrl.List)
r.POST("/", ctrl.Create)
r.GET("/:sprintID/", ctrl.Get)
r.DELETE("/:sprintID/", ctrl.Delete)
r.PUT("/:sprintID/", ctrl.Update)
r.POST("/:sprintID/activate/", ctrl.ActivateSprint)
r.POST("/:sprintID/freeze/", ctrl.FreezeSprint)
r.POST("/:sprintID/process/", ctrl.Process)
r.GET("/:sprintID/member-summary/", ctrl.GetSprintMemberSummary)
r.GET("/:sprintID/process_history/", ctrl.GetTrails)
}
// List the sprints accessible to the user
func (ctrl SprintController) List(c *gin.Context) {
userID, _ := c.Get("userID")
retroID := c.Param("retroID")
after, _ := c.GetQuery("after")
perPage, _ := c.GetQuery("count")
perPageInt, err := strconv.Atoi(perPage)
if err != nil || perPageInt < 0 {
perPageInt = 20
}
if !ctrl.PermissionService.UserCanAccessRetro(retroID, userID.(uint)) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{})
return
}
sprints, status, err := ctrl.SprintService.GetSprintsList(retroID, userID.(uint), perPageInt, after)
if err != nil {
c.AbortWithStatusJSON(status, gin.H{"error": err.Error()})
return
}
c.JSON(status, sprints)
}
// Create a new draft sprint for the retro
func (ctrl SprintController) Create(c *gin.Context) {
userID, _ := c.Get("userID")
retroID := c.Param("retroID")
if !ctrl.PermissionService.UserCanAccessRetro(retroID, userID.(uint)) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{})
return
}
sprintData := retroSerializers.CreateSprintSerializer{CreatedByID: userID.(uint)}
err := c.BindJSON(&sprintData)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "invalid request data"})
return
}
sprint, status, err := ctrl.SprintService.Create(retroID, userID.(uint), sprintData)
if err != nil {
c.AbortWithStatusJSON(status, gin.H{"error": err.Error()})
return
}
ctrl.TrailService.Add(
constants.CreatedSprint,
constants.Sprint,
strconv.Itoa(int(sprint.ID)),
userID.(uint))
c.JSON(http.StatusCreated, sprint)
}
// Get Sprint Data
func (ctrl SprintController) Get(c *gin.Context) {
userID, _ := c.Get("userID")
sprintID := c.Param("sprintID")
retroID := c.Param("retroID")
if !ctrl.PermissionService.UserCanAccessSprint(retroID, sprintID, userID.(uint)) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{})
return
}
sprint, status, err := ctrl.SprintService.Get(sprintID, userID.(uint), true)
if err != nil {
c.AbortWithStatusJSON(status, gin.H{"error": err.Error()})
return
}
c.JSON(status, sprint)
}
// Delete Sprint
func (ctrl SprintController) Delete(c *gin.Context) {
userID, _ := c.Get("userID")
sprintID := c.Param("sprintID")
retroID := c.Param("retroID")
if !ctrl.PermissionService.UserCanEditSprint(retroID, sprintID, userID.(uint)) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{})
return
}
status, err := ctrl.SprintService.DeleteSprint(sprintID)
if err != nil {
c.AbortWithStatusJSON(status, gin.H{"error": err.Error()})
return
}
ctrl.TrailService.Add(
constants.DeletedSprint,
constants.Sprint,
sprintID,
userID.(uint))
c.JSON(status, nil)
}
// Update a sprint
func (ctrl SprintController) Update(c *gin.Context) {
userID, _ := c.Get("userID")
sprintID := c.Param("sprintID")
retroID := c.Param("retroID")
if !ctrl.PermissionService.UserCanEditSprint(retroID, sprintID, userID.(uint)) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{})
return
}
var sprintData retroSerializers.UpdateSprintSerializer
if err := c.BindJSON(&sprintData); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "invalid request data"})
return
}
response, status, err := ctrl.SprintService.UpdateSprint(sprintID, userID.(uint), sprintData)
if err != nil {
c.AbortWithStatusJSON(status, gin.H{"error": err.Error()})
return
}
ctrl.TrailService.Add(
constants.UpdatedSprint,
constants.Sprint,
sprintID,
userID.(uint))
c.JSON(status, response)
}
// ActivateSprint activates the given sprint
func (ctrl SprintController) ActivateSprint(c *gin.Context) {
userID, _ := c.Get("userID")
sprintID := c.Param("sprintID")
retroID := c.Param("retroID")
if !ctrl.PermissionService.UserCanEditSprint(retroID, sprintID, userID.(uint)) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{})
return
}
status, err := ctrl.SprintService.ActivateSprint(sprintID, retroID)
if err != nil {
c.AbortWithStatusJSON(status, gin.H{"error": err.Error()})
return
}
ctrl.TrailService.Add(
constants.ActivatedSprint,
constants.Sprint,
sprintID,
userID.(uint))
c.JSON(status, nil)
}
// FreezeSprint freezes the given sprint
func (ctrl SprintController) FreezeSprint(c *gin.Context) {
userID, _ := c.Get("userID")
sprintID := c.Param("sprintID")
retroID := c.Param("retroID")
if !ctrl.PermissionService.UserCanEditSprint(retroID, sprintID, userID.(uint)) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{})
return
}
status, err := ctrl.SprintService.FreezeSprint(sprintID, retroID)
if err != nil {
c.AbortWithStatusJSON(status, gin.H{"error": err.Error()})
return
}
ctrl.TrailService.Add(
constants.FreezeSprint,
constants.Sprint,
sprintID,
userID.(uint))
c.JSON(status, nil)
}
// Process Sprint
func (ctrl SprintController) Process(c *gin.Context) {
userID, _ := c.Get("userID")
sprintID := c.Param("sprintID")
retroID := c.Param("retroID")
if !ctrl.PermissionService.UserCanAccessSprint(retroID, sprintID, userID.(uint)) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{})
return
}
sprintIDInt, err := strconv.Atoi(sprintID)
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "invalid sprint"})
}
sprint, _, err := ctrl.SprintService.Get(sprintID, userID.(uint), false)
if err != nil {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": "sprint not found"})
}
ctrl.SprintService.QueueSprint(uint(sprintIDInt), sprint.Status == retroModels.ActiveSprint)
ctrl.TrailService.Add(
constants.TriggeredSprintRefresh,
constants.Sprint,
sprintID,
userID.(uint))
c.JSON(http.StatusNoContent, nil)
}
// GetSprintMemberSummary returns the sprint member summary list
func (ctrl SprintController) GetSprintMemberSummary(c *gin.Context) {
userID, _ := c.Get("userID")
sprintID := c.Param("sprintID")
retroID := c.Param("retroID")
if !ctrl.PermissionService.UserCanAccessSprint(retroID, sprintID, userID.(uint)) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{})
return
}
response, status, err := ctrl.SprintService.GetSprintMembersSummary(sprintID)
if err != nil {
c.AbortWithStatusJSON(status, gin.H{"error": err.Error()})
return
}
c.JSON(status, response)
}
// GetTrails is method to get the all trails related to a particular sprint
func (ctrl SprintController) GetTrails(c *gin.Context) {
sprintID := c.Param("sprintID")
retroID := c.Param("retroID")
userID, _ := c.Get("userID")
sprintIDInt, errConversion := strconv.Atoi(sprintID)
if errConversion != nil {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{})
return
}
if !ctrl.PermissionService.UserCanAccessSprint(retroID, sprintID, userID.(uint)) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{})
return
}
trails, status, err := ctrl.TrailService.GetTrails(uint(sprintIDInt), retroID)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{})
return
}
c.JSON(status, trails)
}