Skip to content

Commit 972ee00

Browse files
committed
refactor: rename Budgeted to Allocated, do not use SUM() anymore
1 parent d04fefa commit 972ee00

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

pkg/models/budget.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -90,30 +90,27 @@ func (b Budget) Income(db *gorm.DB, month types.Month) (income decimal.Decimal,
9090
return
9191
}
9292

93-
// Budgeted calculates the sum that has been budgeted for a specific month.
94-
func (b Budget) Budgeted(db *gorm.DB, month types.Month) (decimal.Decimal, error) {
95-
var budgeted decimal.NullDecimal
96-
err := db.
97-
Select("SUM(amount)").
93+
// Allocated calculates the sum that has been budgeted for a specific month.
94+
func (b Budget) Allocated(db *gorm.DB, month types.Month) (allocated decimal.Decimal, err error) {
95+
var allocations []Allocation
96+
err = db.
9897
Joins("JOIN envelopes ON allocations.envelope_id = envelopes.id AND envelopes.deleted_at IS NULL").
9998
Joins("JOIN categories ON envelopes.category_id = categories.id AND categories.deleted_at IS NULL").
10099
Joins("JOIN budgets ON categories.budget_id = budgets.id AND budgets.deleted_at IS NULL").
101100
Where("budgets.id = ?", b.ID).
102101
Where("allocations.month >= date(?)", month).
103102
Where("allocations.month < date(?)", month.AddDate(0, 1)).
104-
Table("allocations").
105-
Find(&budgeted).
103+
Find(&allocations).
106104
Error
107105
if err != nil {
108106
return decimal.Zero, err
109107
}
110108

111-
// If no transactions are found, the value is nil
112-
if !budgeted.Valid {
113-
return decimal.NewFromFloat(0), nil
109+
for _, a := range allocations {
110+
allocated = allocated.Add(a.Amount)
114111
}
115112

116-
return budgeted.Decimal, nil
113+
return
117114
}
118115

119116
type CategoryEnvelopes struct {
@@ -149,7 +146,7 @@ func (b Budget) Month(db *gorm.DB, month types.Month, baseURL string) (Month, er
149146
}
150147

151148
// Add budgeted sum to response
152-
budgeted, err := b.Budgeted(db, result.Month)
149+
budgeted, err := b.Allocated(db, result.Month)
153150
if err != nil {
154151
return Month{}, err
155152
}

pkg/models/budget_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,12 +258,12 @@ func (suite *TestSuiteStandard) TestBudgetCalculations() {
258258
assert.True(suite.T(), income.IsZero(), "Income is %s, should be 0", income)
259259

260260
// Verify budgeted for used budget
261-
budgeted, err := budget.Budgeted(suite.db, marchTwentyTwentyTwo)
261+
budgeted, err := budget.Allocated(suite.db, marchTwentyTwentyTwo)
262262
assert.Nil(suite.T(), err)
263263
assert.True(suite.T(), budgeted.Equal(decimal.NewFromFloat(25)), "Budgeted is %s, should be 25", budgeted)
264264

265265
// Verify budgeted for empty budget
266-
budgeted, err = emptyBudget.Budgeted(suite.db, marchTwentyTwentyTwo)
266+
budgeted, err = emptyBudget.Allocated(suite.db, marchTwentyTwentyTwo)
267267
assert.Nil(suite.T(), err)
268268
assert.True(suite.T(), budgeted.IsZero(), "Budgeted is %s, should be 0", budgeted)
269269
}
@@ -305,7 +305,7 @@ func (suite *TestSuiteStandard) TestBudgetBudgetedDBFail() {
305305

306306
suite.CloseDB()
307307

308-
_, err = budget.Budgeted(suite.db, types.NewMonth(200, 2))
308+
_, err = budget.Allocated(suite.db, types.NewMonth(200, 2))
309309
suite.Assert().NotNil(err)
310310
suite.Assert().Equal("sql: database is closed", err.Error())
311311
}

0 commit comments

Comments
 (0)