From ae9e9d34c961acf10bc798901a659f444d437754 Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Fri, 25 Feb 2022 22:28:22 +0000 Subject: [PATCH] Use type instead of isAccount flag --- postgres/accounts.sql.go | 12 ++++++------ postgres/categories.sql.go | 6 ++++-- postgres/payees.sql.go | 22 +++++++++++++++++----- postgres/queries/accounts.sql | 2 +- postgres/queries/categories.sql | 3 ++- postgres/queries/payees.sql | 2 +- server/transaction.go | 13 ++++++++----- web/src/components/Autocomplete.vue | 9 +++++++-- web/src/components/TransactionEditRow.vue | 6 ++++-- web/src/components/TransactionInputRow.vue | 5 ++++- 10 files changed, 54 insertions(+), 26 deletions(-) diff --git a/postgres/accounts.sql.go b/postgres/accounts.sql.go index fa5ee92..31f9aa8 100644 --- a/postgres/accounts.sql.go +++ b/postgres/accounts.sql.go @@ -130,7 +130,7 @@ func (q *Queries) GetAccountsWithBalance(ctx context.Context, budgetID uuid.UUID } const searchAccounts = `-- name: SearchAccounts :many -SELECT accounts.id, accounts.budget_id, accounts.name, true as is_account FROM accounts +SELECT accounts.id, accounts.budget_id, accounts.name, 'account' as type FROM accounts WHERE accounts.budget_id = $1 AND accounts.name LIKE $2 ORDER BY accounts.name @@ -142,10 +142,10 @@ type SearchAccountsParams struct { } type SearchAccountsRow struct { - ID uuid.UUID - BudgetID uuid.UUID - Name string - IsAccount bool + ID uuid.UUID + BudgetID uuid.UUID + Name string + Type interface{} } func (q *Queries) SearchAccounts(ctx context.Context, arg SearchAccountsParams) ([]SearchAccountsRow, error) { @@ -161,7 +161,7 @@ func (q *Queries) SearchAccounts(ctx context.Context, arg SearchAccountsParams) &i.ID, &i.BudgetID, &i.Name, - &i.IsAccount, + &i.Type, ); err != nil { return nil, err } diff --git a/postgres/categories.sql.go b/postgres/categories.sql.go index 7ddb062..fbf295e 100644 --- a/postgres/categories.sql.go +++ b/postgres/categories.sql.go @@ -118,7 +118,8 @@ func (q *Queries) GetCategoryGroups(ctx context.Context, budgetID uuid.UUID) ([] } const searchCategories = `-- name: SearchCategories :many -SELECT CONCAT(category_groups.name, ' : ', categories.name) as name, categories.id FROM categories +SELECT CONCAT(category_groups.name, ' : ', categories.name) as name, categories.id, 'category' as type +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 @@ -133,6 +134,7 @@ type SearchCategoriesParams struct { type SearchCategoriesRow struct { Name interface{} ID uuid.UUID + Type interface{} } func (q *Queries) SearchCategories(ctx context.Context, arg SearchCategoriesParams) ([]SearchCategoriesRow, error) { @@ -144,7 +146,7 @@ func (q *Queries) SearchCategories(ctx context.Context, arg SearchCategoriesPara var items []SearchCategoriesRow for rows.Next() { var i SearchCategoriesRow - if err := rows.Scan(&i.Name, &i.ID); err != nil { + if err := rows.Scan(&i.Name, &i.ID, &i.Type); err != nil { return nil, err } items = append(items, i) diff --git a/postgres/payees.sql.go b/postgres/payees.sql.go index 8e39497..446097f 100644 --- a/postgres/payees.sql.go +++ b/postgres/payees.sql.go @@ -58,7 +58,7 @@ func (q *Queries) GetPayees(ctx context.Context, budgetID uuid.UUID) ([]Payee, e } const searchPayees = `-- name: SearchPayees :many -SELECT payees.id, payees.budget_id, payees.name FROM payees +SELECT payees.id, payees.budget_id, payees.name, 'payee' as type FROM payees WHERE payees.budget_id = $1 AND payees.name LIKE $2 ORDER BY payees.name @@ -69,16 +69,28 @@ type SearchPayeesParams struct { Search string } -func (q *Queries) SearchPayees(ctx context.Context, arg SearchPayeesParams) ([]Payee, error) { +type SearchPayeesRow struct { + ID uuid.UUID + BudgetID uuid.UUID + Name string + Type interface{} +} + +func (q *Queries) SearchPayees(ctx context.Context, arg SearchPayeesParams) ([]SearchPayeesRow, error) { rows, err := q.db.QueryContext(ctx, searchPayees, arg.BudgetID, arg.Search) if err != nil { return nil, err } defer rows.Close() - var items []Payee + var items []SearchPayeesRow for rows.Next() { - var i Payee - if err := rows.Scan(&i.ID, &i.BudgetID, &i.Name); err != nil { + var i SearchPayeesRow + if err := rows.Scan( + &i.ID, + &i.BudgetID, + &i.Name, + &i.Type, + ); err != nil { return nil, err } items = append(items, i) diff --git a/postgres/queries/accounts.sql b/postgres/queries/accounts.sql index 97e581b..ac291d5 100644 --- a/postgres/queries/accounts.sql +++ b/postgres/queries/accounts.sql @@ -22,7 +22,7 @@ GROUP BY accounts.id, accounts.name ORDER BY accounts.name; -- name: SearchAccounts :many -SELECT accounts.id, accounts.budget_id, accounts.name, true as is_account FROM accounts +SELECT accounts.id, accounts.budget_id, accounts.name, 'account' as type FROM accounts WHERE accounts.budget_id = @budget_id AND accounts.name LIKE @search ORDER BY accounts.name; diff --git a/postgres/queries/categories.sql b/postgres/queries/categories.sql index 2ff245b..fb507af 100644 --- a/postgres/queries/categories.sql +++ b/postgres/queries/categories.sql @@ -21,7 +21,8 @@ WHERE category_groups.budget_id = $1 ORDER BY category_groups.name, categories.name; -- name: SearchCategories :many -SELECT CONCAT(category_groups.name, ' : ', categories.name) as name, categories.id FROM categories +SELECT CONCAT(category_groups.name, ' : ', categories.name) as name, categories.id, 'category' as type +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 diff --git a/postgres/queries/payees.sql b/postgres/queries/payees.sql index b82e9b1..ed38a5f 100644 --- a/postgres/queries/payees.sql +++ b/postgres/queries/payees.sql @@ -10,7 +10,7 @@ WHERE payees.budget_id = $1 ORDER BY name; -- name: SearchPayees :many -SELECT payees.* FROM payees +SELECT payees.*, 'payee' as type FROM payees WHERE payees.budget_id = @budget_id AND payees.name LIKE @search ORDER BY payees.name; diff --git a/server/transaction.go b/server/transaction.go index 7906624..30c1d77 100644 --- a/server/transaction.go +++ b/server/transaction.go @@ -15,9 +15,9 @@ import ( type NewTransactionPayload struct { Date JSONDate `json:"date"` Payee struct { - ID uuid.NullUUID - Name string - IsAccount bool + ID uuid.NullUUID + Name string + Type string } `json:"payee"` CategoryID uuid.NullUUID `json:"categoryId"` Memo string `json:"memo"` @@ -56,7 +56,7 @@ func (h *Handler) newTransaction(c *gin.Context) { AccountID: payload.AccountID, } - if payload.Payee.IsAccount { + if payload.Payee.Type == "account" { err := h.CreateTransferForOtherAccount(newTransaction, amount, payload, c) if err != nil { c.AbortWithError(http.StatusInternalServerError, err) @@ -103,7 +103,10 @@ func (h *Handler) CreateTransferForOtherAccount(newTransaction postgres.CreateTr newTransaction.CategoryID = uuid.NullUUID{} _, err := h.Service.CreateTransaction(c.Request.Context(), newTransaction) - return fmt.Errorf("create transfer transaction: %w", err) + if err != nil { + return fmt.Errorf("create transfer transaction: %w", err) + } + return nil } func GetPayeeID(context context.Context, payload NewTransactionPayload, h *Handler) (uuid.NullUUID, error) { diff --git a/web/src/components/Autocomplete.vue b/web/src/components/Autocomplete.vue index 19a8055..5568a60 100644 --- a/web/src/components/Autocomplete.vue +++ b/web/src/components/Autocomplete.vue @@ -6,23 +6,26 @@ import { useBudgetsStore } from "../stores/budget"; export interface Suggestion { ID: string Name: string + Type: string } const props = defineProps<{ text: String, id: String | undefined, - model: String + model: String, + type?: string | undefined, }>(); const SearchQuery = ref(props.text || ""); const Suggestions = ref>([]); -const emit = defineEmits(["update:id", "update:text"]); +const emit = defineEmits(["update:id", "update:text", "update:type"]); watch(SearchQuery, () => { load(SearchQuery.value); }); function load(text: String) { emit('update:id', null); emit('update:text', text); + emit('update:type', undefined); if (text == "") { Suggestions.value = []; return; @@ -53,6 +56,7 @@ function keypress(e: KeyboardEvent) { function selectElement(element: Suggestion) { emit('update:id', element.ID); emit('update:text', element.Name); + emit('update:type', element.Type); Suggestions.value = []; }; function select(e: MouseEvent) { @@ -67,6 +71,7 @@ function select(e: MouseEvent) { function clear() { emit('update:id', null); emit('update:text', SearchQuery.value); + emit('update:type', undefined); }; diff --git a/web/src/components/TransactionEditRow.vue b/web/src/components/TransactionEditRow.vue index 4465dcc..b5e89b1 100644 --- a/web/src/components/TransactionEditRow.vue +++ b/web/src/components/TransactionEditRow.vue @@ -1,5 +1,5 @@