Skip to content

Commit de86f5d

Browse files
authored
refactor: use generics for resource detail OPTIONS requests and DELETE requests (#1147)
1 parent 238a22b commit de86f5d

File tree

8 files changed

+81
-305
lines changed

8 files changed

+81
-305
lines changed

internal/controllers/v4/account.go

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,7 @@ func OptionsAccountList(c *gin.Context) {
5050
// @Param id path URIID true "ignored, but needed: https://github.com/swaggo/swag/issues/1014"
5151
// @Router /v4/accounts/{id} [options]
5252
func OptionsAccountDetail(c *gin.Context) {
53-
var uri URIID
54-
err := c.ShouldBindUri(&uri)
55-
if err != nil {
56-
c.JSON(status(err), httpError{
57-
Error: err.Error(),
58-
})
59-
return
60-
}
61-
62-
err = models.DB.First(&models.Account{}, uri.ID).Error
63-
if err != nil {
64-
c.JSON(status(err), httpError{
65-
Error: err.Error(),
66-
})
67-
return
68-
}
69-
70-
httputil.OptionsGetPatchDelete(c)
53+
resourceOptionsDetail(c, models.Account{})
7154
}
7255

7356
// @Summary Creates accounts
@@ -462,31 +445,5 @@ func UpdateAccount(c *gin.Context) {
462445
// @Param id path URIID true "ignored, but needed: https://github.com/swaggo/swag/issues/1014"
463446
// @Router /v4/accounts/{id} [delete]
464447
func DeleteAccount(c *gin.Context) {
465-
var uri URIID
466-
err := c.ShouldBindUri(&uri)
467-
if err != nil {
468-
c.JSON(status(err), httpError{
469-
Error: err.Error(),
470-
})
471-
return
472-
}
473-
474-
var account models.Account
475-
err = models.DB.First(&account, uri.ID).Error
476-
if err != nil {
477-
c.JSON(status(err), httpError{
478-
Error: err.Error(),
479-
})
480-
return
481-
}
482-
483-
err = models.DB.Delete(&account).Error
484-
if err != nil {
485-
c.JSON(status(err), httpError{
486-
Error: err.Error(),
487-
})
488-
return
489-
}
490-
491-
c.JSON(http.StatusNoContent, nil)
448+
deleteResource[models.Account](c)
492449
}

internal/controllers/v4/budget.go

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,7 @@ func OptionsBudgetList(c *gin.Context) {
4747
// @Param id path URIID true "ignored, but needed: https://github.com/swaggo/swag/issues/1014"
4848
// @Router /v4/budgets/{id} [options]
4949
func OptionsBudgetDetail(c *gin.Context) {
50-
var uri URIID
51-
err := c.ShouldBindUri(&uri)
52-
if err != nil {
53-
c.JSON(status(err), httpError{
54-
Error: err.Error(),
55-
})
56-
return
57-
}
58-
59-
err = models.DB.First(&models.Budget{}, uri.ID).Error
60-
if err != nil {
61-
c.JSON(status(err), httpError{
62-
Error: err.Error(),
63-
})
64-
return
65-
}
66-
67-
httputil.OptionsGetPatchDelete(c)
50+
resourceOptionsDetail(c, models.Budget{})
6851
}
6952

7053
// @Summary Create budget
@@ -296,31 +279,5 @@ func UpdateBudget(c *gin.Context) {
296279
// @Param id path URIID true "ignored, but needed: https://github.com/swaggo/swag/issues/1014"
297280
// @Router /v4/budgets/{id} [delete]
298281
func DeleteBudget(c *gin.Context) {
299-
var uri URIID
300-
err := c.ShouldBindUri(&uri)
301-
if err != nil {
302-
c.JSON(status(err), httpError{
303-
Error: err.Error(),
304-
})
305-
return
306-
}
307-
308-
var budget models.Budget
309-
err = models.DB.First(&budget, uri.ID).Error
310-
if err != nil {
311-
c.JSON(status(err), httpError{
312-
Error: err.Error(),
313-
})
314-
return
315-
}
316-
317-
err = models.DB.Delete(&budget).Error
318-
if err != nil {
319-
c.JSON(status(err), httpError{
320-
Error: err.Error(),
321-
})
322-
return
323-
}
324-
325-
c.JSON(http.StatusNoContent, nil)
282+
deleteResource[models.Budget](c)
326283
}

internal/controllers/v4/category.go

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,24 +47,7 @@ func OptionsCategoryList(c *gin.Context) {
4747
// @Param id path URIID true "ignored, but needed: https://github.com/swaggo/swag/issues/1014"
4848
// @Router /v4/categories/{id} [options]
4949
func OptionsCategoryDetail(c *gin.Context) {
50-
var uri URIID
51-
err := c.ShouldBindUri(&uri)
52-
if err != nil {
53-
c.JSON(status(err), httpError{
54-
Error: err.Error(),
55-
})
56-
return
57-
}
58-
59-
err = models.DB.First(&models.Category{}, uri.ID).Error
60-
if err != nil {
61-
c.JSON(status(err), httpError{
62-
Error: err.Error(),
63-
})
64-
return
65-
}
66-
67-
httputil.OptionsGetPatchDelete(c)
50+
resourceOptionsDetail(c, models.Category{})
6851
}
6952

7053
// @Summary Create category
@@ -334,31 +317,5 @@ func UpdateCategory(c *gin.Context) {
334317
// @Param id path URIID true "ignored, but needed: https://github.com/swaggo/swag/issues/1014"
335318
// @Router /v4/categories/{id} [delete]
336319
func DeleteCategory(c *gin.Context) {
337-
var uri URIID
338-
err := c.ShouldBindUri(&uri)
339-
if err != nil {
340-
c.JSON(status(err), httpError{
341-
Error: err.Error(),
342-
})
343-
return
344-
}
345-
346-
var category models.Category
347-
err = models.DB.First(&category, uri.ID).Error
348-
if err != nil {
349-
c.JSON(status(err), httpError{
350-
Error: err.Error(),
351-
})
352-
return
353-
}
354-
355-
err = models.DB.Delete(&category).Error
356-
if err != nil {
357-
c.JSON(status(err), httpError{
358-
Error: err.Error(),
359-
})
360-
return
361-
}
362-
363-
c.JSON(http.StatusNoContent, nil)
320+
deleteResource[models.Category](c)
364321
}

internal/controllers/v4/envelope.go

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -48,24 +48,7 @@ func OptionsEnvelopeList(c *gin.Context) {
4848
// @Param id path URIID true "ignored, but needed: https://github.com/swaggo/swag/issues/1014"
4949
// @Router /v4/envelopes/{id} [options]
5050
func OptionsEnvelopeDetail(c *gin.Context) {
51-
var uri URIID
52-
err := c.ShouldBindUri(&uri)
53-
if err != nil {
54-
c.JSON(status(err), httpError{
55-
Error: err.Error(),
56-
})
57-
return
58-
}
59-
60-
err = models.DB.First(&models.Envelope{}, uri.ID).Error
61-
if err != nil {
62-
c.JSON(status(err), httpError{
63-
Error: err.Error(),
64-
})
65-
return
66-
}
67-
68-
httputil.OptionsGetPatchDelete(c)
51+
resourceOptionsDetail(c, models.Envelope{})
6952
}
7053

7154
// @Summary Create envelope
@@ -312,31 +295,5 @@ func UpdateEnvelope(c *gin.Context) {
312295
// @Param id path URIID true "ignored, but needed: https://github.com/swaggo/swag/issues/1014"
313296
// @Router /v4/envelopes/{id} [delete]
314297
func DeleteEnvelope(c *gin.Context) {
315-
var uri URIID
316-
err := c.ShouldBindUri(&uri)
317-
if err != nil {
318-
c.JSON(status(err), httpError{
319-
Error: err.Error(),
320-
})
321-
return
322-
}
323-
324-
var envelope models.Envelope
325-
err = models.DB.First(&envelope, uri.ID).Error
326-
if err != nil {
327-
c.JSON(status(err), httpError{
328-
Error: err.Error(),
329-
})
330-
return
331-
}
332-
333-
err = models.DB.Delete(&envelope).Error
334-
if err != nil {
335-
c.JSON(status(err), httpError{
336-
Error: err.Error(),
337-
})
338-
return
339-
}
340-
341-
c.JSON(http.StatusNoContent, nil)
298+
deleteResource[models.Envelope](c)
342299
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package v4
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/envelope-zero/backend/v7/internal/httputil"
7+
"github.com/envelope-zero/backend/v7/internal/models"
8+
"github.com/gin-gonic/gin"
9+
)
10+
11+
type Resource interface {
12+
models.Account | models.Budget | models.Category | models.Envelope | models.Goal | models.MatchRule | models.Transaction
13+
}
14+
15+
// resourceOptionsDetail returns the appropriate response for an HTTP OPTIONS request for a specific resource.
16+
//
17+
// Note: This function only works for resources with an ID, not for configurations (like /month-configs) or calculated endpoints (like /months)
18+
func resourceOptionsDetail[R Resource](c *gin.Context, resource R) {
19+
var uri URIID
20+
err := c.ShouldBindUri(&uri)
21+
if err != nil {
22+
c.JSON(status(err), httpError{
23+
Error: err.Error(),
24+
})
25+
return
26+
}
27+
28+
err = models.DB.First(&resource, uri.ID).Error
29+
if err != nil {
30+
c.JSON(status(err), httpError{
31+
Error: err.Error(),
32+
})
33+
return
34+
}
35+
36+
httputil.OptionsGetPatchDelete(c)
37+
}
38+
39+
func deleteResource[R Resource](c *gin.Context) {
40+
var uri URIID
41+
err := c.ShouldBindUri(&uri)
42+
if err != nil {
43+
c.JSON(status(err), httpError{
44+
Error: err.Error(),
45+
})
46+
return
47+
}
48+
49+
var resource R
50+
err = models.DB.First(&resource, uri.ID).Error
51+
if err != nil {
52+
c.JSON(status(err), httpError{
53+
Error: err.Error(),
54+
})
55+
return
56+
}
57+
58+
err = models.DB.Delete(&resource).Error
59+
if err != nil {
60+
c.JSON(status(err), httpError{
61+
Error: err.Error(),
62+
})
63+
return
64+
}
65+
66+
c.JSON(http.StatusNoContent, nil)
67+
}

internal/controllers/v4/goal.go

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,7 @@ func OptionsGoals(c *gin.Context) {
4444
// @Param id path URIID true "ignored, but needed: https://github.com/swaggo/swag/issues/1014"
4545
// @Router /v4/goals/{id} [options]
4646
func OptionsGoalDetail(c *gin.Context) {
47-
var uri URIID
48-
err := c.ShouldBindUri(&uri)
49-
if err != nil {
50-
c.JSON(status(err), httpError{
51-
Error: err.Error(),
52-
})
53-
return
54-
}
55-
56-
err = models.DB.First(&models.Goal{}, uri.ID).Error
57-
if err != nil {
58-
c.JSON(status(err), httpError{
59-
Error: err.Error(),
60-
})
61-
return
62-
}
63-
64-
httputil.OptionsGetPatchDelete(c)
47+
resourceOptionsDetail(c, models.Goal{})
6548
}
6649

6750
// @Summary Create goals
@@ -364,31 +347,5 @@ func UpdateGoal(c *gin.Context) {
364347
// @Param id path URIID true "ignored, but needed: https://github.com/swaggo/swag/issues/1014"
365348
// @Router /v4/goals/{id} [delete]
366349
func DeleteGoal(c *gin.Context) {
367-
var uri URIID
368-
err := c.ShouldBindUri(&uri)
369-
if err != nil {
370-
c.JSON(status(err), httpError{
371-
Error: err.Error(),
372-
})
373-
return
374-
}
375-
376-
var goal models.Goal
377-
err = models.DB.First(&goal, uri.ID).Error
378-
if err != nil {
379-
c.JSON(status(err), httpError{
380-
Error: err.Error(),
381-
})
382-
return
383-
}
384-
385-
err = models.DB.Delete(&goal).Error
386-
if err != nil {
387-
c.JSON(status(err), httpError{
388-
Error: err.Error(),
389-
})
390-
return
391-
}
392-
393-
c.JSON(http.StatusNoContent, nil)
350+
deleteResource[models.Goal](c)
394351
}

0 commit comments

Comments
 (0)