Implement autocomplete for categories
This commit is contained in:
parent
056071c6e6
commit
057c576831
@ -60,6 +60,28 @@ func getDate(c *gin.Context) (time.Time, error) {
|
||||
return getFirstOfMonth(year, month, time.Now().Location()), nil
|
||||
}
|
||||
|
||||
func (h *Handler) autocompleteCategories(c *gin.Context) {
|
||||
budgetID := c.Param("budgetid")
|
||||
budgetUUID, err := uuid.Parse(budgetID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusBadRequest, fmt.Errorf("budgetid missing from URL"))
|
||||
return
|
||||
}
|
||||
|
||||
query := c.Request.URL.Query().Get("s")
|
||||
searchParams := postgres.SearchCategoriesParams{
|
||||
BudgetID: budgetUUID,
|
||||
Search: "%" + query + "%",
|
||||
}
|
||||
categories, err := h.Service.SearchCategories(c.Request.Context(), searchParams)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, categories)
|
||||
}
|
||||
|
||||
func (h *Handler) autocompletePayee(c *gin.Context) {
|
||||
budgetID := c.Param("budgetid")
|
||||
budgetUUID, err := uuid.Parse(budgetID)
|
||||
|
@ -67,6 +67,7 @@ func (h *Handler) Serve() {
|
||||
authenticated.GET("/admin/clear-database", h.clearDatabase)
|
||||
authenticated.GET("/budget/:budgetid", h.budgeting)
|
||||
authenticated.GET("/budget/:budgetid/autocomplete/payees", h.autocompletePayee)
|
||||
authenticated.GET("/budget/:budgetid/autocomplete/categories", h.autocompleteCategories)
|
||||
authenticated.DELETE("/budget/:budgetid", h.deleteBudget)
|
||||
authenticated.POST("/budget/:budgetid/import/ynab", h.importYNAB)
|
||||
authenticated.POST("/budget/:budgetid/settings/clear", h.clearBudget)
|
||||
|
@ -116,3 +116,44 @@ func (q *Queries) GetCategoryGroups(ctx context.Context, budgetID uuid.UUID) ([]
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const searchCategories = `-- name: SearchCategories :many
|
||||
SELECT CONCAT(category_groups.name, ' : ', categories.name) as name, categories.id FROM categories
|
||||
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
|
||||
WHERE category_groups.budget_id = $1
|
||||
AND categories.name LIKE $2
|
||||
ORDER BY category_groups.name, categories.name
|
||||
`
|
||||
|
||||
type SearchCategoriesParams struct {
|
||||
BudgetID uuid.UUID
|
||||
Search string
|
||||
}
|
||||
|
||||
type SearchCategoriesRow struct {
|
||||
Name interface{}
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) SearchCategories(ctx context.Context, arg SearchCategoriesParams) ([]SearchCategoriesRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, searchCategories, arg.BudgetID, arg.Search)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []SearchCategoriesRow
|
||||
for rows.Next() {
|
||||
var i SearchCategoriesRow
|
||||
if err := rows.Scan(&i.Name, &i.ID); 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
|
||||
}
|
||||
|
@ -18,4 +18,12 @@ RETURNING *;
|
||||
SELECT categories.*, category_groups.name as group FROM categories
|
||||
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
|
||||
WHERE category_groups.budget_id = $1
|
||||
ORDER BY category_groups.name, categories.name;
|
||||
ORDER BY category_groups.name, categories.name;
|
||||
|
||||
-- name: SearchCategories :many
|
||||
SELECT CONCAT(category_groups.name, ' : ', categories.name) as name, categories.id FROM categories
|
||||
INNER JOIN category_groups ON categories.category_group_id = category_groups.id
|
||||
WHERE category_groups.budget_id = @budget_id
|
||||
AND categories.name LIKE @search
|
||||
ORDER BY category_groups.name, categories.name;
|
||||
--ORDER BY levenshtein(payees.name, $2);
|
@ -44,7 +44,7 @@ export default defineComponent({
|
||||
<Autocomplete v-model="Payee" type="payees" />
|
||||
</td>
|
||||
<td style="max-width: 200px;">
|
||||
<input type="text" v-model="Category" />
|
||||
<Autocomplete v-model="Category" type="categories" />
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" v-model="Memo" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user