Switch between payees and accounts depending on prefix
This commit is contained in:
parent
dae9abeeea
commit
d89a8f4e2e
@ -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
|
||||
}
|
||||
|
@ -20,3 +20,9 @@ LEFT JOIN transactions ON transactions.account_id = accounts.id AND transactions
|
||||
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;
|
@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.javil.eu/jacob1123/budgeteer/postgres"
|
||||
"github.com/gin-gonic/gin"
|
||||
@ -39,10 +40,27 @@ func (h *Handler) autocompletePayee(c *gin.Context) {
|
||||
}
|
||||
|
||||
query := c.Request.URL.Query().Get("s")
|
||||
|
||||
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)
|
||||
@ -51,3 +69,4 @@ func (h *Handler) autocompletePayee(c *gin.Context) {
|
||||
|
||||
c.JSON(http.StatusOK, payees)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user