]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blame - api/admin.go
Display total balance/performance.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / admin.go
CommitLineData
2e4885d9 1package api
2
3import (
4 "fmt"
5
1d68446a 6 "git.immae.eu/Cryptoportfolio/Front.git/db"
1fcb7758 7 "github.com/shopspring/decimal"
2e4885d9 8)
9
10type GetAllPortfoliosQuery struct {
11 In struct {
12 Market string
13 }
1fcb7758 14 Out struct {
15 Portfolios map[string]Portfolio `json:"portfolios"`
16 TotalBTCValue decimal.Decimal `json:"totalBtcValue"`
17 TotalBTCVariation decimal.Decimal `json:"totalBtcVariation"`
18 }
2e4885d9 19}
20
21func (q GetAllPortfoliosQuery) ValidateParams() *Error {
22 if q.In.Market != "poloniex" {
23 return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
24 }
25
26 return nil
27}
28
29func (q GetAllPortfoliosQuery) Run() (interface{}, *Error) {
30 u, err := db.GetActiveUsers()
31 if err != nil {
32 return nil, NewInternalError(err)
33 }
34
1fcb7758 35 q.Out.Portfolios = make(map[string]Portfolio)
2e4885d9 36
37 for _, marketConfig := range u {
38 report, err := GetWeekPortfolio(marketConfig)
39 if ErrorIs(err, NotFound) {
40 continue
41 }
42
43 if err != nil {
44 return nil, NewInternalError(err)
45 }
46
1fcb7758 47 q.Out.Portfolios[marketConfig.User.Email] = report.Round()
48 q.Out.TotalBTCValue = q.Out.TotalBTCValue.Add(report.Performance.Value)
49 q.Out.TotalBTCVariation = q.Out.TotalBTCVariation.Add(report.Performance.Variation)
2e4885d9 50 }
51
1fcb7758 52 q.Out.TotalBTCValue = q.Out.TotalBTCValue.Round(3)
53 q.Out.TotalBTCVariation = q.Out.TotalBTCVariation.Round(3)
54
2e4885d9 55 return q.Out, nil
56}