Implement new transaction

This commit is contained in:
Jan Bader 2022-02-07 16:17:14 +00:00
parent 95fcb9a586
commit 9dad1dabbd
3 changed files with 71 additions and 68 deletions

30
http/json-date.go Normal file
View File

@ -0,0 +1,30 @@
package http
import (
"encoding/json"
"strings"
"time"
)
type JSONDate time.Time
// Implement Marshaler and Unmarshaler interface
func (j *JSONDate) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
t, err := time.Parse("2006-01-02", s)
if err != nil {
return err
}
*j = JSONDate(t)
return nil
}
func (j JSONDate) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Time(j))
}
// Maybe a Format function for printing your date
func (j JSONDate) Format(s string) string {
t := time.Time(j)
return t.Format(s)
}

View File

@ -1,10 +1,8 @@
package http package http
import ( import (
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"time" "time"
"git.javil.eu/jacob1123/budgeteer/postgres" "git.javil.eu/jacob1123/budgeteer/postgres"
@ -22,32 +20,11 @@ type NewTransactionPayload struct {
ID uuid.NullUUID ID uuid.NullUUID
Name string Name string
} `json:"category"` } `json:"category"`
Memo string `json:"memo"` Memo string `json:"memo"`
Amount string `json:"amount"` Amount string `json:"amount"`
BudgetID uuid.UUID `json:"budget_id"` BudgetID uuid.UUID `json:"budget_id"`
} AccountID uuid.UUID `json:"account_id"`
State string `json:"state"`
type JSONDate time.Time
// Implement Marshaler and Unmarshaler interface
func (j *JSONDate) UnmarshalJSON(b []byte) error {
s := strings.Trim(string(b), "\"")
t, err := time.Parse("2006-01-02", s)
if err != nil {
return err
}
*j = JSONDate(t)
return nil
}
func (j JSONDate) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Time(j))
}
// Maybe a Format function for printing your date
func (j JSONDate) Format(s string) string {
t := time.Time(j)
return t.Format(s)
} }
func (h *Handler) newTransaction(c *gin.Context) { func (h *Handler) newTransaction(c *gin.Context) {
@ -59,25 +36,45 @@ func (h *Handler) newTransaction(c *gin.Context) {
} }
fmt.Printf("%v\n", payload) fmt.Printf("%v\n", payload)
return
transactionAccountID, err := getUUID(c, "account_id")
if err != nil {
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id: %w", err))
return
}
amount := postgres.Numeric{} amount := postgres.Numeric{}
amount.Set(payload.Amount) amount.Set(payload.Amount)
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)) c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("parse transaction id: %w", err))
return return
}*/
//if !transactionUUID.Valid {
new := postgres.CreateTransactionParams{
Memo: payload.Memo,
Date: time.Time(payload.Date),
Amount: amount,
AccountID: payload.AccountID,
PayeeID: payload.Payee.ID, //TODO handle new payee
CategoryID: payload.Category.ID, //TODO handle new category
Status: postgres.TransactionStatus(payload.State),
}
_, err = h.Service.CreateTransaction(c.Request.Context(), new)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
} }
if !transactionUUID.Valid { return
new := postgres.CreateTransactionParams{ // }
/*
_, delete := c.GetPostForm("delete")
if delete {
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: payload.Memo, Memo: payload.Memo,
Date: time.Time(payload.Date), Date: time.Time(payload.Date),
Amount: amount, Amount: amount,
@ -85,34 +82,8 @@ func (h *Handler) newTransaction(c *gin.Context) {
PayeeID: payload.Payee.ID, //TODO handle new payee PayeeID: payload.Payee.ID, //TODO handle new payee
CategoryID: payload.Category.ID, //TODO handle new category CategoryID: payload.Category.ID, //TODO handle new category
} }
_, err = h.Service.CreateTransaction(c.Request.Context(), new) err = h.Service.UpdateTransaction(c.Request.Context(), update)
if err != nil { if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err)) c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("update transaction: %w", err))
} }*/
return
}
_, delete := c.GetPostForm("delete")
if delete {
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: payload.Memo,
Date: time.Time(payload.Date),
Amount: amount,
AccountID: transactionAccountID,
PayeeID: payload.Payee.ID, //TODO handle new payee
CategoryID: payload.Category.ID, //TODO handle new category
}
err = h.Service.UpdateTransaction(c.Request.Context(), update)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("update transaction: %w", err))
}
} }

View File

@ -21,11 +21,13 @@ export default defineComponent({
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
budget_id: this.budgetid, budget_id: this.budgetid,
account_id: this.accountid,
date: this.$data.TransactionDate, date: this.$data.TransactionDate,
payee: this.$data.Payee, payee: this.$data.Payee,
category: this.$data.Category, category: this.$data.Category,
memo: this.$data.Memo, memo: this.$data.Memo,
amount: this.$data.Amount amount: this.$data.Amount,
state: "Uncleared"
}), }),
headers: this.$store.getters.AuthHeaders, headers: this.$store.getters.AuthHeaders,
}) })