Skip to content

Commit

Permalink
fix: unmarshall type error
Browse files Browse the repository at this point in the history
  • Loading branch information
noobj committed Sep 23, 2022
1 parent e3f4093 commit e6e2d79
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
22 changes: 14 additions & 8 deletions cmd/ahorro/fetchentries/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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",
}},
}},
},
Expand All @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions internal/repositories/ahorro/entry_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ 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 {
Id primitive.ObjectID `json:"_id" bson:"_id"`
Name string
User primitive.ObjectID
Color string
V int `bson:"__v"`
V int `bson:"__v,omitempty"`
}

type AhorroRepository struct {
Expand Down

0 comments on commit e6e2d79

Please sign in to comment.