Implement categories for new transactions
This commit is contained in:
parent
1ab1fa74e0
commit
d0ad0dcb3a
@ -11,10 +11,13 @@ import (
|
||||
type AccountData struct {
|
||||
AlwaysNeededData
|
||||
Account *postgres.Account
|
||||
Categories []postgres.GetCategoriesRow
|
||||
Transactions []postgres.GetTransactionsForAccountRow
|
||||
}
|
||||
|
||||
func (h *Handler) account(c *gin.Context) {
|
||||
data := c.MustGet("data").(AlwaysNeededData)
|
||||
|
||||
accountID := c.Param("accountid")
|
||||
accountUUID, err := uuid.Parse(accountID)
|
||||
if err != nil {
|
||||
@ -28,6 +31,12 @@ func (h *Handler) account(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
categories, err := h.Service.GetCategories(c.Request.Context(), data.Budget.ID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
return
|
||||
}
|
||||
|
||||
transactions, err := h.Service.GetTransactionsForAccount(c.Request.Context(), accountUUID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
@ -35,8 +44,9 @@ func (h *Handler) account(c *gin.Context) {
|
||||
}
|
||||
|
||||
d := AccountData{
|
||||
c.MustGet("data").(AlwaysNeededData),
|
||||
data,
|
||||
&account,
|
||||
categories,
|
||||
transactions,
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,7 @@ import (
|
||||
type AllAccountsData struct {
|
||||
AlwaysNeededData
|
||||
Account *postgres.Account
|
||||
Categories []postgres.GetCategoriesRow
|
||||
Transactions []postgres.GetTransactionsForBudgetRow
|
||||
}
|
||||
|
||||
@ -23,6 +24,12 @@ func (h *Handler) allAccounts(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
categories, err := h.Service.GetCategories(c.Request.Context(), budgetUUID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotFound, err)
|
||||
return
|
||||
}
|
||||
|
||||
transactions, err := h.Service.GetTransactionsForBudget(c.Request.Context(), budgetUUID)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, err)
|
||||
@ -34,6 +41,7 @@ func (h *Handler) allAccounts(c *gin.Context) {
|
||||
&postgres.Account{
|
||||
Name: "All accounts",
|
||||
},
|
||||
categories,
|
||||
transactions,
|
||||
}
|
||||
|
||||
|
@ -10,17 +10,48 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func getUUID(c *gin.Context, name string) (uuid.UUID, error) {
|
||||
value, succ := c.GetPostForm(name)
|
||||
if !succ {
|
||||
return uuid.UUID{}, fmt.Errorf("not set")
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(value)
|
||||
if err != nil {
|
||||
return uuid.UUID{}, fmt.Errorf("not a valid uuid: %w", err)
|
||||
}
|
||||
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func getNullUUID(c *gin.Context, name string) (uuid.NullUUID, error) {
|
||||
value, succ := c.GetPostForm(name)
|
||||
if !succ {
|
||||
return uuid.NullUUID{}, nil
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(value)
|
||||
if err != nil {
|
||||
return uuid.NullUUID{}, fmt.Errorf("not a valid uuid: %w", err)
|
||||
}
|
||||
|
||||
return uuid.NullUUID{
|
||||
UUID: id,
|
||||
Valid: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (h *Handler) newTransaction(c *gin.Context) {
|
||||
transactionMemo, _ := c.GetPostForm("memo")
|
||||
transactionAccount, succ := c.GetPostForm("account_id")
|
||||
if !succ {
|
||||
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id missing"))
|
||||
transactionAccountID, err := getUUID(c, "account_id")
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotAcceptable, err)
|
||||
return
|
||||
}
|
||||
|
||||
transactionAccountID, err := uuid.Parse(transactionAccount)
|
||||
if !succ {
|
||||
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id is not a valid uuid"))
|
||||
transactionCategoryID, err := getNullUUID(c, "category_id")
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotAcceptable, err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -50,7 +81,7 @@ func (h *Handler) newTransaction(c *gin.Context) {
|
||||
Amount: amount,
|
||||
AccountID: transactionAccountID,
|
||||
PayeeID: uuid.NullUUID{},
|
||||
CategoryID: uuid.NullUUID{},
|
||||
CategoryID: transactionCategoryID,
|
||||
}
|
||||
_, err = h.Service.CreateTransaction(c.Request.Context(), new)
|
||||
if err != nil {
|
||||
|
@ -14,7 +14,7 @@
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">New Budget</h5>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
</div>
|
||||
@ -30,7 +30,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
||||
<input type="submit" class="btn btn-primary" value="Create" class="form-control" />
|
||||
</div>
|
||||
</form>
|
||||
|
@ -14,13 +14,22 @@
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title">New Transaction</h5>
|
||||
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
|
||||
<button type="button" class="close" data-bs-dismiss="modal" aria-label="Close">
|
||||
<span aria-hidden="true">×</span>
|
||||
</button>
|
||||
</div>
|
||||
<form id="newtransactionform" action="/api/v1/transaction/new" method="POST">
|
||||
<div class="modal-body">
|
||||
<input type="hidden" name="account_id" value="{{.Account.ID}}" />
|
||||
<div class="form-group">
|
||||
<label for="category_id">Category</label>
|
||||
<select name="category_id" class="form-control">
|
||||
<option value="">-- none --</option>
|
||||
{{range .Categories}}
|
||||
<option value="{{.ID}}">{{.Group}} : {{.Name}}</option>
|
||||
{{end}}
|
||||
</select>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="date">Date</label>
|
||||
<input type="date" name="date" class="form-control" value="{{now.Format "2006-01-02"}}" />
|
||||
@ -35,7 +44,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
|
||||
<input type="submit" class="btn btn-primary" value="Create" class="form-control" />
|
||||
</div>
|
||||
</form>
|
||||
|
Loading…
x
Reference in New Issue
Block a user