From 957f1e83637b6192dd2c6a519cdd989a5c95ca7f Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sun, 24 Nov 2024 00:28:22 +0200 Subject: [PATCH] Update transfer handlers to show and use card transformer that will use currency transformer as well if their field is not nil --- handlers/transfer.go | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/handlers/transfer.go b/handlers/transfer.go index ae3a725..d05447d 100644 --- a/handlers/transfer.go +++ b/handlers/transfer.go @@ -1,18 +1,52 @@ package handlers import ( + "fmt" + "git.qowevisa.me/Qowevisa/fin-check-api/db" "git.qowevisa.me/Qowevisa/fin-check-api/types" "github.com/gin-gonic/gin" ) var transferTransform func(inp *db.Transfer) types.DbTransfer = func(inp *db.Transfer) types.DbTransfer { + var fromCard types.DbCard + var toCard types.DbCard + if inp.FromCard != nil { + fromCard = cardTransform(inp.FromCard) + } + if inp.ToCard != nil { + toCard = cardTransform(inp.ToCard) + } + haveDiffCurrs := false + if inp.FromCard != nil && inp.FromCard.Currency != nil && inp.ToCard != nil && inp.ToCard.Currency != nil { + haveDiffCurrs = inp.FromCard.CurrencyID != inp.ToCard.CurrencyID + } + var showValue string + if haveDiffCurrs { + showValue = fmt.Sprintf("%d.%02d%s -> %d.%02d%s", + inp.FromValue/100, + inp.FromValue%100, + inp.FromCard.Currency.Symbol, + inp.ToValue/100, + inp.ToValue%100, + inp.ToCard.Currency.Symbol, + ) + } else { + showValue = fmt.Sprintf("%d.%02d", inp.Value/100, inp.Value%100) + } return types.DbTransfer{ ID: inp.ID, FromCardID: inp.FromCardID, ToCardID: inp.ToCardID, Value: inp.Value, + FromValue: inp.FromValue, + ToValue: inp.ToValue, Date: inp.Date, + // + ShowValue: showValue, + HaveDifferentCurrencies: haveDiffCurrs, + FromCard: fromCard, + ToCard: toCard, } } @@ -52,7 +86,7 @@ func TransferGetAll(c *gin.Context) { } dbc := db.Connect() var entities []*db.Transfer - if err := dbc.Find(&entities, db.Transfer{UserID: userID}).Error; err != nil { + if err := dbc.Preload("FromCard.Currency").Preload("ToCard.Currency").Find(&entities, db.Transfer{UserID: userID}).Error; err != nil { c.JSON(500, types.ErrorResponse{Message: err.Error()}) return } @@ -82,6 +116,8 @@ func TransferAdd(c *gin.Context) { dst.ToCardID = src.ToCardID dst.Value = src.Value dst.Date = src.Date + dst.FromValue = src.FromValue + dst.ToValue = src.ToValue })(c) } @@ -107,6 +143,8 @@ func TransferPutId(c *gin.Context) { dst.ToCardID = src.ToCardID dst.Value = src.Value dst.Date = src.Date + dst.FromValue = src.FromValue + dst.ToValue = src.ToValue }, transferTransform, )(c)