From d89a8f4e2ede65791cfafa61ea2b238f53ae8f72 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Wed, 23 Feb 2022 14:47:40 +0000 Subject: [PATCH] Switch between payees and accounts depending on prefix --- postgres/accounts.sql.go | 41 +++++++++++++++++++++++++++++++++++ postgres/queries/accounts.sql | 6 +++++ server/autocomplete.go | 39 ++++++++++++++++++++++++--------- 3 files changed, 76 insertions(+), 10 deletions(-) diff --git a/postgres/accounts.sql.go b/postgres/accounts.sql.go index 50d1a5b..c50bc8f 100644 --- a/postgres/accounts.sql.go +++ b/postgres/accounts.sql.go @@ -127,3 +127,44 @@ func (q *Queries) GetAccountsWithBalance(ctx context.Context, budgetID uuid.UUID } return items, nil } + +const searchAccounts = `-- name: SearchAccounts :many +SELECT accounts.id, accounts.budget_id, accounts.name FROM accounts +WHERE accounts.budget_id = $1 +AND accounts.name LIKE $2 +ORDER BY accounts.name +` + +type SearchAccountsParams struct { + BudgetID uuid.UUID + Search string +} + +type SearchAccountsRow struct { + ID uuid.UUID + BudgetID uuid.UUID + Name string +} + +func (q *Queries) SearchAccounts(ctx context.Context, arg SearchAccountsParams) ([]SearchAccountsRow, error) { + rows, err := q.db.QueryContext(ctx, searchAccounts, arg.BudgetID, arg.Search) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SearchAccountsRow + for rows.Next() { + var i SearchAccountsRow + if err := rows.Scan(&i.ID, &i.BudgetID, &i.Name); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/postgres/queries/accounts.sql b/postgres/queries/accounts.sql index fb48404..a7b969e 100644 --- a/postgres/queries/accounts.sql +++ b/postgres/queries/accounts.sql @@ -19,4 +19,10 @@ FROM accounts LEFT JOIN transactions ON transactions.account_id = accounts.id AND transactions.date < NOW() WHERE accounts.budget_id = $1 GROUP BY accounts.id, accounts.name +ORDER BY accounts.name; + +-- name: SearchAccounts :many +SELECT accounts.id, accounts.budget_id, accounts.name FROM accounts +WHERE accounts.budget_id = @budget_id +AND accounts.name LIKE @search ORDER BY accounts.name; \ No newline at end of file diff --git a/server/autocomplete.go b/server/autocomplete.go index 46dfb3c..5472430 100644 --- a/server/autocomplete.go +++ b/server/autocomplete.go @@ -2,6 +2,7 @@ package server import ( "net/http" + "strings" "git.javil.eu/jacob1123/budgeteer/postgres" "github.com/gin-gonic/gin" @@ -39,15 +40,33 @@ func (h *Handler) autocompletePayee(c *gin.Context) { } query := c.Request.URL.Query().Get("s") - searchParams := postgres.SearchPayeesParams{ - BudgetID: budgetUUID, - Search: query + "%", - } - payees, err := h.Service.SearchPayees(c.Request.Context(), searchParams) - if err != nil { - c.AbortWithError(http.StatusInternalServerError, err) - return - } - c.JSON(http.StatusOK, payees) + transferPrefix := "Transfer" + if strings.HasPrefix(query, transferPrefix) { + searchParams := postgres.SearchAccountsParams{ + BudgetID: budgetUUID, + Search: "%" + strings.Trim(query[len(transferPrefix):], " \t\n:") + "%", + } + + accounts, err := h.Service.SearchAccounts(c.Request.Context(), searchParams) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + c.JSON(http.StatusOK, accounts) + } else { + searchParams := postgres.SearchPayeesParams{ + BudgetID: budgetUUID, + Search: query + "%", + } + + payees, err := h.Service.SearchPayees(c.Request.Context(), searchParams) + if err != nil { + c.AbortWithError(http.StatusInternalServerError, err) + return + } + + c.JSON(http.StatusOK, payees) + } }