Skip to content

Commit 85cfed3

Browse files
committed
feat: treat category name and note filter parameters like "contains"
1 parent a4cbb48 commit 85cfed3

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

pkg/controllers/category.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/envelope-zero/backend/pkg/models"
1010
"github.com/gin-gonic/gin"
1111
"github.com/google/uuid"
12+
"golang.org/x/exp/slices"
1213
)
1314

1415
type CategoryListResponse struct {
@@ -31,9 +32,9 @@ type CategoryLinks struct {
3132
}
3233

3334
type CategoryQueryFilter struct {
34-
Name string `form:"name"`
35+
Name string `form:"name" filterField:"false"`
3536
BudgetID string `form:"budget"`
36-
Note string `form:"note"`
37+
Note string `form:"note" filterField:"false"`
3738
}
3839

3940
func (f CategoryQueryFilter) ToCreate(c *gin.Context) (models.CategoryCreate, bool) {
@@ -43,9 +44,7 @@ func (f CategoryQueryFilter) ToCreate(c *gin.Context) (models.CategoryCreate, bo
4344
}
4445

4546
return models.CategoryCreate{
46-
Name: f.Name,
4747
BudgetID: budgetID,
48-
Note: f.Note,
4948
}, true
5049
}
5150

@@ -159,18 +158,32 @@ func (co Controller) GetCategories(c *gin.Context) {
159158
_ = c.Bind(&filter)
160159

161160
// Get the fields that we are filtering for
162-
queryFields, _ := httputil.GetURLFields(c.Request.URL, filter)
161+
queryFields, setFields := httputil.GetURLFields(c.Request.URL, filter)
163162

164163
// Convert the QueryFilter to a Create struct
165164
create, ok := filter.ToCreate(c)
166165
if !ok {
167166
return
168167
}
169168

170-
var categories []models.Category
171-
if !queryWithRetry(c, co.DB.Where(&models.Category{
169+
query := co.DB.Where(&models.Category{
172170
CategoryCreate: create,
173-
}, queryFields...).Find(&categories)) {
171+
}, queryFields...)
172+
173+
if filter.Name != "" {
174+
query = query.Where("name LIKE ?", fmt.Sprintf("%%%s%%", filter.Name))
175+
} else if slices.Contains(setFields, "Name") {
176+
query = query.Where("name = ''")
177+
}
178+
179+
if filter.Note != "" {
180+
query = query.Where("note LIKE ?", fmt.Sprintf("%%%s%%", filter.Note))
181+
} else if slices.Contains(setFields, "Note") {
182+
query = query.Where("note = ''")
183+
}
184+
185+
var categories []models.Category
186+
if !queryWithRetry(c, query.Find(&categories)) {
174187
return
175188
}
176189

pkg/controllers/category_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ func (suite *TestSuiteStandard) TestGetCategoriesFilter() {
128128
})
129129

130130
_ = suite.createTestCategory(models.CategoryCreate{
131-
Name: "Saving",
132-
Note: "For later",
131+
Name: "Groceries",
132+
Note: "For Groceries",
133133
BudgetID: b2.Data.ID,
134134
})
135135

@@ -149,6 +149,10 @@ func (suite *TestSuiteStandard) TestGetCategoriesFilter() {
149149
{"Empty Note", "note=", 0},
150150
{"Empty Name", "name=", 0},
151151
{"Name & Note", "name=Category Name&note=A note for this category", 1},
152+
{"Fuzzy name, no note", "name=Category&note=", 0},
153+
{"Fuzzy name", "name=t", 2},
154+
{"Fuzzy note, no name", "name=&note=Groceries", 0},
155+
{"Fuzzy note", "note=Groceries", 2},
152156
}
153157

154158
for _, tt := range tests {

0 commit comments

Comments
 (0)