Add ability to edit payees

This commit is contained in:
Jan Bader 2021-12-28 20:40:53 +00:00
parent 5018e5b973
commit 1a4267186a
5 changed files with 55 additions and 25 deletions

View File

@ -13,6 +13,7 @@ type TransactionData struct {
Transaction *postgres.Transaction
Account *postgres.Account
Categories []postgres.GetCategoriesRow
Payees []postgres.Payee
}
func (h *Handler) transaction(c *gin.Context) {
@ -43,11 +44,18 @@ func (h *Handler) transaction(c *gin.Context) {
return
}
payees, err := h.Service.GetPayees(c.Request.Context(), data.Budget.ID)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}
d := TransactionData{
data,
&transaction,
&account,
categories,
payees,
}
c.HTML(http.StatusOK, "transaction.html", d)

View File

@ -72,6 +72,12 @@ func (h *Handler) newTransaction(c *gin.Context) {
return
}
transactionPayeeID, err := getNullUUIDFromForm(c, "payee_id")
if err != nil {
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("payee_id: %w", err))
return
}
transactionDate, succ := c.GetPostForm("date")
if !succ {
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("date missing"))
@ -95,39 +101,47 @@ func (h *Handler) newTransaction(c *gin.Context) {
transactionUUID, err := getNullUUIDFromParam(c, "transactionid")
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("parse transaction id: %w", err))
return
}
if !transactionUUID.Valid {
new := postgres.CreateTransactionParams{
Memo: transactionMemo,
Date: transactionDateValue,
Amount: amount,
AccountID: transactionAccountID,
PayeeID: uuid.NullUUID{},
PayeeID: transactionPayeeID,
CategoryID: transactionCategoryID,
}
_, err = h.Service.CreateTransaction(c.Request.Context(), new)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
}
return
}
} else {
_, delete := c.GetPostForm("delete")
if delete {
h.Service.DeleteTransaction(c.Request.Context(), transactionUUID.UUID)
err = h.Service.DeleteTransaction(c.Request.Context(), transactionUUID.UUID)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("delete transaction: %w", err))
}
return
}
update := postgres.UpdateTransactionParams{
ID: transactionUUID.UUID,
Memo: transactionMemo,
Date: transactionDateValue,
Amount: amount,
AccountID: transactionAccountID,
PayeeID: uuid.NullUUID{},
PayeeID: transactionPayeeID,
CategoryID: transactionCategoryID,
}
err = h.Service.UpdateTransaction(c.Request.Context(), update)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("update transaction: %w", err))
return
}
}
}

View File

@ -31,6 +31,7 @@ func (q *Queries) CreatePayee(ctx context.Context, arg CreatePayeeParams) (Payee
const getPayees = `-- name: GetPayees :many
SELECT payees.id, payees.budget_id, payees.name FROM payees
WHERE payees.budget_id = $1
ORDER BY name
`
func (q *Queries) GetPayees(ctx context.Context, budgetID uuid.UUID) ([]Payee, error) {

View File

@ -6,4 +6,5 @@ RETURNING *;
-- name: GetPayees :many
SELECT payees.* FROM payees
WHERE payees.budget_id = $1;
WHERE payees.budget_id = $1
ORDER BY name;

View File

@ -36,11 +36,17 @@
<select name="category_id" class="form-control">
<option value="" {{if not $.Transaction.CategoryID.Valid}}selected{{end}}>-- none --</option>
{{range .Categories}}
<option
value="{{.ID}}"
{{if and $.Transaction.CategoryID.Valid (eq .ID $.Transaction.CategoryID.UUID)}}selected{{end}}
>{{.Group}} : {{.Name}}</option>
{{end}}
<option value="{{.ID}}" {{if and $.Transaction.CategoryID.Valid (eq .ID $.Transaction.CategoryID.UUID)}}selected{{end}}>{{.Group}} : {{.Name}}</option>
{{- end}}
</select>
</div>
<div class="form-group">
<label for="payee_id">Payee</label>
<select name="payee_id" class="form-control">
<option value="" {{if not $.Transaction.PayeeID.Valid}}selected{{end}}>-- none --</option>
{{range .Payees}}
<option value="{{.ID}}" {{if and $.Transaction.PayeeID.Valid (eq .ID $.Transaction.PayeeID.UUID)}}selected{{end}}>{{.Name}}</option>
{{- end}}
</select>
</div>
<div class="form-group">