From 245bc1e44a276f91b3c51f205c60cbfb231365f7 Mon Sep 17 00:00:00 2001 From: noobj Date: Mon, 26 Sep 2022 17:48:53 +0800 Subject: [PATCH] feat: change entries amount column to float32 --- cmd/ahorro/fetchentries/fetchentries_test.go | 2 +- cmd/ahorro/fetchentries/main.go | 23 +++++++++++-------- cmd/ahorro/sync/callback/main.go | 6 +++++ cmd/ahorro/sync/handler/main.go | 9 +++++++- .../repositories/ahorro/entry_repository.go | 6 ++--- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/cmd/ahorro/fetchentries/fetchentries_test.go b/cmd/ahorro/fetchentries/fetchentries_test.go index 23a950f..9ae650d 100644 --- a/cmd/ahorro/fetchentries/fetchentries_test.go +++ b/cmd/ahorro/fetchentries/fetchentries_test.go @@ -93,7 +93,7 @@ var _ = Describe("Fetchentries", func() { Context("when handler return expected json response", func() { It("should pass", func() { - expectedRes := "{\"categories\":[{\"_id\":\"62badc82d420270009a51019\",\"sum\":110,\"percentage\":\"0.55\",\"name\":\"Food\",\"entries\":[{\"_id\":\"62badc82d420270009a51019\",\"amount\":110,\"date\":\"2022-01-05\",\"descr\":\"fuck\",\"category\":\"62badc82d420270009a51019\",\"user\":\"62badc82d420270009a51019\"}],\"color\":\"#a4e56c\"},{\"_id\":\"62badc82d420270009a51019\",\"sum\":90,\"percentage\":\"0.45\",\"name\":\"Abc\",\"entries\":[{\"_id\":\"62badc82d420270009a51019\",\"amount\":90,\"date\":\"2022-01-05\",\"descr\":\"fuck\",\"category\":\"62badc82d420270009a51019\",\"user\":\"62badc82d420270009a51019\"}],\"color\":\"#a4e51c\"}],\"total\":200}" + expectedRes := "{\"categories\":[{\"_id\":\"62badc82d420270009a51019\",\"sum\":110,\"percentage\":\"0.55\",\"name\":\"Food\",\"entries\":[{\"_id\":\"62badc82d420270009a51019\",\"amount\":110,\"date\":\"2022-01-05\",\"descr\":\"fuck\"}],\"color\":\"#a4e56c\"},{\"_id\":\"62badc82d420270009a51019\",\"sum\":90,\"percentage\":\"0.45\",\"name\":\"Abc\",\"entries\":[{\"_id\":\"62badc82d420270009a51019\",\"amount\":90,\"date\":\"2022-01-05\",\"descr\":\"fuck\"}],\"color\":\"#a4e51c\"}],\"total\":200}" res, err := main.Handler(ctx, fakeRequest) fmt.Printf("%+v", res.Body) Expect(res.Body).To(Equal(expectedRes)) diff --git a/cmd/ahorro/fetchentries/main.go b/cmd/ahorro/fetchentries/main.go index 15a4b86..67780f9 100644 --- a/cmd/ahorro/fetchentries/main.go +++ b/cmd/ahorro/fetchentries/main.go @@ -18,19 +18,26 @@ import ( type Response events.APIGatewayProxyResponse +type Entry struct { + Id primitive.ObjectID `json:"_id" bson:"_id"` + Amount float32 `json:"amount" bson:"amount"` + Date string `json:"date"` + Descr string `json:"descr"` +} + type AggregateResult struct { - Entries []AhorroRepository.Entry + Entries []Entry Sum float32 Category []AhorroRepository.Category } type CategoryEntriesBundle struct { - Id primitive.ObjectID `json:"_id" bson:"_id"` - Sum float32 `json:"sum"` - Percentage string `json:"percentage"` - Name string `json:"name"` - Entries []AhorroRepository.Entry `json:"entries"` - Color string `json:"color"` + Id primitive.ObjectID `json:"_id" bson:"_id"` + Sum float32 `json:"sum"` + Percentage string `json:"percentage"` + Name string `json:"name"` + Entries []Entry `json:"entries"` + Color string `json:"color"` } func checkTimeFormat(format string, timeString string) bool { @@ -127,10 +134,8 @@ func Handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (event for _, repoResult := range repoResults { fmt.Printf("%+v", repoResult) doc, _ := bson.Marshal(repoResult) - var result AggregateResult err := bson.Unmarshal(doc, &result) - if err != nil { fmt.Println("Unmarshall error: ", err) return helper.GenerateErrorResponse[events.APIGatewayProxyResponse](500) diff --git a/cmd/ahorro/sync/callback/main.go b/cmd/ahorro/sync/callback/main.go index 28e6293..d5abb9c 100644 --- a/cmd/ahorro/sync/callback/main.go +++ b/cmd/ahorro/sync/callback/main.go @@ -104,5 +104,11 @@ func Handler(ctx context.Context, request events.APIGatewayV2HTTPRequest) (event } func main() { + userRepo := UserRepository.New() + defer userRepo.Disconnect()() + container.NamedSingleton("UserRepo", func() repositories.IRepository { + return userRepo + }) + lambda.Start(jwtMiddleWare.Auth(Handler)) } diff --git a/cmd/ahorro/sync/handler/main.go b/cmd/ahorro/sync/handler/main.go index 24da34b..fe849f2 100644 --- a/cmd/ahorro/sync/handler/main.go +++ b/cmd/ahorro/sync/handler/main.go @@ -7,6 +7,7 @@ import ( "io" "log" "math/rand" + "strconv" "strings" "time" @@ -205,8 +206,14 @@ func collateCategoryItems(categoryItems []CategoryItem, userId primitive.ObjectI func collateEntryItems(entryItems []EntryItem, cateIdMap map[string]primitive.ObjectID, userId primitive.ObjectID) []interface{} { var result []interface{} for _, entryItem := range entryItems { + amount, err := strconv.ParseFloat(entryItem.Amount, 32) + if err != nil { + log.Println("Collating entries failed:", err) + panic("Collating entries failed") + } + newItemBson := bson.M{ - "amount": entryItem.Amount, + "amount": amount, "date": entryItem.Date, "descr": entryItem.Descr, "category": cateIdMap[entryItem.CategoryId], diff --git a/internal/repositories/ahorro/entry_repository.go b/internal/repositories/ahorro/entry_repository.go index 67357cc..86905cf 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 string `json:"amount" bson:"amount"` + Amount float32 `json:"amount" bson:"amount"` Date string `json:"date"` Descr string `json:"descr"` - Category primitive.ObjectID `json:"category" bson:"category,omitempty"` - User primitive.ObjectID `json:"user" bson:"user,omitempty"` + Category primitive.ObjectID `json:"category,omitempty" bson:"category,omitempty"` + User primitive.ObjectID `json:"user,omitempty" bson:"user,omitempty"` } type Category struct {