Update expenseTransform to show ShowValue and Card if possible

This commit is contained in:
qowevisa 2024-11-24 11:34:48 +02:00
parent e608e64cce
commit 4d452737cd

View File

@ -1,19 +1,35 @@
package handlers package handlers
import ( import (
"fmt"
"git.qowevisa.me/Qowevisa/fin-check-api/db" "git.qowevisa.me/Qowevisa/fin-check-api/db"
"git.qowevisa.me/Qowevisa/fin-check-api/types" "git.qowevisa.me/Qowevisa/fin-check-api/types"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
var expenseTransform func(inp *db.Expense) types.DbExpense = func(inp *db.Expense) types.DbExpense { var expenseTransform func(inp *db.Expense) types.DbExpense = func(inp *db.Expense) types.DbExpense {
var card types.DbCard
expenseValueSymbolPostfix := ""
if inp.Card != nil {
card = cardTransform(inp.Card)
if inp.Card.Currency != nil {
expenseValueSymbolPostfix = fmt.Sprintf(" (%s)", inp.Card.Currency.Symbol)
}
} else {
card = types.DbCard{}
}
var showValue string
showValue = fmt.Sprintf("%d.%02d%s", inp.Value/100, inp.Value%100, expenseValueSymbolPostfix)
return types.DbExpense{ return types.DbExpense{
ID: inp.ID, ID: inp.ID,
CardID: inp.CardID, CardID: inp.CardID,
TypeID: inp.TypeID, TypeID: inp.TypeID,
Value: inp.Value, Value: inp.Value,
Comment: inp.Comment, Comment: inp.Comment,
Date: inp.Date, Date: inp.Date,
Card: card,
ShowValue: showValue,
} }
} }
@ -53,7 +69,7 @@ func ExpenseGetAll(c *gin.Context) {
} }
dbc := db.Connect() dbc := db.Connect()
var entities []*db.Expense var entities []*db.Expense
if err := dbc.Find(&entities, db.Expense{UserID: userID}).Error; err != nil { if err := dbc.Preload("Card.Currency").Find(&entities, db.Expense{UserID: userID}).Error; err != nil {
c.JSON(500, types.ErrorResponse{Message: err.Error()}) c.JSON(500, types.ErrorResponse{Message: err.Error()})
return return
} }