/
cleanup.go
60 lines (52 loc) · 1.46 KB
/
cleanup.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
package v3
import (
"net/http"
"github.com/envelope-zero/backend/v4/pkg/httperrors"
"github.com/envelope-zero/backend/v4/pkg/models"
"github.com/gin-gonic/gin"
)
// @Summary Delete everything
// @Description Permanently deletes all resources
// @Tags v3
// @Success 204
// @Failure 400 {object} httperrors.HTTPError
// @Failure 500 {object} httperrors.HTTPError
// @Param confirm query string false "Confirmation to delete all resources. Must have the value 'yes-please-delete-everything'"
// @Router /v3 [delete]
func Cleanup(c *gin.Context) {
var params struct {
Confirm string `form:"confirm"`
}
err := c.Bind(¶ms)
if err != nil || params.Confirm != "yes-please-delete-everything" {
c.JSON(http.StatusBadRequest, httperrors.HTTPError{
Error: httperrors.ErrCleanupConfirmation.Error(),
})
return
}
// The order is important here since there are foreign keys to consider!
resources := []models.Model{
models.MatchRule{},
models.Goal{},
models.Transaction{},
models.MonthConfig{},
models.Envelope{},
models.Category{},
models.Account{},
models.Budget{},
}
// Use a transaction so that we can roll back if errors happen
tx := models.DB.Begin()
for _, model := range resources {
err := tx.Unscoped().Where("true").Delete(&model).Error
if err != nil {
c.JSON(http.StatusInternalServerError, httperrors.HTTPError{
Error: err.Error(),
})
tx.Rollback()
return
}
}
tx.Commit()
c.JSON(http.StatusNoContent, nil)
}