]>
Commit | Line | Data |
---|---|---|
1 | package api | |
2 | ||
3 | import ( | |
4 | "fmt" | |
5 | ||
6 | "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db" | |
7 | ) | |
8 | ||
9 | type MarketConfigQuery struct { | |
10 | In struct { | |
11 | User db.User | |
12 | Market string | |
13 | } | |
14 | } | |
15 | ||
16 | func (q MarketConfigQuery) 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 MarketConfigQuery) Run() (interface{}, *Error) { | |
25 | config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market) | |
26 | if err != nil { | |
27 | return nil, NewInternalError(err) | |
28 | } | |
29 | ||
30 | if config == nil { | |
31 | configMap := make(map[string]string) | |
32 | configMap["key"] = "" | |
33 | configMap["secret"] = "" | |
34 | ||
35 | config, err = db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap) | |
36 | if err != nil { | |
37 | return nil, NewInternalError(err) | |
38 | } | |
39 | ||
40 | } | |
41 | ||
42 | return config.Config, nil | |
43 | } | |
44 | ||
45 | type UpdateMarketConfigQuery struct { | |
46 | In struct { | |
47 | User db.User | |
48 | Market string | |
49 | Key string | |
50 | Secret string | |
51 | } | |
52 | } | |
53 | ||
54 | func (q UpdateMarketConfigQuery) ValidateParams() *Error { | |
55 | if q.In.Market == "" { | |
56 | return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} | |
57 | } | |
58 | ||
59 | return nil | |
60 | } | |
61 | ||
62 | func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) { | |
63 | configMap := make(map[string]string) | |
64 | if q.In.Key != "" { | |
65 | configMap["key"] = q.In.Key | |
66 | } | |
67 | if q.In.Secret != "" { | |
68 | configMap["secret"] = q.In.Secret | |
69 | } | |
70 | ||
71 | _, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap) | |
72 | if err != nil { | |
73 | return nil, NewInternalError(err) | |
74 | } | |
75 | ||
76 | return nil, nil | |
77 | } |