From 482217c8f98b5c4d2f55dcb1de27ce72f7c4424b Mon Sep 17 00:00:00 2001 From: Florian Baumann Date: Tue, 7 Feb 2023 08:32:06 +0100 Subject: [PATCH] Fix gains --- src/serra/gains.go | 18 +++++++++++------- src/serra/storage.go | 4 ++-- src/serra/update.go | 8 ++++++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/serra/gains.go b/src/serra/gains.go index 9709eef..2972e62 100644 --- a/src/serra/gains.go +++ b/src/serra/gains.go @@ -49,7 +49,11 @@ func Gains(limit float64, sort int, since string) error { old, _ := strconv.Atoi(since) - // TODO redo this calculation + currencyField := "$serra_prices.usd" + if getCurrency() == "EUR" { + currencyField = "$serra_prices.eur" + } + raise_pipeline := mongo.Pipeline{ bson.D{{"$project", bson.D{ @@ -58,12 +62,12 @@ func Gains(limit float64, sort int, since string) error { {"collectornumber", true}, {"old", bson.D{{"$arrayElemAt", - bson.A{"$serra_prices.value", old}, + bson.A{currencyField, old}, }}, }, {"current", bson.D{{"$arrayElemAt", - bson.A{"$serra_prices.value", -1}, + bson.A{currencyField, -1}, }}, }, }, @@ -107,12 +111,12 @@ func Gains(limit float64, sort int, since string) error { {"code", true}, {"old", bson.D{{"$arrayElemAt", - bson.A{"$serra_prices.value", old}, + bson.A{currencyField, old}, }}, }, {"current", bson.D{{"$arrayElemAt", - bson.A{"$serra_prices.value", -1}, + bson.A{currencyField, -1}, }}, }, }, @@ -159,12 +163,12 @@ func Gains(limit float64, sort int, since string) error { fmt.Printf("%sCards%s\n", Purple, Reset) // print each card for _, e := range raise { - fmt.Printf("%s%+.0f%%%s %s %s(%s/%s)%s (%.2f->%s%.2f EUR%s) \n", p_color, e["rate"], Reset, e["name"], Yellow, e["set"], e["collectornumber"], Reset, e["old"], Green, e["current"], Reset) + fmt.Printf("%s%+.0f%%%s %s %s(%s/%s)%s (%.2f->%s%.2f %s%s) \n", p_color, e["rate"], Reset, e["name"], Yellow, e["set"], e["collectornumber"], Reset, e["old"], Green, e["current"], getCurrency(), Reset) } fmt.Printf("\n%sSets%s\n", Purple, Reset) for _, e := range sraise { - fmt.Printf("%s%+.0f%%%s %s %s(%s)%s (%.2f->%s%.2f EUR%s) \n", p_color, e["rate"], Reset, e["name"], Yellow, e["code"], Reset, e["old"], Green, e["current"], Reset) + fmt.Printf("%s%+.0f%%%s %s %s(%s)%s (%.2f->%s%.2f %s%s) \n", p_color, e["rate"], Reset, e["name"], Yellow, e["code"], Reset, e["old"], Green, e["current"], getCurrency(), Reset) } return nil diff --git a/src/serra/storage.go b/src/serra/storage.go index 9afa4a6..259694a 100644 --- a/src/serra/storage.go +++ b/src/serra/storage.go @@ -31,9 +31,9 @@ func getCurrencyField() string { return "$prices.eur" case "USD": return "$prices.usd" + default: + return "$prices.usd" } - // default - return "$prices.usd" } func storage_connect() *mongo.Client { diff --git a/src/serra/update.go b/src/serra/update.go index d50aa9d..fb887ca 100644 --- a/src/serra/update.go +++ b/src/serra/update.go @@ -124,8 +124,12 @@ var updateCmd = &cobra.Command{ t.Date = primitive.NewDateTimeFromTime(time.Now()) mapstructure.Decode(totalvalue[0], &t) - // TODO: eur value chooser - fmt.Printf("\n%sUpdating total value of collection to: %s%.02f EUR%s\n", Green, Yellow, totalvalue[0]["eur"], Reset) + // This is here to be able to fetch currency from + // constructed new priceentry + tmpCard := Card{} + tmpCard.Prices = t + + fmt.Printf("\n%sUpdating total value of collection to: %s%.02f %s%s\n", Green, Yellow, tmpCard.getValue(), getCurrency(), Reset) totalcoll.storage_add_total(t) return nil