From 2e4885d98ec49203180deb7e4e9148762e4720e7 Mon Sep 17 00:00:00 2001 From: jloup Date: Sun, 13 May 2018 23:14:26 +0200 Subject: Admin minimal dashboard. --- api/admin.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ api/routes.go | 12 +++++++++++- api/user.go | 7 ++++--- 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 api/admin.go (limited to 'api') 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 @@ +package api + +import ( + "fmt" + + "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/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 +} 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{ { "/admin", []Middleware{JwtAuth, UserConfirmed, UserIsAdmin, OtpAuth}, - []Route{}, + []Route{ + {"GET", []gin.HandlerFunc{AdminGetAllPortfolios}, "/portfolios"}, + }, }, } @@ -189,3 +191,11 @@ func UserAccount(c *gin.Context) { RunQuery(query, c) } + +func AdminGetAllPortfolios(c *gin.Context) { + query := &GetAllPortfoliosQuery{} + + query.In.Market = "poloniex" + + RunQuery(query, c) +} 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 { } type SignResult struct { - Token string `json:"token"` + Token string `json:"token"` + IsAdmin bool `json:"isAdmin"` } func (s SignParams) Validate() *Error { @@ -142,7 +143,7 @@ func (q SignupQuery) Run() (interface{}, *Error) { } } - return SignResult{token}, nil + return SignResult{token, newUser.Role == db.RoleAdmin}, nil } type SigninQuery struct { @@ -173,7 +174,7 @@ func (q SigninQuery) Run() (interface{}, *Error) { return nil, NewInternalError(err) } - return SignResult{token}, nil + return SignResult{token, user.Role == db.RoleAdmin}, nil } type ConfirmEmailQuery struct { -- cgit v1.2.3