diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/admin.go | 46 | ||||
-rw-r--r-- | api/routes.go | 12 | ||||
-rw-r--r-- | api/user.go | 7 |
3 files changed, 61 insertions, 4 deletions
diff --git a/api/admin.go b/api/admin.go new file mode 100644 index 0000000..0ac6050 --- /dev/null +++ b/api/admin.go | |||
@@ -0,0 +1,46 @@ | |||
1 | package api | ||
2 | |||
3 | import ( | ||
4 | "fmt" | ||
5 | |||
6 | "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db" | ||
7 | ) | ||
8 | |||
9 | type GetAllPortfoliosQuery struct { | ||
10 | In struct { | ||
11 | Market string | ||
12 | } | ||
13 | Out map[string]Portfolio | ||
14 | } | ||
15 | |||
16 | func (q GetAllPortfoliosQuery) ValidateParams() *Error { | ||
17 | if q.In.Market != "poloniex" { | ||
18 | return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} | ||
19 | } | ||
20 | |||
21 | return nil | ||
22 | } | ||
23 | |||
24 | func (q GetAllPortfoliosQuery) Run() (interface{}, *Error) { | ||
25 | u, err := db.GetActiveUsers() | ||
26 | if err != nil { | ||
27 | return nil, NewInternalError(err) | ||
28 | } | ||
29 | |||
30 | q.Out = make(map[string]Portfolio) | ||
31 | |||
32 | for _, marketConfig := range u { | ||
33 | report, err := GetWeekPortfolio(marketConfig) | ||
34 | if ErrorIs(err, NotFound) { | ||
35 | continue | ||
36 | } | ||
37 | |||
38 | if err != nil { | ||
39 | return nil, NewInternalError(err) | ||
40 | } | ||
41 | |||
42 | q.Out[marketConfig.User.Email] = report.Round() | ||
43 | } | ||
44 | |||
45 | return q.Out, nil | ||
46 | } | ||
diff --git a/api/routes.go b/api/routes.go index 3adbfe9..a102419 100644 --- a/api/routes.go +++ b/api/routes.go | |||
@@ -58,7 +58,9 @@ var Groups = []Group{ | |||
58 | { | 58 | { |
59 | "/admin", | 59 | "/admin", |
60 | []Middleware{JwtAuth, UserConfirmed, UserIsAdmin, OtpAuth}, | 60 | []Middleware{JwtAuth, UserConfirmed, UserIsAdmin, OtpAuth}, |
61 | []Route{}, | 61 | []Route{ |
62 | {"GET", []gin.HandlerFunc{AdminGetAllPortfolios}, "/portfolios"}, | ||
63 | }, | ||
62 | }, | 64 | }, |
63 | } | 65 | } |
64 | 66 | ||
@@ -189,3 +191,11 @@ func UserAccount(c *gin.Context) { | |||
189 | 191 | ||
190 | RunQuery(query, c) | 192 | RunQuery(query, c) |
191 | } | 193 | } |
194 | |||
195 | func AdminGetAllPortfolios(c *gin.Context) { | ||
196 | query := &GetAllPortfoliosQuery{} | ||
197 | |||
198 | query.In.Market = "poloniex" | ||
199 | |||
200 | RunQuery(query, c) | ||
201 | } | ||
diff --git a/api/user.go b/api/user.go index bc24bbb..ff539f0 100644 --- a/api/user.go +++ b/api/user.go | |||
@@ -62,7 +62,8 @@ type SignParams struct { | |||
62 | } | 62 | } |
63 | 63 | ||
64 | type SignResult struct { | 64 | type SignResult struct { |
65 | Token string `json:"token"` | 65 | Token string `json:"token"` |
66 | IsAdmin bool `json:"isAdmin"` | ||
66 | } | 67 | } |
67 | 68 | ||
68 | func (s SignParams) Validate() *Error { | 69 | func (s SignParams) Validate() *Error { |
@@ -142,7 +143,7 @@ func (q SignupQuery) Run() (interface{}, *Error) { | |||
142 | } | 143 | } |
143 | } | 144 | } |
144 | 145 | ||
145 | return SignResult{token}, nil | 146 | return SignResult{token, newUser.Role == db.RoleAdmin}, nil |
146 | } | 147 | } |
147 | 148 | ||
148 | type SigninQuery struct { | 149 | type SigninQuery struct { |
@@ -173,7 +174,7 @@ func (q SigninQuery) Run() (interface{}, *Error) { | |||
173 | return nil, NewInternalError(err) | 174 | return nil, NewInternalError(err) |
174 | } | 175 | } |
175 | 176 | ||
176 | return SignResult{token}, nil | 177 | return SignResult{token, user.Role == db.RoleAdmin}, nil |
177 | } | 178 | } |
178 | 179 | ||
179 | type ConfirmEmailQuery struct { | 180 | type ConfirmEmailQuery struct { |