/
goals_v3.go
397 lines (349 loc) · 10.3 KB
/
goals_v3.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package controllers
import (
"net/http"
"github.com/envelope-zero/backend/v3/internal/types"
"github.com/envelope-zero/backend/v3/pkg/httperrors"
"github.com/envelope-zero/backend/v3/pkg/httputil"
"github.com/envelope-zero/backend/v3/pkg/models"
"github.com/gin-gonic/gin"
"golang.org/x/exp/slices"
)
func (co Controller) RegisterGoalRoutesV3(r *gin.RouterGroup) {
{
r.OPTIONS("", co.OptionsGoalsV3)
r.GET("", co.GetGoalsV3)
r.POST("", co.CreateGoalsV3)
}
{
r.OPTIONS("/:id", co.OptionsGoalDetailV3)
// FIMXE: These three
r.GET("/:id", co.GetGoalV3)
r.PATCH("/:id", co.UpdateGoalV3)
r.DELETE("/:id", co.DeleteGoalV3)
}
}
// @Summary Allowed HTTP verbs
// @Description Returns an empty response with the HTTP Header "allow" set to the allowed HTTP verbs
// @Tags Goals
// @Success 204
// @Router /v3/goals [options]
func (co Controller) OptionsGoalsV3(c *gin.Context) {
httputil.OptionsGetPost(c)
}
// @Summary Allowed HTTP verbs
// @Description Returns an empty response with the HTTP Header "allow" set to the allowed HTTP verbs
// @Tags Goals
// @Success 204
// @Failure 400 {object} httperrors.HTTPError
// @Failure 404 {object} httperrors.HTTPError
// @Failure 500 {object} httperrors.HTTPError
// @Param id path string true "ID formatted as string"
// @Router /v3/goals/{id} [options]
func (co Controller) OptionsGoalDetailV3(c *gin.Context) {
id, err := httputil.UUIDFromString(c.Param("id"))
if !err.Nil() {
c.JSON(err.Status, httperrors.HTTPError{
Error: err.Error(),
})
return
}
_, err = getResourceByID[models.Goal](c, co, id)
if !err.Nil() {
c.JSON(err.Status, httperrors.HTTPError{
Error: err.Error(),
})
return
}
httputil.OptionsGetPatchDelete(c)
}
// @Summary Create goals
// @Description Creates new goals
// @Tags Goals
// @Produce json
// @Success 201 {object} GoalCreateResponseV3
// @Failure 400 {object} GoalCreateResponseV3
// @Failure 404 {object} GoalCreateResponseV3
// @Failure 500 {object} GoalCreateResponseV3
// @Param goals body []GoalV3Editable true "Goals"
// @Router /v3/goals [post]
func (co Controller) CreateGoalsV3(c *gin.Context) {
var goals []GoalV3Editable
// Bind data and return error if not possible
err := httputil.BindData(c, &goals)
if !err.Nil() {
e := err.Error()
c.JSON(err.Status, GoalCreateResponseV3{
Error: &e,
})
return
}
// The final http status. Will be modified when errors occur
status := http.StatusCreated
r := GoalCreateResponseV3{}
for _, create := range goals {
goal := create.model()
// Verify that the envelope exists. If not, append the error and move to the next goal
_, err := getResourceByID[models.Envelope](c, co, create.EnvelopeID)
if !err.Nil() {
status = r.appendError(err, status)
continue
}
dbErr := co.DB.Create(&goal).Error
if dbErr != nil {
err := httperrors.GenericDBError[models.Goal](goal, c, dbErr)
status = r.appendError(err, status)
continue
}
// Transform for the API and append
apiResource := newGoalV3(c, goal)
r.Data = append(r.Data, GoalResponseV3{Data: &apiResource})
}
c.JSON(status, r)
}
// @Summary Get goals
// @Description Returns a list of goals
// @Tags Goals
// @Produce json
// @Success 200 {object} GoalListResponseV3
// @Failure 400 {object} GoalListResponseV3
// @Failure 500 {object} GoalListResponseV3
// @Router /v3/goals [get]
// @Param name query string false "Filter by name"
// @Param note query string false "Filter by note"
// @Param search query string false "Search for this text in name and note"
// @Param archived query bool false "Is the goal archived?"
// @Param envelope query string false "Filter by envelope ID"
// @Param month query string false "Month of the goal. Ignores exact time, matches on the month of the RFC3339 timestamp provided."
// @Param fromMonth query string false "Goals for this and later months. Ignores exact time, matches on the month of the RFC3339 timestamp provided."
// @Param untilMonth query string false "Goals for this and earlier months. Ignores exact time, matches on the month of the RFC3339 timestamp provided."
// @Param amount query string false "Filter by amount"
// @Param amountLessOrEqual query string false "Amount less than or equal to this"
// @Param amountMoreOrEqual query string false "Amount more than or equal to this"
// @Param offset query uint false "The offset of the first goal returned. Defaults to 0."
// @Param limit query int false "Maximum number of goal to return. Defaults to 50."
func (co Controller) GetGoalsV3(c *gin.Context) {
var filter GoalQueryFilterV3
if err := c.Bind(&filter); err != nil {
s := err.Error()
c.JSON(http.StatusBadRequest, GoalListResponseV3{
Error: &s,
})
return
}
queryFields, setFields := httputil.GetURLFields(c.Request.URL, filter)
where, err := filter.model()
if !err.Nil() {
s := err.Error()
c.JSON(err.Status, GoalListResponseV3{
Error: &s,
})
return
}
q := co.DB.
Order("date(month) ASC, name ASC").
Where(&where, queryFields...)
q = stringFilters(co.DB, q, setFields, filter.Name, filter.Note, filter.Search)
// Set the offset. Does not need checking since the default is 0
q = q.Offset(int(filter.Offset))
// Default to 50 Accounts and set the limit
limit := 50
if slices.Contains(setFields, "Limit") {
limit = filter.Limit
}
q = q.Limit(limit)
if !where.Month.IsZero() {
q = q.Where("goals.month >= date(?)", where.Month).Where("goals.month < date(?)", where.Month.AddDate(0, 1))
}
if filter.FromMonth != "" {
fromMonth, e := types.ParseMonth(filter.FromMonth)
if e != nil {
s := e.Error()
c.JSON(http.StatusBadRequest, GoalListResponseV3{
Error: &s,
})
}
q = q.Where("goals.month >= date(?)", fromMonth)
}
if filter.UntilMonth != "" {
untilMonth, e := types.ParseMonth(filter.UntilMonth)
if e != nil {
s := e.Error()
c.JSON(http.StatusBadRequest, GoalListResponseV3{
Error: &s,
})
}
q = q.Where("goals.month < date(?)", untilMonth.AddDate(0, 1))
}
if !filter.AmountLessOrEqual.IsZero() {
q = q.Where("goals.amount <= ?", filter.AmountLessOrEqual)
}
if !filter.AmountMoreOrEqual.IsZero() {
q = q.Where("goals.amount >= ?", filter.AmountMoreOrEqual)
}
var goals []models.Goal
err = query(c, q.Find(&goals))
if !err.Nil() {
s := err.Error()
c.JSON(err.Status, GoalListResponseV3{
Error: &s,
})
return
}
var count int64
err = query(c, q.Limit(-1).Offset(-1).Count(&count))
if !err.Nil() {
e := err.Error()
c.JSON(err.Status, GoalListResponseV3{
Error: &e,
})
return
}
// Transform resources to their API representation
data := make([]GoalV3, 0, len(goals))
for _, goal := range goals {
data = append(data, newGoalV3(c, goal))
}
c.JSON(http.StatusOK, GoalListResponseV3{
Data: data,
Pagination: &Pagination{
Count: len(data),
Total: count,
Offset: filter.Offset,
Limit: limit,
},
})
}
// @Summary Get goal
// @Description Returns a specific goal
// @Tags Goals
// @Produce json
// @Success 200 {object} GoalResponseV3
// @Failure 400 {object} GoalResponseV3
// @Failure 404 {object} GoalResponseV3
// @Failure 500 {object} GoalResponseV3
// @Param id path string true "ID formatted as string"
// @Router /v3/goals/{id} [get]
func (co Controller) GetGoalV3(c *gin.Context) {
id, err := httputil.UUIDFromString(c.Param("id"))
if !err.Nil() {
e := err.Error()
c.JSON(err.Status, GoalResponseV3{
Error: &e,
})
return
}
var goal models.Goal
err = query(c, co.DB.First(&goal, id))
if !err.Nil() {
e := err.Error()
c.JSON(err.Status, GoalResponseV3{
Error: &e,
})
return
}
apiResource := newGoalV3(c, goal)
c.JSON(http.StatusOK, GoalResponseV3{Data: &apiResource})
}
// @Summary Update goal
// @Description Updates an existing goal. Only values to be updated need to be specified.
// @Tags Goals
// @Accept json
// @Produce json
// @Success 200 {object} GoalResponseV3
// @Failure 400 {object} GoalResponseV3
// @Failure 404 {object} GoalResponseV3
// @Failure 500 {object} GoalResponseV3
// @Param id path string true "ID formatted as string"
// @Param goal body GoalV3Editable true "Goal"
// @Router /v3/goals/{id} [patch]
func (co Controller) UpdateGoalV3(c *gin.Context) {
id, err := httputil.UUIDFromString(c.Param("id"))
if !err.Nil() {
e := err.Error()
c.JSON(err.Status, GoalResponseV3{
Error: &e,
})
return
}
goal, err := getResourceByID[models.Goal](c, co, id)
if !err.Nil() {
e := err.Error()
c.JSON(err.Status, GoalResponseV3{
Error: &e,
})
return
}
// Get the fields that are set to be updated
updateFields, err := httputil.GetBodyFields(c, GoalV3Editable{})
if !err.Nil() {
e := err.Error()
c.JSON(err.Status, GoalResponseV3{
Error: &e,
})
return
}
// Bind the data for the patch
var data GoalV3Editable
err = httputil.BindData(c, &data)
if !err.Nil() {
e := err.Error()
c.JSON(err.Status, GoalResponseV3{
Error: &e,
})
return
}
// Check that the referenced envelope exists
if slices.Contains(updateFields, "EnvelopeID") {
_, err = getResourceByID[models.Envelope](c, co, data.EnvelopeID)
if !err.Nil() {
e := err.Error()
c.JSON(err.Status, GoalResponseV3{
Error: &e,
})
return
}
}
err = query(c, co.DB.Model(&goal).Select("", updateFields...).Updates(data.model()))
if !err.Nil() {
e := err.Error()
c.JSON(err.Status, GoalResponseV3{
Error: &e,
})
return
}
apiResource := newGoalV3(c, goal)
c.JSON(http.StatusOK, GoalResponseV3{Data: &apiResource})
}
// @Summary Delete goal
// @Description Deletes a goal
// @Tags Goals
// @Success 204
// @Failure 400 {object} httperrors.HTTPError
// @Failure 404 {object} httperrors.HTTPError
// @Failure 500 {object} httperrors.HTTPError
// @Param id path string true "ID formatted as string"
// @Router /v3/goals/{id} [delete]
func (co Controller) DeleteGoalV3(c *gin.Context) {
id, err := httputil.UUIDFromString(c.Param("id"))
if !err.Nil() {
c.JSON(err.Status, httperrors.HTTPError{
Error: err.Error(),
})
return
}
goal, err := getResourceByID[models.Goal](c, co, id)
if !err.Nil() {
c.JSON(err.Status, httperrors.HTTPError{
Error: err.Error(),
})
return
}
err = query(c, co.DB.Delete(&goal))
if !err.Nil() {
c.JSON(err.Status, httperrors.HTTPError{
Error: err.Error(),
})
return
}
c.JSON(http.StatusNoContent, nil)
}