Add ability to edit payees
This commit is contained in:
parent
5018e5b973
commit
1a4267186a
@ -13,6 +13,7 @@ type TransactionData struct {
|
|||||||
Transaction *postgres.Transaction
|
Transaction *postgres.Transaction
|
||||||
Account *postgres.Account
|
Account *postgres.Account
|
||||||
Categories []postgres.GetCategoriesRow
|
Categories []postgres.GetCategoriesRow
|
||||||
|
Payees []postgres.Payee
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) transaction(c *gin.Context) {
|
func (h *Handler) transaction(c *gin.Context) {
|
||||||
@ -43,11 +44,18 @@ func (h *Handler) transaction(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
payees, err := h.Service.GetPayees(c.Request.Context(), data.Budget.ID)
|
||||||
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusNotFound, err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
d := TransactionData{
|
d := TransactionData{
|
||||||
data,
|
data,
|
||||||
&transaction,
|
&transaction,
|
||||||
&account,
|
&account,
|
||||||
categories,
|
categories,
|
||||||
|
payees,
|
||||||
}
|
}
|
||||||
|
|
||||||
c.HTML(http.StatusOK, "transaction.html", d)
|
c.HTML(http.StatusOK, "transaction.html", d)
|
||||||
|
@ -72,6 +72,12 @@ func (h *Handler) newTransaction(c *gin.Context) {
|
|||||||
return
|
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")
|
transactionDate, succ := c.GetPostForm("date")
|
||||||
if !succ {
|
if !succ {
|
||||||
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("date missing"))
|
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("date missing"))
|
||||||
@ -95,39 +101,47 @@ func (h *Handler) newTransaction(c *gin.Context) {
|
|||||||
|
|
||||||
transactionUUID, err := getNullUUIDFromParam(c, "transactionid")
|
transactionUUID, err := getNullUUIDFromParam(c, "transactionid")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("parse transaction id: %w", err))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !transactionUUID.Valid {
|
if !transactionUUID.Valid {
|
||||||
new := postgres.CreateTransactionParams{
|
new := postgres.CreateTransactionParams{
|
||||||
Memo: transactionMemo,
|
Memo: transactionMemo,
|
||||||
Date: transactionDateValue,
|
Date: transactionDateValue,
|
||||||
Amount: amount,
|
Amount: amount,
|
||||||
AccountID: transactionAccountID,
|
AccountID: transactionAccountID,
|
||||||
PayeeID: uuid.NullUUID{},
|
PayeeID: transactionPayeeID,
|
||||||
CategoryID: transactionCategoryID,
|
CategoryID: transactionCategoryID,
|
||||||
}
|
}
|
||||||
_, err = h.Service.CreateTransaction(c.Request.Context(), new)
|
_, err = h.Service.CreateTransaction(c.Request.Context(), new)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
|
||||||
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
_, delete := c.GetPostForm("delete")
|
_, delete := c.GetPostForm("delete")
|
||||||
if 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{
|
update := postgres.UpdateTransactionParams{
|
||||||
|
ID: transactionUUID.UUID,
|
||||||
Memo: transactionMemo,
|
Memo: transactionMemo,
|
||||||
Date: transactionDateValue,
|
Date: transactionDateValue,
|
||||||
Amount: amount,
|
Amount: amount,
|
||||||
AccountID: transactionAccountID,
|
AccountID: transactionAccountID,
|
||||||
PayeeID: uuid.NullUUID{},
|
PayeeID: transactionPayeeID,
|
||||||
CategoryID: transactionCategoryID,
|
CategoryID: transactionCategoryID,
|
||||||
}
|
}
|
||||||
err = h.Service.UpdateTransaction(c.Request.Context(), update)
|
err = h.Service.UpdateTransaction(c.Request.Context(), update)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("update transaction: %w", err))
|
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("update transaction: %w", err))
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -31,6 +31,7 @@ func (q *Queries) CreatePayee(ctx context.Context, arg CreatePayeeParams) (Payee
|
|||||||
const getPayees = `-- name: GetPayees :many
|
const getPayees = `-- name: GetPayees :many
|
||||||
SELECT payees.id, payees.budget_id, payees.name FROM payees
|
SELECT payees.id, payees.budget_id, payees.name FROM payees
|
||||||
WHERE payees.budget_id = $1
|
WHERE payees.budget_id = $1
|
||||||
|
ORDER BY name
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetPayees(ctx context.Context, budgetID uuid.UUID) ([]Payee, error) {
|
func (q *Queries) GetPayees(ctx context.Context, budgetID uuid.UUID) ([]Payee, error) {
|
||||||
|
@ -6,4 +6,5 @@ RETURNING *;
|
|||||||
|
|
||||||
-- name: GetPayees :many
|
-- name: GetPayees :many
|
||||||
SELECT payees.* FROM payees
|
SELECT payees.* FROM payees
|
||||||
WHERE payees.budget_id = $1;
|
WHERE payees.budget_id = $1
|
||||||
|
ORDER BY name;
|
@ -36,11 +36,17 @@
|
|||||||
<select name="category_id" class="form-control">
|
<select name="category_id" class="form-control">
|
||||||
<option value="" {{if not $.Transaction.CategoryID.Valid}}selected{{end}}>-- none --</option>
|
<option value="" {{if not $.Transaction.CategoryID.Valid}}selected{{end}}>-- none --</option>
|
||||||
{{range .Categories}}
|
{{range .Categories}}
|
||||||
<option
|
<option value="{{.ID}}" {{if and $.Transaction.CategoryID.Valid (eq .ID $.Transaction.CategoryID.UUID)}}selected{{end}}>{{.Group}} : {{.Name}}</option>
|
||||||
value="{{.ID}}"
|
{{- end}}
|
||||||
{{if and $.Transaction.CategoryID.Valid (eq .ID $.Transaction.CategoryID.UUID)}}selected{{end}}
|
</select>
|
||||||
>{{.Group}} : {{.Name}}</option>
|
</div>
|
||||||
{{end}}
|
<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>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
Loading…
x
Reference in New Issue
Block a user