11package controllers
22
33import (
4- "fmt"
54 "net/http"
65
76 "github.com/envelope-zero/backend/v2/pkg/httperrors"
87 "github.com/envelope-zero/backend/v2/pkg/httputil"
98 "github.com/envelope-zero/backend/v2/pkg/models"
109 "github.com/gin-gonic/gin"
1110 "github.com/google/uuid"
12- "golang.org/x/exp/slices"
1311)
1412
1513type AccountListResponse struct {
@@ -32,6 +30,7 @@ type AccountQueryFilter struct {
3230 OnBudget bool `form:"onBudget"` // Is the account on-budget?
3331 External bool `form:"external"` // Is the account external?
3432 Hidden bool `form:"hidden"` // Is the account hidden?
33+ Search string `form:"search" filterField:"false"`
3534}
3635
3736func (f AccountQueryFilter ) ToCreate (c * gin.Context ) (models.AccountCreate , bool ) {
@@ -154,6 +153,7 @@ func (co Controller) CreateAccount(c *gin.Context) {
154153// @Param onBudget query bool false "Is the account on-budget?"
155154// @Param external query bool false "Is the account external?"
156155// @Param hidden query bool false "Is the account hidden?"
156+ // @Param search query string false "Search for this text in name and note"
157157func (co Controller ) GetAccounts (c * gin.Context ) {
158158 var filter AccountQueryFilter
159159 if err := c .Bind (& filter ); err != nil {
@@ -174,17 +174,7 @@ func (co Controller) GetAccounts(c *gin.Context) {
174174 AccountCreate : create ,
175175 }, queryFields ... )
176176
177- if filter .Name != "" {
178- query = query .Where ("name LIKE ?" , fmt .Sprintf ("%%%s%%" , filter .Name ))
179- } else if slices .Contains (setFields , "Name" ) {
180- query = query .Where ("name = ''" )
181- }
182-
183- if filter .Note != "" {
184- query = query .Where ("note LIKE ?" , fmt .Sprintf ("%%%s%%" , filter .Note ))
185- } else if slices .Contains (setFields , "Note" ) {
186- query = query .Where ("note = ''" )
187- }
177+ query = stringFilters (co .DB , query , setFields , filter .Name , filter .Note , filter .Search )
188178
189179 var accounts []models.Account
190180 if ! queryWithRetry (c , query .Find (& accounts )) {
0 commit comments