Skip to content

Commit b6ef99f

Browse files
authored
feat: sorting of categories and envelopes for /v3/months (#918)
1 parent 2b6c52f commit b6ef99f

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

pkg/controllers/month_v3.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,12 @@ func (co Controller) GetMonthV3(c *gin.Context) {
123123

124124
// Get all categories for the budget
125125
var categories []models.Category
126-
err = co.DB.Where(&models.Category{CategoryCreate: models.CategoryCreate{BudgetID: b.ID}}).Find(&categories).Error
126+
err = co.DB.
127+
Where(&models.Category{CategoryCreate: models.CategoryCreate{BudgetID: b.ID}}).
128+
Order("name ASC").
129+
Find(&categories).
130+
Error
131+
127132
if err != nil {
128133
e := httperrors.Parse(c, err)
129134
s := e.Error()
@@ -146,11 +151,16 @@ func (co Controller) GetMonthV3(c *gin.Context) {
146151

147152
var envelopes []models.Envelope
148153

149-
err = co.DB.Where(&models.Envelope{
150-
EnvelopeCreate: models.EnvelopeCreate{
151-
CategoryID: category.ID,
152-
},
153-
}).Find(&envelopes).Error
154+
err = co.DB.
155+
Where(&models.Envelope{
156+
EnvelopeCreate: models.EnvelopeCreate{
157+
CategoryID: category.ID,
158+
},
159+
}).
160+
Order("name asc").
161+
Find(&envelopes).
162+
Error
163+
154164
if err != nil {
155165
e := httperrors.Parse(c, err)
156166
s := e.Error()

pkg/controllers/month_v3_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,35 @@ func (suite *TestSuiteStandard) TestMonthsV3PostFails() {
297297
}
298298
}
299299

300+
// TestMonthsV3Sorting verifies that categories and months are sorted correctly
301+
func (suite *TestSuiteStandard) TestMonthsV3Sorting() {
302+
budget := suite.createTestBudgetV3(suite.T(), models.BudgetCreate{})
303+
categoryU := suite.createTestCategoryV3(suite.T(), controllers.CategoryCreateV3{BudgetID: budget.Data.ID, Name: "Upkeep"})
304+
envelopeU := suite.createTestEnvelopeV3(suite.T(), controllers.EnvelopeCreateV3{CategoryID: categoryU.Data.ID, Name: "Utilities"})
305+
envelopeM := suite.createTestEnvelopeV3(suite.T(), controllers.EnvelopeCreateV3{CategoryID: categoryU.Data.ID, Name: "Muppets"})
306+
307+
categoryA := suite.createTestCategoryV3(suite.T(), controllers.CategoryCreateV3{BudgetID: budget.Data.ID, Name: "Alphabetically first"})
308+
envelopeB := suite.createTestEnvelopeV3(suite.T(), controllers.EnvelopeCreateV3{CategoryID: categoryA.Data.ID, Name: "Batteries"})
309+
envelopeC := suite.createTestEnvelopeV3(suite.T(), controllers.EnvelopeCreateV3{CategoryID: categoryA.Data.ID, Name: "Chargers"})
310+
311+
// Get month data
312+
recorder := test.Request(suite.controller, suite.T(), http.MethodGet, strings.Replace(budget.Data.Links.Month, "YYYY-MM", types.MonthOf(time.Now()).String(), 1), "")
313+
assertHTTPStatus(suite.T(), &recorder, http.StatusOK)
314+
315+
// Parse month data
316+
var response controllers.MonthResponseV3
317+
suite.decodeResponse(&recorder, &response)
318+
month := response.Data
319+
320+
assert.Equal(suite.T(), categoryU.Data.ID, month.Categories[1].ID)
321+
assert.Equal(suite.T(), envelopeU.Data.ID, month.Categories[1].Envelopes[1].ID)
322+
assert.Equal(suite.T(), envelopeM.Data.ID, month.Categories[1].Envelopes[0].ID)
323+
324+
assert.Equal(suite.T(), categoryA.Data.ID, month.Categories[0].ID)
325+
assert.Equal(suite.T(), envelopeB.Data.ID, month.Categories[0].Envelopes[0].ID)
326+
assert.Equal(suite.T(), envelopeC.Data.ID, month.Categories[0].Envelopes[1].ID)
327+
}
328+
300329
// TestMonthsV3 verifies that the monthly calculations are correct.
301330
func (suite *TestSuiteStandard) TestMonthsV3() {
302331
budget := suite.createTestBudgetV3(suite.T(), models.BudgetCreate{})

0 commit comments

Comments
 (0)