]>
Commit | Line | Data |
---|---|---|
1 | package api | |
2 | ||
3 | import ( | |
4 | "fmt" | |
5 | ||
6 | "git.immae.eu/Cryptoportfolio/Front.git/db" | |
7 | "github.com/shopspring/decimal" | |
8 | ) | |
9 | ||
10 | type GetAllPortfoliosQuery struct { | |
11 | In struct { | |
12 | Market string | |
13 | } | |
14 | Out struct { | |
15 | Portfolios map[string]Portfolio `json:"portfolios"` | |
16 | TotalBTCValue decimal.Decimal `json:"totalBtcValue"` | |
17 | TotalBTCVariation decimal.Decimal `json:"totalBtcVariation"` | |
18 | } | |
19 | } | |
20 | ||
21 | func (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 | ||
29 | func (q GetAllPortfoliosQuery) Run() (interface{}, *Error) { | |
30 | u, err := db.GetActiveUsers() | |
31 | if err != nil { | |
32 | return nil, NewInternalError(err) | |
33 | } | |
34 | ||
35 | q.Out.Portfolios = make(map[string]Portfolio) | |
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 | ||
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) | |
50 | } | |
51 | ||
52 | q.Out.TotalBTCValue = q.Out.TotalBTCValue.Round(3) | |
53 | q.Out.TotalBTCVariation = q.Out.TotalBTCVariation.Round(3) | |
54 | ||
55 | return q.Out, nil | |
56 | } |