blob: e4bfda53f0cdabeb3aa530be56db424923738667 (
plain) (
tree)
|
|
package api
import (
"fmt"
"git.immae.eu/Cryptoportfolio/Front.git/db"
"github.com/shopspring/decimal"
)
type GetAllPortfoliosQuery struct {
In struct {
Market string
}
Out struct {
Portfolios map[string]Portfolio `json:"portfolios"`
TotalBTCValue decimal.Decimal `json:"totalBtcValue"`
TotalBTCVariation decimal.Decimal `json:"totalBtcVariation"`
}
}
func (q GetAllPortfoliosQuery) ValidateParams() *Error {
if q.In.Market != "poloniex" {
return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)}
}
return nil
}
func (q GetAllPortfoliosQuery) Run() (interface{}, *Error) {
u, err := db.GetActiveUsers()
if err != nil {
return nil, NewInternalError(err)
}
q.Out.Portfolios = make(map[string]Portfolio)
for _, marketConfig := range u {
report, err := GetWeekPortfolio(marketConfig)
if ErrorIs(err, NotFound) {
continue
}
if err != nil {
return nil, NewInternalError(err)
}
q.Out.Portfolios[marketConfig.User.Email] = report.Round()
q.Out.TotalBTCValue = q.Out.TotalBTCValue.Add(report.Performance.Value)
q.Out.TotalBTCVariation = q.Out.TotalBTCVariation.Add(report.Performance.Variation)
}
q.Out.TotalBTCValue = q.Out.TotalBTCValue.Round(3)
q.Out.TotalBTCVariation = q.Out.TotalBTCVariation.Round(3)
return q.Out, nil
}
|