-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities_db.go
More file actions
279 lines (260 loc) · 5.97 KB
/
utilities_db.go
File metadata and controls
279 lines (260 loc) · 5.97 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package main
import (
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
func (user *User) InsertSession(origin string, timezone string) (session Session, err error) {
uuid, err := CreateUUID()
if err != nil {
return
}
session_sql := `
INSERT INTO sessions (code, userwallet, origin, timezone, createdat)
VALUES ($1, $2, $3, $4, $5)
RETURNING code, userwallet, origin, createdat;`
err = Db.QueryRow(
session_sql,
uuid,
user.Wallet,
origin,
timezone,
time.Now()).Scan(
&session.Code,
&session.UserWallet,
&session.Origin,
&session.CreatedAt,
)
if err != nil {
err = errors.New("Error inserting new session in db")
return
}
return
}
func GetSession(r *http.Request, using string) (session Session, err error) {
err = session.ExtractFromRequest(r, using)
if err != nil {
return
}
err = session.Select()
return
}
func (session *Session) ExtractFromRequest(r *http.Request, using string) (err error) {
switch using {
case "header":
err = session.ExtractFromHeader(r)
if err != nil {
return
}
case "cookie":
err = session.ExtractFromCookie(r)
if err != nil {
return
}
}
return
}
func (session *Session) ExtractFromHeader(r *http.Request) (err error) {
if len(r.Header["Authorization"]) > 0 {
auth := strings.Split(r.Header["Authorization"][0], "sessionId=")
if auth[1] != "" {
session.Code = strings.Split(auth[1], ";")[0]
return
} else {
err = errors.New("Could not find sessionId in header")
return
}
} else {
err = errors.New("Could not find authorization in header")
return
}
}
func (session *Session) ExtractFromCookie(r *http.Request) (err error) {
for _, cookie := range r.Cookies() {
if cookie.Name == "sessionId" {
session.Code = cookie.Value
}
}
if session.Code == "" {
err = errors.New("Empty sessionId in cookie")
}
return
}
func (session *Session) Select() (err error) {
err = Db.QueryRow(`
SELECT
userwallet,
origin,
timezone
FROM sessions
WHERE code = $1;`, session.Code).Scan(
&session.UserWallet,
&session.Origin,
&session.Timezone,
)
return
}
func IsWalletInSessions(wallet string) (exists bool) {
err := Db.QueryRow(`
SELECT
true
FROM sessions
WHERE userwallet = $1;`, wallet).Scan(&exists)
if err != nil {
return false
}
return exists
}
func InsertUser(wallet string) {
default_profile_picture := os.Getenv("CDN_PATH") + "/profile_pictures/default_picture.png"
statement := `
INSERT INTO users (
wallet, profilepicture, username, privacy,
createdat, updatedat)
VALUES (
$1, $2, '', 'all',
current_timestamp, current_timestamp);`
_, err := Db.Exec(statement, wallet, default_profile_picture)
if err != nil {
log.Error(err)
return
}
}
func InsertVisibility(wallet string) {
statement := `
INSERT INTO visibilities (
wallet,
totalcounttrades,
totalportfolio,
totalreturn,
totalroi,
tradeqtyavailable,
tradevalue,
tradereturn,
traderoi,
subtradesall,
subtradereasons,
subtradequantity,
subtradeavgprice,
subtradetotal)
VALUES (
$1, TRUE, TRUE, TRUE, TRUE,
TRUE, TRUE, TRUE ,TRUE, TRUE,
TRUE, TRUE, TRUE, TRUE);`
_, err := Db.Exec(statement, wallet)
if err != nil {
log.Error(err)
return
}
}
func SelectUser(by string, value string) (user User, err error) {
user_sql := fmt.Sprintf(`
SELECT
wallet,
TO_CHAR(createdat, 'Month') || ' ' || TO_CHAR(createdat, 'YYYY') AS jointime,
CASE WHEN username IS NULL THEN '' ELSE username END AS username,
CASE WHEN twitter IS NULL THEN '' ELSE twitter END AS twitter,
CASE WHEN discord IS NULL THEN '' ELSE discord END AS discord,
CASE WHEN github IS NULL THEN '' ELSE github END AS github,
privacy,
CASE WHEN profilepicture IS NULL THEN '' ELSE profilepicture END AS profilepicture,
f.count_followers,
fo.count_followings,
s.count_subscribers,
CASE WHEN mf.monthly_fee IS NULL THEN '0' ELSE mf.monthly_fee END AS monthly_fee
FROM users
LEFT JOIN (
SELECT
COUNT(*) AS count_followers
FROM followers
WHERE followto = $1) f ON(1=1)
LEFT JOIN (
SELECT
COUNT(*) AS count_followings
FROM followers
WHERE followfrom = $1) fo ON(1=1)
LEFT JOIN (
SELECT
COUNT(*) AS count_subscribers
FROM subscribers
WHERE subscribeto = $1) s ON(1=1)
LEFT JOIN (
SELECT DISTINCT ON(sender)
sender,
createdat AS eventcreatedat,
payload#>>'{Value}' AS monthly_fee
FROM smartcontractevents
WHERE name = 'ChangePlan'
AND LOWER(sender) = LOWER($1)
ORDER BY 1, 2 DESC) mf ON(1=1)
WHERE %s = $1;`, by)
err = Db.QueryRow(user_sql, value).Scan(
&user.Wallet,
&user.JoinTime,
&user.Username,
&user.Twitter,
&user.Discord,
&user.Github,
&user.Privacy,
&user.ProfilePicture,
&user.Followers,
&user.Followings,
&user.Subscribers,
&user.MonthlyFee,
)
if err != nil {
log.WithFields(log.Fields{
"by": by,
"value": value,
"custom_msg": "Failed selecting user",
}).Error(err)
return
}
visibility_sql := `
SELECT
totalcounttrades,
totalportfolio,
totalreturn,
totalroi,
tradeqtyavailable,
tradevalue,
tradereturn,
traderoi,
subtradesall,
subtradereasons,
subtradequantity,
subtradeavgprice,
subtradetotal
FROM visibilities
WHERE wallet = $1;`
err = Db.QueryRow(
visibility_sql,
&user.Wallet).Scan(
&user.Visibility.TotalCountTrades,
&user.Visibility.TotalPortfolio,
&user.Visibility.TotalReturn,
&user.Visibility.TotalRoi,
&user.Visibility.TradeQtyAvailable,
&user.Visibility.TradeValue,
&user.Visibility.TradeReturn,
&user.Visibility.TradeRoi,
&user.Visibility.SubtradesAll,
&user.Visibility.SubtradeReasons,
&user.Visibility.SubtradeQuantity,
&user.Visibility.SubtradeAvgPrice,
&user.Visibility.SubtradeTotal,
)
if err != nil {
log.WithFields(log.Fields{
"by": by,
"value": value,
"custom_msg": "Failed selecting visibilities",
}).Error(err)
return
}
return
}