Skip to content

Commit 765c63c

Browse files
authored
feat: always include all resource fields in month response (#563)
With this change, all fields for categories and envelopes are always included in queries to the month endpoint.
1 parent 53038f8 commit 765c63c

File tree

10 files changed

+435
-66
lines changed

10 files changed

+435
-66
lines changed

api/docs.go

Lines changed: 128 additions & 7 deletions
Large diffs are not rendered by default.

api/swagger.json

Lines changed: 128 additions & 7 deletions
Large diffs are not rendered by default.

api/swagger.yaml

Lines changed: 113 additions & 7 deletions
Large diffs are not rendered by default.

pkg/controllers/budget_test.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,11 @@ func (suite *TestSuiteStandard) TestBudgetMonth() {
285285
Income: decimal.NewFromFloat(0),
286286
Envelopes: []models.EnvelopeMonth{
287287
{
288-
Name: "Utilities",
288+
Envelope: models.Envelope{
289+
EnvelopeCreate: models.EnvelopeCreate{
290+
Name: "Utilities",
291+
},
292+
},
289293
Month: types.NewMonth(2022, 1),
290294
Spent: decimal.NewFromFloat(-10),
291295
Balance: decimal.NewFromFloat(10.99),
@@ -303,7 +307,11 @@ func (suite *TestSuiteStandard) TestBudgetMonth() {
303307
Income: decimal.NewFromFloat(0),
304308
Envelopes: []models.EnvelopeMonth{
305309
{
306-
Name: "Utilities",
310+
Envelope: models.Envelope{
311+
EnvelopeCreate: models.EnvelopeCreate{
312+
Name: "Utilities",
313+
},
314+
},
307315
Month: types.NewMonth(2022, 2),
308316
Balance: decimal.NewFromFloat(53.11),
309317
Spent: decimal.NewFromFloat(-5),
@@ -321,7 +329,11 @@ func (suite *TestSuiteStandard) TestBudgetMonth() {
321329
Income: decimal.NewFromFloat(1500),
322330
Envelopes: []models.EnvelopeMonth{
323331
{
324-
Name: "Utilities",
332+
Envelope: models.Envelope{
333+
EnvelopeCreate: models.EnvelopeCreate{
334+
Name: "Utilities",
335+
},
336+
},
325337
Month: types.NewMonth(2022, 3),
326338
Balance: decimal.NewFromFloat(69.28),
327339
Spent: decimal.NewFromFloat(-15),

pkg/controllers/envelope_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,11 @@ func (suite *TestSuiteStandard) TestEnvelopeMonth() {
291291
{
292292
fmt.Sprintf("%s/2022-01", envelope.Data.Links.Self),
293293
models.EnvelopeMonth{
294-
Name: "Utilities",
294+
Envelope: models.Envelope{
295+
EnvelopeCreate: models.EnvelopeCreate{
296+
Name: "Utilities",
297+
},
298+
},
295299
Month: types.NewMonth(2022, 1),
296300
Spent: decimal.NewFromFloat(-10),
297301
Balance: decimal.NewFromFloat(10.99),
@@ -301,7 +305,11 @@ func (suite *TestSuiteStandard) TestEnvelopeMonth() {
301305
{
302306
fmt.Sprintf("%s/2022-02", envelope.Data.Links.Self),
303307
models.EnvelopeMonth{
304-
Name: "Utilities",
308+
Envelope: models.Envelope{
309+
EnvelopeCreate: models.EnvelopeCreate{
310+
Name: "Utilities",
311+
},
312+
},
305313
Month: types.NewMonth(2022, 2),
306314
Balance: decimal.NewFromFloat(53.11),
307315
Spent: decimal.NewFromFloat(-5),
@@ -311,7 +319,11 @@ func (suite *TestSuiteStandard) TestEnvelopeMonth() {
311319
{
312320
fmt.Sprintf("%s/2022-03", envelope.Data.Links.Self),
313321
models.EnvelopeMonth{
314-
Name: "Utilities",
322+
Envelope: models.Envelope{
323+
EnvelopeCreate: models.EnvelopeCreate{
324+
Name: "Utilities",
325+
},
326+
},
315327
Month: types.NewMonth(2022, 3),
316328
Balance: decimal.NewFromFloat(69.28),
317329
Spent: decimal.NewFromFloat(-15),
@@ -322,7 +334,11 @@ func (suite *TestSuiteStandard) TestEnvelopeMonth() {
322334
{
323335
fmt.Sprintf("%s/1998-10", envelope.Data.Links.Self),
324336
models.EnvelopeMonth{
325-
Name: "Utilities",
337+
Envelope: models.Envelope{
338+
EnvelopeCreate: models.EnvelopeCreate{
339+
Name: "Utilities",
340+
},
341+
},
326342
Month: types.NewMonth(1998, 10),
327343
Spent: decimal.NewFromFloat(-0),
328344
Balance: decimal.NewFromFloat(0),

pkg/models/budget.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,11 @@ func (b Budget) Allocated(db *gorm.DB, month types.Month) (allocated decimal.Dec
114114
}
115115

116116
type CategoryEnvelopes struct {
117-
ID uuid.UUID `json:"id" example:"dafd9a74-6aeb-46b9-9f5a-cfca624fea85"` // ID of the category
118-
Name string `json:"name" example:"Rainy Day Funds" default:""` // Name of the category
119-
Envelopes []EnvelopeMonth `json:"envelopes"` // Slice of all envelopes
120-
Balance decimal.Decimal `json:"balance" example:"-10.13"` // Sum of the balances of the envelopes
121-
Allocation decimal.Decimal `json:"allocation" example:"90"` // Sum of allocations for the envelopes
122-
Spent decimal.Decimal `json:"spent" example:"100.13"` // Sum spent for all envelopes
117+
Category
118+
Envelopes []EnvelopeMonth `json:"envelopes"` // Slice of all envelopes
119+
Balance decimal.Decimal `json:"balance" example:"-10.13"` // Sum of the balances of the envelopes
120+
Allocation decimal.Decimal `json:"allocation" example:"90"` // Sum of allocations for the envelopes
121+
Spent decimal.Decimal `json:"spent" example:"100.13"` // Sum spent for all envelopes
123122
}
124123

125124
type Month struct {
@@ -175,8 +174,7 @@ func (b Budget) Month(db *gorm.DB, month types.Month, baseURL string) (Month, er
175174
var categoryEnvelopes CategoryEnvelopes
176175

177176
// Set the basic category values
178-
categoryEnvelopes.ID = category.ID
179-
categoryEnvelopes.Name = category.Name
177+
categoryEnvelopes.Category = category
180178
categoryEnvelopes.Envelopes = make([]EnvelopeMonth, 0)
181179

182180
var envelopes []Envelope

pkg/models/budget_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -394,14 +394,13 @@ func (suite *TestSuiteStandard) TestMonth() {
394394
Available: decimal.NewFromFloat(-20.99),
395395
Categories: []models.CategoryEnvelopes{
396396
{
397-
Name: category.Name,
398-
ID: category.ID,
397+
Category: category,
399398
Balance: decimal.NewFromFloat(10.99),
400399
Spent: decimal.NewFromFloat(-10),
401400
Allocation: decimal.NewFromFloat(20.99),
402401
Envelopes: []models.EnvelopeMonth{
403402
{
404-
Name: "Utilities",
403+
Envelope: envelope,
405404
Month: types.NewMonth(2022, 1),
406405
Spent: decimal.NewFromFloat(-10),
407406
Balance: decimal.NewFromFloat(10.99),
@@ -426,14 +425,13 @@ func (suite *TestSuiteStandard) TestMonth() {
426425
Available: decimal.NewFromFloat(-68.11),
427426
Categories: []models.CategoryEnvelopes{
428427
{
429-
Name: category.Name,
430-
ID: category.ID,
428+
Category: category,
431429
Balance: decimal.NewFromFloat(53.11),
432430
Spent: decimal.NewFromFloat(-5),
433431
Allocation: decimal.NewFromFloat(47.12),
434432
Envelopes: []models.EnvelopeMonth{
435433
{
436-
Name: "Utilities",
434+
Envelope: envelope,
437435
Month: types.NewMonth(2022, 2),
438436
Balance: decimal.NewFromFloat(53.11),
439437
Spent: decimal.NewFromFloat(-5),
@@ -458,14 +456,13 @@ func (suite *TestSuiteStandard) TestMonth() {
458456
Available: decimal.NewFromFloat(1400.72),
459457
Categories: []models.CategoryEnvelopes{
460458
{
461-
Name: category.Name,
462-
ID: category.ID,
459+
Category: category,
463460
Balance: decimal.NewFromFloat(69.28),
464461
Spent: decimal.NewFromFloat(-15),
465462
Allocation: decimal.NewFromFloat(31.17),
466463
Envelopes: []models.EnvelopeMonth{
467464
{
468-
Name: "Utilities",
465+
Envelope: envelope,
469466
Month: types.NewMonth(2022, 3),
470467
Balance: decimal.NewFromFloat(69.28),
471468
Spent: decimal.NewFromFloat(-15),

pkg/models/category.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import "github.com/google/uuid"
66
type Category struct {
77
DefaultModel
88
CategoryCreate
9-
Budget Budget `json:"-"`
9+
Budget Budget `json:"-"` // The budget the category belongs to
1010
}
1111

1212
type CategoryCreate struct {
13-
Name string `json:"name" gorm:"uniqueIndex:category_budget_name" example:"Saving" default:""`
14-
BudgetID uuid.UUID `json:"budgetId" gorm:"uniqueIndex:category_budget_name" example:"52d967d3-33f4-4b04-9ba7-772e5ab9d0ce"`
15-
Note string `json:"note" example:"All envelopes for long-term saving" default:""`
16-
Hidden bool `json:"hidden" example:"true" default:"false"`
13+
Name string `json:"name" gorm:"uniqueIndex:category_budget_name" example:"Saving" default:""` // Name of the category
14+
BudgetID uuid.UUID `json:"budgetId" gorm:"uniqueIndex:category_budget_name" example:"52d967d3-33f4-4b04-9ba7-772e5ab9d0ce"` // ID of the budget the category belongs to
15+
Note string `json:"note" example:"All envelopes for long-term saving" default:""` // Notes about the category
16+
Hidden bool `json:"hidden" example:"true" default:"false"` // Is the category hidden?
1717
}

pkg/models/envelope.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ type Envelope struct {
1818
}
1919

2020
type EnvelopeCreate struct {
21-
Name string `json:"name" gorm:"uniqueIndex:envelope_category_name" example:"Groceries" default:""`
22-
CategoryID uuid.UUID `json:"categoryId" gorm:"uniqueIndex:envelope_category_name" example:"878c831f-af99-4a71-b3ca-80deb7d793c1"`
23-
Note string `json:"note" example:"For stuff bought at supermarkets and drugstores" default:""`
24-
Hidden bool `json:"hidden" example:"true" default:"false"`
21+
Name string `json:"name" gorm:"uniqueIndex:envelope_category_name" example:"Groceries" default:""` // Name of the envelope
22+
CategoryID uuid.UUID `json:"categoryId" gorm:"uniqueIndex:envelope_category_name" example:"878c831f-af99-4a71-b3ca-80deb7d793c1"` // ID of the category the envelope belongs to
23+
Note string `json:"note" example:"For stuff bought at supermarkets and drugstores" default:""` // Notes about the envelope
24+
Hidden bool `json:"hidden" example:"true" default:"false"` // Is the envelope hidden?
2525
}
2626

2727
type EnvelopeMonthLinks struct {
@@ -30,13 +30,12 @@ type EnvelopeMonthLinks struct {
3030

3131
// EnvelopeMonth contains data about an Envelope for a specific month.
3232
type EnvelopeMonth struct {
33-
ID uuid.UUID `json:"id" example:"10b9705d-3356-459e-9d5a-28d42a6c4547"` // The ID of the Envelope
34-
Name string `json:"name" example:"Groceries"` // The name of the Envelope
33+
Envelope
3534
Month types.Month `json:"month" example:"1969-06-01T00:00:00.000000Z" hidden:"deprecated"` // This is always set to 00:00 UTC on the first of the month. **This field is deprecated and will be removed in v2**
36-
Spent decimal.Decimal `json:"spent" example:"73.12"`
37-
Balance decimal.Decimal `json:"balance" example:"12.32"`
38-
Allocation decimal.Decimal `json:"allocation" example:"85.44"`
39-
Links EnvelopeMonthLinks `json:"links"`
35+
Spent decimal.Decimal `json:"spent" example:"73.12"` // The amount spent over the whole month
36+
Balance decimal.Decimal `json:"balance" example:"12.32"` // The balance at the end of the monht
37+
Allocation decimal.Decimal `json:"allocation" example:"85.44"` // The amount of money allocated
38+
Links EnvelopeMonthLinks `json:"links"` // Linked resources
4039
}
4140

4241
// Spent returns the amount spent for the month the time.Time instance is in.
@@ -266,8 +265,7 @@ func (e Envelope) Balance(db *gorm.DB, month types.Month) (decimal.Decimal, erro
266265
func (e Envelope) Month(db *gorm.DB, month types.Month) (EnvelopeMonth, uuid.UUID, error) {
267266
spent := e.Spent(db, month)
268267
envelopeMonth := EnvelopeMonth{
269-
ID: e.ID,
270-
Name: e.Name,
268+
Envelope: e,
271269
Month: month,
272270
Spent: spent,
273271
Balance: decimal.NewFromFloat(0),

pkg/models/model.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ import (
1111
// As EnvelopeMonth uses the Envelope ID and the Month as primary key,
1212
// we the timestamps are managed in the Timestamps struct.
1313
type DefaultModel struct {
14-
ID uuid.UUID `json:"id" example:"65392deb-5e92-4268-b114-297faad6cdce"`
14+
ID uuid.UUID `json:"id" example:"65392deb-5e92-4268-b114-297faad6cdce"` // UUID for the resource
1515
Timestamps
1616
}
1717

1818
// Timestamps only contains the timestamps that gorm sets automatically to enable other
1919
// primary keys than ID.
2020
type Timestamps struct {
21-
CreatedAt time.Time `json:"createdAt" example:"2022-04-02T19:28:44.491514Z"`
22-
UpdatedAt time.Time `json:"updatedAt" example:"2022-04-17T20:14:01.048145Z"`
23-
DeletedAt *gorm.DeletedAt `json:"deletedAt" gorm:"index" example:"2022-04-22T21:01:05.058161Z" swaggertype:"primitive,string"`
21+
CreatedAt time.Time `json:"createdAt" example:"2022-04-02T19:28:44.491514Z"` // Time the resource was created
22+
UpdatedAt time.Time `json:"updatedAt" example:"2022-04-17T20:14:01.048145Z"` // Last time the resource was updated
23+
DeletedAt *gorm.DeletedAt `json:"deletedAt" gorm:"index" example:"2022-04-22T21:01:05.058161Z" swaggertype:"primitive,string"` // Time the resource was marked as deleted
2424
}
2525

2626
// AfterFind updates the timestamps to use UTC as

0 commit comments

Comments
 (0)