Skip to content

Commit

Permalink
fix: handle empty search filter query
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobheun committed Jan 23, 2023
1 parent bb0e9d9 commit 674caa9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 5 additions & 2 deletions db/deals.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,15 +306,18 @@ func withSearchFilter(filter map[string]interface{}) (string, []interface{}) {
for _, filterField := range filterFields {
// If the filterField is in the filter and it's not empty, append
value, ok := filter[filterField]
if ok {
if ok && value != nil {
st := filterField + " = ?"
statements = append(statements, st)
whereArgs = append(whereArgs, value)
}
}

where := "(" + strings.Join(statements, " AND ") + ")"
if len(statements) == 0 {
return "", whereArgs
}

where := "(" + strings.Join(statements, " AND ") + ")"
return where, whereArgs
}

Expand Down
8 changes: 8 additions & 0 deletions db/deals_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,12 @@ func TestWithSearchFilter(t *testing.T) {
}
req.Equal("(Checkpoint = ? AND IsOffline = ?)", where)
req.Equal(expectedArgs, whereArgs)

where, whereArgs = withSearchFilter(map[string]interface{}{
"IsOffline": nil,
"NotAValidFilter": nil,
})

req.Equal("", where)
req.Equal(0, len(whereArgs))
}

0 comments on commit 674caa9

Please sign in to comment.