Implement update and delete for transactions
This commit is contained in:
@ -24,9 +24,26 @@ func getUUID(c *gin.Context, name string) (uuid.UUID, error) {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func getNullUUID(c *gin.Context, name string) (uuid.NullUUID, error) {
|
||||
func getNullUUIDFromParam(c *gin.Context, name string) (uuid.NullUUID, error) {
|
||||
value := c.Param(name)
|
||||
if value == "" {
|
||||
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 getNullUUIDFromForm(c *gin.Context, name string) (uuid.NullUUID, error) {
|
||||
value, succ := c.GetPostForm(name)
|
||||
if !succ {
|
||||
if !succ || value == "" {
|
||||
return uuid.NullUUID{}, nil
|
||||
}
|
||||
|
||||
@ -45,13 +62,13 @@ func (h *Handler) newTransaction(c *gin.Context) {
|
||||
transactionMemo, _ := c.GetPostForm("memo")
|
||||
transactionAccountID, err := getUUID(c, "account_id")
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotAcceptable, err)
|
||||
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("account_id: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
transactionCategoryID, err := getNullUUID(c, "category_id")
|
||||
transactionCategoryID, err := getNullUUIDFromForm(c, "category_id")
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusNotAcceptable, err)
|
||||
c.AbortWithError(http.StatusNotAcceptable, fmt.Errorf("category_id: %w", err))
|
||||
return
|
||||
}
|
||||
|
||||
@ -75,17 +92,42 @@ func (h *Handler) newTransaction(c *gin.Context) {
|
||||
|
||||
amount := postgres.Numeric{}
|
||||
amount.Set(transactionAmount)
|
||||
new := postgres.CreateTransactionParams{
|
||||
Memo: transactionMemo,
|
||||
Date: transactionDateValue,
|
||||
Amount: amount,
|
||||
AccountID: transactionAccountID,
|
||||
PayeeID: uuid.NullUUID{},
|
||||
CategoryID: transactionCategoryID,
|
||||
}
|
||||
_, err = h.Service.CreateTransaction(c.Request.Context(), new)
|
||||
|
||||
transactionUUID, err := getNullUUIDFromParam(c, "transactionid")
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("create transaction: %w", err))
|
||||
return
|
||||
|
||||
}
|
||||
if !transactionUUID.Valid {
|
||||
new := postgres.CreateTransactionParams{
|
||||
Memo: transactionMemo,
|
||||
Date: transactionDateValue,
|
||||
Amount: amount,
|
||||
AccountID: transactionAccountID,
|
||||
PayeeID: uuid.NullUUID{},
|
||||
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)
|
||||
}
|
||||
update := postgres.UpdateTransactionParams{
|
||||
Memo: transactionMemo,
|
||||
Date: transactionDateValue,
|
||||
Amount: amount,
|
||||
AccountID: transactionAccountID,
|
||||
PayeeID: uuid.NullUUID{},
|
||||
CategoryID: transactionCategoryID,
|
||||
}
|
||||
err = h.Service.UpdateTransaction(c.Request.Context(), update)
|
||||
if err != nil {
|
||||
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("update transaction: %w", err))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user