aboutsummaryrefslogtreecommitdiff
path: root/api/admin.go
blob: 3b16d878aad0e8a9502aac76551777102a00cd35 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package api

import (
	"fmt"

	"git.immae.eu/Cryptoportfolio/Front.git/db"
)

type GetAllPortfoliosQuery struct {
	In struct {
		Market string
	}
	Out map[string]Portfolio
}

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 = 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[marketConfig.User.Email] = report.Round()
	}

	return q.Out, nil
}