-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroute_api.go
More file actions
126 lines (107 loc) · 2.62 KB
/
route_api.go
File metadata and controls
126 lines (107 loc) · 2.62 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
"github.com/lib/pq"
log "github.com/sirupsen/logrus"
)
func ListTrades(w http.ResponseWriter, r *http.Request) {
session, err := GetSession(r, "header")
if err != nil {
log.WithFields(log.Fields{
"customMsg": "Failed listing trades, wrong header",
}).Error(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
if session.Origin != "api" {
log.Error("Failed listing trades, origin not api")
w.WriteHeader(http.StatusBadRequest)
return
}
var tradeCodes []string
err = Db.QueryRow(`
SELECT
ARRAY_AGG(code)
FROM trades
WHERE userwallet = $1;`,
session.UserWallet).Scan(pq.Array(&tradeCodes))
if err != nil {
log.Fatal(err)
}
json.NewEncoder(w).Encode(tradeCodes)
}
func ListSubtrades(w http.ResponseWriter, r *http.Request) {
session, err := GetSession(r, "header")
if err != nil {
log.WithFields(log.Fields{
"customMsg": "Failed listing subtrades, wrong header",
}).Error(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
if session.Origin != "api" {
log.Error("Failed listing trades, origin not api")
w.WriteHeader(http.StatusBadRequest)
return
}
tradecode := mux.Vars(r)["tradecode"]
var subtradeCodes []string
err = Db.QueryRow(`
SELECT
ARRAY_AGG(code)
FROM subtrades
WHERE userwallet = $1
AND tradecode = $2;`,
session.UserWallet,
tradecode,
).Scan(pq.Array(&subtradeCodes))
if err != nil {
log.Fatal(err)
}
json.NewEncoder(w).Encode(subtradeCodes)
}
func GetSnapshot(w http.ResponseWriter, r *http.Request) {
session, err := GetSession(r, "header")
if err != nil {
log.WithFields(log.Fields{
"customMsg": "Failed getting snapshot, wrong header",
}).Error(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
if session.Origin != "api" {
log.Error("Failed getting snapshot, origin not api")
w.WriteHeader(http.StatusBadRequest)
return
}
user, err := SelectUser("wallet", session.UserWallet)
if err != nil {
log.WithFields(log.Fields{
"customMsg": "Failed getting snapshot, wrong user",
"userWallet": session.UserWallet,
}).Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
snapshot := user.GetSnapshot()
export_snapshot := struct {
UserDetails UserDetails
Trades []Trade
CountTrades int
TotalReturnUsd string
TotalReturnBtc string
Roi float64
TotalPortfolioUsd string
}{
snapshot.UserDetails,
snapshot.Trades,
snapshot.CountTrades,
snapshot.TotalReturnUsd,
snapshot.TotalReturnBtc,
snapshot.Roi,
snapshot.TotalPortfolioUsd,
}
json.NewEncoder(w).Encode(export_snapshot)
}