Improve editing and creating of transactions
This commit is contained in:
parent
faef975f1a
commit
4c6d21c2b4
@ -6,7 +6,7 @@ WHERE id = $1;
|
||||
INSERT INTO transactions
|
||||
(date, memo, amount, account_id, payee_id, category_id, group_id, status)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING *;
|
||||
RETURNING id;
|
||||
|
||||
-- name: UpdateTransaction :exec
|
||||
UPDATE transactions
|
||||
|
@ -15,7 +15,7 @@ const createTransaction = `-- name: CreateTransaction :one
|
||||
INSERT INTO transactions
|
||||
(date, memo, amount, account_id, payee_id, category_id, group_id, status)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING id, date, memo, amount, account_id, category_id, payee_id, group_id, status
|
||||
RETURNING id
|
||||
`
|
||||
|
||||
type CreateTransactionParams struct {
|
||||
@ -29,7 +29,7 @@ type CreateTransactionParams struct {
|
||||
Status TransactionStatus
|
||||
}
|
||||
|
||||
func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionParams) (Transaction, error) {
|
||||
func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionParams) (uuid.UUID, error) {
|
||||
row := q.db.QueryRowContext(ctx, createTransaction,
|
||||
arg.Date,
|
||||
arg.Memo,
|
||||
@ -40,19 +40,9 @@ func (q *Queries) CreateTransaction(ctx context.Context, arg CreateTransactionPa
|
||||
arg.GroupID,
|
||||
arg.Status,
|
||||
)
|
||||
var i Transaction
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Date,
|
||||
&i.Memo,
|
||||
&i.Amount,
|
||||
&i.AccountID,
|
||||
&i.CategoryID,
|
||||
&i.PayeeID,
|
||||
&i.GroupID,
|
||||
&i.Status,
|
||||
)
|
||||
return i, err
|
||||
var id uuid.UUID
|
||||
err := row.Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
|
||||
const deleteAllTransactions = `-- name: DeleteAllTransactions :execrows
|
||||
|
@ -89,12 +89,12 @@ func (*Handler) CreateReconcilationTransaction(amount numeric.Numeric, accountUU
|
||||
AccountID: accountUUID,
|
||||
Status: "Reconciled",
|
||||
}
|
||||
newTransaction, err := db.CreateTransaction(c.Request.Context(), createTransaction)
|
||||
transactionUUID, err := db.CreateTransaction(c.Request.Context(), createTransaction)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("insert new transaction: %w", err)
|
||||
}
|
||||
|
||||
transaction, err := db.GetTransaction(c.Request.Context(), newTransaction.ID)
|
||||
transaction, err := db.GetTransaction(c.Request.Context(), transactionUUID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("get created transaction: %w", err)
|
||||
}
|
||||
|
@ -70,11 +70,18 @@ func (h *Handler) newTransaction(c *gin.Context) {
|
||||
newTransaction.PayeeID = payeeID
|
||||
}
|
||||
|
||||
transaction, err := h.Service.CreateTransaction(c.Request.Context(), newTransaction)
|
||||
transactionUUID, err := h.Service.CreateTransaction(c.Request.Context(), newTransaction)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
transaction, err := h.Service.GetTransaction(c.Request.Context(), transactionUUID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("get transaction: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, transaction)
|
||||
}
|
||||
|
||||
@ -100,7 +107,16 @@ func (h *Handler) UpdateTransaction(payload NewTransactionPayload, amount numeri
|
||||
err := h.Service.UpdateTransaction(c.Request.Context(), editTransaction)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("edit transaction: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
transaction, err := h.Service.GetTransaction(c.Request.Context(), transactionUUID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("get transaction: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, transaction)
|
||||
}
|
||||
|
||||
func (h *Handler) CreateTransferForOtherAccount(newTransaction postgres.CreateTransactionParams, amount numeric.Numeric, payload NewTransactionPayload, c *gin.Context) error {
|
||||
|
@ -8,6 +8,8 @@ const props = defineProps<{
|
||||
transactionid: string
|
||||
}>()
|
||||
|
||||
const emit = defineEmits(["save"]);
|
||||
|
||||
const accountStore = useAccountStore();
|
||||
const TX = accountStore.Transactions.get(props.transactionid)!;
|
||||
const payeeType = ref<string|undefined>(undefined);
|
||||
@ -28,6 +30,7 @@ const payload = computed(() => JSON.stringify({
|
||||
function saveTransaction(e: MouseEvent) {
|
||||
e.preventDefault();
|
||||
accountStore.editTransaction(TX.ID, payload.value);
|
||||
emit('save');
|
||||
}
|
||||
</script>
|
||||
|
||||
|
@ -44,7 +44,7 @@ const Reconciling = computed(() => useAccountStore().Reconciling);
|
||||
<input type="checkbox" v-model="transaction.Reconciled" />
|
||||
</td>
|
||||
</tr>
|
||||
<TransactionEditRow v-if="edit" :transactionid="transaction.ID" />
|
||||
<TransactionEditRow v-if="edit" :transactionid="transaction.ID" @save="edit = false" />
|
||||
</template>
|
||||
|
||||
<style>
|
||||
|
@ -206,12 +206,13 @@ export const useAccountStore = defineStore("budget/account", {
|
||||
async saveTransaction(payload: string) {
|
||||
const result = await POST("/transaction/new", payload);
|
||||
const response = await result.json();
|
||||
this.CurrentAccount?.Transactions.unshift(response);
|
||||
this.AddTransaction(this.CurrentAccount!, response);
|
||||
this.CurrentAccount?.Transactions.unshift(response.ID);
|
||||
},
|
||||
async editTransaction(transactionid: string, payload: string) {
|
||||
const result = await POST("/transaction/" + transactionid, payload);
|
||||
const response = await result.json();
|
||||
this.CurrentAccount?.Transactions.unshift(response);
|
||||
this.AddTransaction(this.CurrentAccount!, response);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user