Add total to StatsTypeCurrencyChart

This commit is contained in:
qowevisa 2024-12-01 12:17:08 +02:00
parent 66dfdada06
commit eeaec565bd
2 changed files with 8 additions and 0 deletions

View File

@ -7,6 +7,7 @@ import (
"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"
"git.qowevisa.me/Qowevisa/fin-check-api/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@ -85,14 +86,20 @@ func StatisticsGetAllSpendingsForTypes(c *gin.Context) {
typeToValue[expense.TypeID] = val typeToValue[expense.TypeID] = val
} }
} }
var sum uint64 = 0
var elements []types.StatsType var elements []types.StatsType
for _, val := range typeToValue { for _, val := range typeToValue {
elements = append(elements, val) elements = append(elements, val)
sum += val.Value
} }
slices.SortFunc(elements, func(a, b types.StatsType) int {
return utils.DescendingSort(a.Value, b.Value)
})
ret = append(ret, types.StatsTypeCurrencyChart{ ret = append(ret, types.StatsTypeCurrencyChart{
CurrencyLabel: fmt.Sprintf("%s (%s)", currency.Symbol, currency.ISOName), CurrencyLabel: fmt.Sprintf("%s (%s)", currency.Symbol, currency.ISOName),
Elements: elements, Elements: elements,
Total: sum,
}) })
} }
c.JSON(200, ret) c.JSON(200, ret)

View File

@ -196,6 +196,7 @@ type StatsType struct {
type StatsTypeCurrencyChart struct { type StatsTypeCurrencyChart struct {
CurrencyLabel string `json:"label" example:"$ (USD)"` CurrencyLabel string `json:"label" example:"$ (USD)"`
Elements []StatsType `json:"elements"` Elements []StatsType `json:"elements"`
Total uint64 `json:"total" example:"120050"`
} }
// {{{ Settings section // {{{ Settings section