Skip to content

Commit 9adb3f5

Browse files
committed
feat: treat envelope name and note filter parameters like "contains"
1 parent 85cfed3 commit 9adb3f5

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

pkg/controllers/envelope.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 EnvelopeListResponse struct {
@@ -36,9 +37,9 @@ type EnvelopeLinks struct {
3637
}
3738

3839
type EnvelopeQueryFilter struct {
39-
Name string `form:"name"`
40+
Name string `form:"name" filterField:"false"`
4041
CategoryID string `form:"category"`
41-
Note string `form:"note"`
42+
Note string `form:"note" filterField:"false"`
4243
}
4344

4445
func (e EnvelopeQueryFilter) ToCreate(c *gin.Context) (models.EnvelopeCreate, bool) {
@@ -48,8 +49,6 @@ func (e EnvelopeQueryFilter) ToCreate(c *gin.Context) (models.EnvelopeCreate, bo
4849
}
4950

5051
return models.EnvelopeCreate{
51-
Name: e.Name,
52-
Note: e.Note,
5352
CategoryID: categoryID,
5453
}, true
5554
}
@@ -161,18 +160,32 @@ func (co Controller) GetEnvelopes(c *gin.Context) {
161160
// The filters contain only strings, so this will always succeed
162161
_ = c.Bind(&filter)
163162

164-
queryFields, _ := httputil.GetURLFields(c.Request.URL, filter)
163+
queryFields, setFields := httputil.GetURLFields(c.Request.URL, filter)
165164

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

172-
var envelopes []models.Envelope
173-
if !queryWithRetry(c, co.DB.Where(&models.Envelope{
171+
query := co.DB.Where(&models.Envelope{
174172
EnvelopeCreate: create,
175-
}, queryFields...).Find(&envelopes)) {
173+
}, queryFields...)
174+
175+
if filter.Name != "" {
176+
query = query.Where("name LIKE ?", fmt.Sprintf("%%%s%%", filter.Name))
177+
} else if slices.Contains(setFields, "Name") {
178+
query = query.Where("name = ''")
179+
}
180+
181+
if filter.Note != "" {
182+
query = query.Where("note LIKE ?", fmt.Sprintf("%%%s%%", filter.Note))
183+
} else if slices.Contains(setFields, "Note") {
184+
query = query.Where("note = ''")
185+
}
186+
187+
var envelopes []models.Envelope
188+
if !queryWithRetry(c, query.Find(&envelopes)) {
176189
return
177190
}
178191

pkg/controllers/envelope_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ func (suite *TestSuiteStandard) TestGetEnvelopesFilter() {
121121
{"Empty Note", "note=", 0},
122122
{"Empty Name", "name=", 0},
123123
{"Name & Note", "name=Groceries&note=For the stuff bought in supermarkets", 1},
124+
{"Fuzzy name", "name=es", 2},
125+
{"Fuzzy note", "note=Because", 2},
124126
}
125127

126128
for _, tt := range tests {

0 commit comments

Comments
 (0)