Skip to content

Commit 00a03e2

Browse files
committed
fix: return HTTP 201 with budget object instead of 204 on import
1 parent dd4f092 commit 00a03e2

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed

api/docs.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,8 +1863,11 @@ const docTemplate = `{
18631863
}
18641864
],
18651865
"responses": {
1866-
"204": {
1867-
"description": "No Content"
1866+
"201": {
1867+
"description": "Created",
1868+
"schema": {
1869+
"$ref": "#/definitions/controllers.BudgetResponse"
1870+
}
18681871
},
18691872
"400": {
18701873
"description": "Bad Request",

api/swagger.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1851,8 +1851,11 @@
18511851
}
18521852
],
18531853
"responses": {
1854-
"204": {
1855-
"description": "No Content"
1854+
"201": {
1855+
"description": "Created",
1856+
"schema": {
1857+
"$ref": "#/definitions/controllers.BudgetResponse"
1858+
}
18561859
},
18571860
"400": {
18581861
"description": "Bad Request",

api/swagger.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,8 +2102,10 @@ paths:
21022102
produces:
21032103
- application/json
21042104
responses:
2105-
"204":
2106-
description: No Content
2105+
"201":
2106+
description: Created
2107+
schema:
2108+
$ref: '#/definitions/controllers.BudgetResponse'
21072109
"400":
21082110
description: Bad Request
21092111
schema:

pkg/controllers/import.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (co Controller) Import(c *gin.Context) {
8080
// @Tags Import
8181
// @Accept multipart/form-data
8282
// @Produce json
83-
// @Success 204
83+
// @Success 201 {object} BudgetResponse
8484
// @Failure 400 {object} httperrors.HTTPError
8585
// @Failure 500 {object} httperrors.HTTPError
8686
// @Param file formData file true "File to import"
@@ -140,11 +140,15 @@ func (co Controller) ImportYnab4(c *gin.Context) {
140140
return
141141
}
142142

143-
err = importer.Create(co.DB, query.BudgetName, resources)
143+
budget, err = importer.Create(co.DB, query.BudgetName, resources)
144144
if err != nil {
145145
httperrors.Handler(c, err)
146146
return
147147
}
148148

149-
c.JSON(http.StatusNoContent, nil)
149+
b, ok := co.getBudgetObject(c, budget.ID)
150+
if !ok {
151+
return
152+
}
153+
c.JSON(http.StatusCreated, BudgetResponse{Data: b})
150154
}

pkg/controllers/import_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ func (suite *TestSuiteStandard) TestImport() {
8787
// Import one
8888
body, headers := suite.loadTestFile("Budget.yfull")
8989
recorder := test.Request(suite.controller, suite.T(), http.MethodPost, "http://example.com/v1/import?budgetName=Test Budget", body, headers)
90-
suite.Assert().Equal(http.StatusNoContent, recorder.Code, "Request ID %s, response %s", recorder.Header().Get("x-request-id"), recorder.Body.String())
90+
suite.Assert().Equal(http.StatusCreated, recorder.Code, "Request ID %s, response %s", recorder.Header().Get("x-request-id"), recorder.Body.String())
9191
}

pkg/importer/creator.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"errors"
55

66
"github.com/envelope-zero/backend/pkg/importer/types"
7+
"github.com/envelope-zero/backend/pkg/models"
78
"github.com/google/uuid"
89
"gorm.io/gorm"
910
)
1011

11-
func Create(db *gorm.DB, budgetName string, resources types.ParsedResources) error {
12+
func Create(db *gorm.DB, budgetName string, resources types.ParsedResources) (models.Budget, error) {
1213
// Start a transaction so we can roll back all created resources if an error occurs
1314
tx := db.Begin()
1415

@@ -18,7 +19,7 @@ func Create(db *gorm.DB, budgetName string, resources types.ParsedResources) err
1819
err := tx.Create(&budget).Error
1920
if err != nil {
2021
tx.Rollback()
21-
return err
22+
return models.Budget{}, err
2223
}
2324

2425
// Create accounts
@@ -30,7 +31,7 @@ func Create(db *gorm.DB, budgetName string, resources types.ParsedResources) err
3031
err := tx.Create(&account).Error
3132
if err != nil {
3233
tx.Rollback()
33-
return err
34+
return models.Budget{}, err
3435
}
3536

3637
// Update the account in the resources struct so that it also contains the ID
@@ -44,7 +45,7 @@ func Create(db *gorm.DB, budgetName string, resources types.ParsedResources) err
4445
err := tx.Create(&category.Model).Error
4546
if err != nil {
4647
tx.Rollback()
47-
return err
48+
return models.Budget{}, err
4849
}
4950
resources.Categories[cName] = category
5051

@@ -55,7 +56,7 @@ func Create(db *gorm.DB, budgetName string, resources types.ParsedResources) err
5556
err := tx.Create(&envelope.Model).Error
5657
if err != nil {
5758
tx.Rollback()
58-
return err
59+
return models.Budget{}, err
5960
}
6061
resources.Categories[category.Model.Name].Envelopes[eName] = envelope
6162
}
@@ -64,7 +65,7 @@ func Create(db *gorm.DB, budgetName string, resources types.ParsedResources) err
6465
// Create transactions
6566
for _, r := range resources.Transactions {
6667
if r.Model.Amount.IsNegative() {
67-
return errors.New("a transaction to be imported has a negative amount, this is invalid")
68+
return models.Budget{}, errors.New("a transaction to be imported has a negative amount, this is invalid")
6869
}
6970

7071
transaction := r.Model
@@ -80,7 +81,7 @@ func Create(db *gorm.DB, budgetName string, resources types.ParsedResources) err
8081
err := tx.Create(&transaction).Error
8182
if err != nil {
8283
tx.Rollback()
83-
return err
84+
return models.Budget{}, err
8485
}
8586
}
8687

@@ -92,7 +93,7 @@ func Create(db *gorm.DB, budgetName string, resources types.ParsedResources) err
9293
err := tx.Create(&allocation).Error
9394
if err != nil {
9495
tx.Rollback()
95-
return err
96+
return models.Budget{}, err
9697
}
9798
}
9899

@@ -104,11 +105,11 @@ func Create(db *gorm.DB, budgetName string, resources types.ParsedResources) err
104105
err := tx.Create(&mConfig).Error
105106
if err != nil {
106107
tx.Rollback()
107-
return err
108+
return models.Budget{}, err
108109
}
109110
}
110111

111112
// No errors happened, commit the transaction
112113
tx.Commit()
113-
return nil
114+
return budget, nil
114115
}

0 commit comments

Comments
 (0)