Implement categories for new transactions

This commit is contained in:
Jan Bader 2021-12-14 14:41:10 +00:00
parent 1ab1fa74e0
commit d0ad0dcb3a
5 changed files with 70 additions and 12 deletions

View File

@ -11,10 +11,13 @@ import (
type AccountData struct { type AccountData struct {
AlwaysNeededData AlwaysNeededData
Account *postgres.Account Account *postgres.Account
Categories []postgres.GetCategoriesRow
Transactions []postgres.GetTransactionsForAccountRow Transactions []postgres.GetTransactionsForAccountRow
} }
func (h *Handler) account(c *gin.Context) { func (h *Handler) account(c *gin.Context) {
data := c.MustGet("data").(AlwaysNeededData)
accountID := c.Param("accountid") accountID := c.Param("accountid")
accountUUID, err := uuid.Parse(accountID) accountUUID, err := uuid.Parse(accountID)
if err != nil { if err != nil {
@ -28,6 +31,12 @@ func (h *Handler) account(c *gin.Context) {
return 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) transactions, err := h.Service.GetTransactionsForAccount(c.Request.Context(), accountUUID)
if err != nil { if err != nil {
c.AbortWithError(http.StatusNotFound, err) c.AbortWithError(http.StatusNotFound, err)
@ -35,8 +44,9 @@ func (h *Handler) account(c *gin.Context) {
} }
d := AccountData{ d := AccountData{
c.MustGet("data").(AlwaysNeededData), data,
&account, &account,
categories,
transactions, transactions,
} }

View File

@ -12,6 +12,7 @@ import (
type AllAccountsData struct { type AllAccountsData struct {
AlwaysNeededData AlwaysNeededData
Account *postgres.Account Account *postgres.Account
Categories []postgres.GetCategoriesRow
Transactions []postgres.GetTransactionsForBudgetRow Transactions []postgres.GetTransactionsForBudgetRow
} }
@ -23,6 +24,12 @@ func (h *Handler) allAccounts(c *gin.Context) {
return 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) transactions, err := h.Service.GetTransactionsForBudget(c.Request.Context(), budgetUUID)
if err != nil { if err != nil {
c.AbortWithError(http.StatusInternalServerError, err) c.AbortWithError(http.StatusInternalServerError, err)
@ -34,6 +41,7 @@ func (h *Handler) allAccounts(c *gin.Context) {
&postgres.Account{ &postgres.Account{
Name: "All accounts", Name: "All accounts",
}, },
categories,
transactions, transactions,
} }

View File

@ -10,17 +10,48 @@ import (
"github.com/google/uuid" "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) { func (h *Handler) newTransaction(c *gin.Context) {
transactionMemo, _ := c.GetPostForm("memo") transactionMemo, _ := c.GetPostForm("memo")
transactionAccount, succ := c.GetPostForm("account_id") transactionAccountID, err := getUUID(c, "account_id")
if !succ { if err != nil {
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id missing")) c.AbortWithError(http.StatusNotAcceptable, err)
return return
} }
transactionAccountID, err := uuid.Parse(transactionAccount) transactionCategoryID, err := getNullUUID(c, "category_id")
if !succ { if err != nil {
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id is not a valid uuid")) c.AbortWithError(http.StatusNotAcceptable, err)
return return
} }
@ -50,7 +81,7 @@ func (h *Handler) newTransaction(c *gin.Context) {
Amount: amount, Amount: amount,
AccountID: transactionAccountID, AccountID: transactionAccountID,
PayeeID: uuid.NullUUID{}, PayeeID: uuid.NullUUID{},
CategoryID: uuid.NullUUID{}, CategoryID: transactionCategoryID,
} }
_, err = h.Service.CreateTransaction(c.Request.Context(), new) _, err = h.Service.CreateTransaction(c.Request.Context(), new)
if err != nil { if err != nil {

View File

@ -14,7 +14,7 @@
<div class="modal-content"> <div class="modal-content">
<div class="modal-header"> <div class="modal-header">
<h5 class="modal-title">New Budget</h5> <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">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
@ -30,7 +30,7 @@
</div> </div>
</div> </div>
<div class="modal-footer"> <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" /> <input type="submit" class="btn btn-primary" value="Create" class="form-control" />
</div> </div>
</form> </form>

View File

@ -14,13 +14,22 @@
<div class="modal-content"> <div class="modal-content">
<div class="modal-header"> <div class="modal-header">
<h5 class="modal-title">New Transaction</h5> <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">&times;</span> <span aria-hidden="true">&times;</span>
</button> </button>
</div> </div>
<form id="newtransactionform" action="/api/v1/transaction/new" method="POST"> <form id="newtransactionform" action="/api/v1/transaction/new" method="POST">
<div class="modal-body"> <div class="modal-body">
<input type="hidden" name="account_id" value="{{.Account.ID}}" /> <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"> <div class="form-group">
<label for="date">Date</label> <label for="date">Date</label>
<input type="date" name="date" class="form-control" value="{{now.Format "2006-01-02"}}" /> <input type="date" name="date" class="form-control" value="{{now.Format "2006-01-02"}}" />
@ -35,7 +44,7 @@
</div> </div>
</div> </div>
<div class="modal-footer"> <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" /> <input type="submit" class="btn btn-primary" value="Create" class="form-control" />
</div> </div>
</form> </form>