From e6e2d79d5d1e304b934f8b80eb20a0284e0ec17b Mon Sep 17 00:00:00 2001 From: noobj Date: Fri, 23 Sep 2022 18:17:44 +0800 Subject: [PATCH] fix: unmarshall type error --- cmd/ahorro/fetchentries/main.go | 22 ++++++++++++------- .../repositories/ahorro/entry_repository.go | 8 +++---- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/cmd/ahorro/fetchentries/main.go b/cmd/ahorro/fetchentries/main.go index 34cbbf6..15a4b86 100644 --- a/cmd/ahorro/fetchentries/main.go +++ b/cmd/ahorro/fetchentries/main.go @@ -20,13 +20,13 @@ type Response events.APIGatewayProxyResponse type AggregateResult struct { Entries []AhorroRepository.Entry - Sum int + Sum float32 Category []AhorroRepository.Category } type CategoryEntriesBundle struct { - Id primitive.ObjectID `json:"_id"` - Sum int `json:"sum"` + Id primitive.ObjectID `json:"_id" bson:"_id"` + Sum float32 `json:"sum"` Percentage string `json:"percentage"` Name string `json:"name"` Entries []AhorroRepository.Entry `json:"entries"` @@ -103,7 +103,7 @@ func Handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (event }, {Key: "sum", Value: bson.D{{ Key: "$sum", Value: bson.M{ - "$toDecimal": "$amount", + "$toDouble": "$amount", }}, }}, }, @@ -123,13 +123,19 @@ func Handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (event repoResults := entryRepository.Aggregate([]bson.D{matchStage, sortStage, groupStage, sortSumStage, lookupStage}) var categories []CategoryEntriesBundle - total := 0 + total := float32(0.0) for _, repoResult := range repoResults { + fmt.Printf("%+v", repoResult) doc, _ := bson.Marshal(repoResult) var result AggregateResult - bson.Unmarshal(doc, &result) - fmt.Printf("%+v", result) + err := bson.Unmarshal(doc, &result) + + if err != nil { + fmt.Println("Unmarshall error: ", err) + return helper.GenerateErrorResponse[events.APIGatewayProxyResponse](500) + } + cateEntriesBundle := CategoryEntriesBundle{ Id: result.Category[0].Id, Sum: result.Sum, @@ -149,7 +155,7 @@ func Handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (event resultForReturn := struct { Categories []CategoryEntriesBundle `json:"categories"` - Total int `json:"total"` + Total float32 `json:"total"` }{ Categories: categories, Total: total, diff --git a/internal/repositories/ahorro/entry_repository.go b/internal/repositories/ahorro/entry_repository.go index be28779..67357cc 100644 --- a/internal/repositories/ahorro/entry_repository.go +++ b/internal/repositories/ahorro/entry_repository.go @@ -9,11 +9,11 @@ import ( type Entry struct { Id primitive.ObjectID `json:"_id" bson:"_id"` - Amount int `json:"amount"` + Amount string `json:"amount" bson:"amount"` Date string `json:"date"` Descr string `json:"descr"` - Category primitive.ObjectID `json:"category"` - User primitive.ObjectID `json:"user"` + Category primitive.ObjectID `json:"category" bson:"category,omitempty"` + User primitive.ObjectID `json:"user" bson:"user,omitempty"` } type Category struct { @@ -21,7 +21,7 @@ type Category struct { Name string User primitive.ObjectID Color string - V int `bson:"__v"` + V int `bson:"__v,omitempty"` } type AhorroRepository struct {