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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
package handlers
import (
"net/http"
"os"
"regexp"
"time"
"github.com/gin-gonic/gin"
"github.com/pektezol/leastportalshub/backend/database"
"github.com/pektezol/leastportalshub/backend/models"
)
type ProfileResponse struct {
Profile bool `json:"profile"`
SteamID string `json:"steam_id"`
UserName string `json:"user_name"`
AvatarLink string `json:"avatar_link"`
CountryCode string `json:"country_code"`
Titles []models.Title `json:"titles"`
Links models.Links `json:"links"`
Rankings ProfileRankings `json:"rankings"`
Records ProfileRecords `json:"records"`
}
type ProfileRankings struct {
Overall ProfileRankingsDetails `json:"overall"`
Singleplayer ProfileRankingsDetails `json:"singleplayer"`
Cooperative ProfileRankingsDetails `json:"cooperative"`
}
type ProfileRankingsDetails struct {
Rank int `json:"rank"`
CompletionCount int `json:"completion_count"`
CompletionTotal int `json:"completion_total"`
}
type ProfileRecords struct {
P2Singleplayer []ProfileRecordsDetails `json:"portal2_singleplayer"`
P2Cooperative []ProfileRecordsDetails `json:"portal2_cooperative"`
}
type ProfileRecordsDetails struct {
MapID int `json:"map_id"`
MapName string `json:"map_name"`
Scores []ProfileScores `json:"scores"`
}
type ProfileScores struct {
DemoID string `json:"demo_id"`
ScoreCount int `json:"score_count"`
ScoreTime int `json:"score_time"`
Date time.Time `json:"date"`
}
type ScoreResponse struct {
MapID int `json:"map_id"`
Records any `json:"records"`
}
// GET Profile
//
// @Description Get profile page of session user.
// @Tags users
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT Token"
// @Success 200 {object} models.Response{data=ProfileResponse}
// @Failure 400 {object} models.Response
// @Failure 401 {object} models.Response
// @Router /profile [get]
func Profile(c *gin.Context) {
// Check if user exists
user, exists := c.Get("user")
if !exists {
c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in."))
return
}
// Get user links
links := models.Links{}
sql := `SELECT u.p2sr, u.steam, u.youtube, u.twitch FROM users u WHERE u.steam_id = $1`
err := database.DB.QueryRow(sql, user.(models.User).SteamID).Scan(&links.P2SR, &links.Steam, &links.YouTube, &links.Twitch)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
// TODO: Get rankings (all maps done in one game)
records := ProfileRecords{
P2Singleplayer: []ProfileRecordsDetails{},
P2Cooperative: []ProfileRecordsDetails{},
}
// Get singleplayer records
sql = `SELECT m.game_id, sp.map_id, m."name", sp.score_count, sp.score_time, sp.demo_id, sp.record_date
FROM records_sp sp INNER JOIN maps m ON sp.map_id = m.id WHERE sp.user_id = $1 ORDER BY sp.map_id, sp.score_count, sp.score_time;`
rows, err := database.DB.Query(sql, user.(models.User).SteamID)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
for rows.Next() {
var mapID int
var mapName string
var gameID int
score := ProfileScores{}
rows.Scan(&gameID, &mapID, &mapName, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date)
if gameID != 1 {
continue
}
// More than one record in one map
if len(records.P2Singleplayer) != 0 && mapID == records.P2Singleplayer[len(records.P2Singleplayer)-1].MapID {
records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores = append(records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores, score)
continue
}
// New map
records.P2Singleplayer = append(records.P2Singleplayer, ProfileRecordsDetails{
MapID: mapID,
MapName: mapName,
Scores: []ProfileScores{},
})
records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores = append(records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores, score)
}
// Get multiplayer records
sql = `SELECT m.game_id, mp.map_id, m."name", mp.score_count, mp.score_time, CASE WHEN host_id = $1 THEN mp.host_demo_id WHEN partner_id = $1 THEN mp.partner_demo_id END demo_id, mp.record_date
FROM records_mp mp INNER JOIN maps m ON mp.map_id = m.id WHERE mp.host_id = $1 OR mp.partner_id = $1 ORDER BY mp.map_id, mp.score_count, mp.score_time;`
rows, err = database.DB.Query(sql, user.(models.User).SteamID)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
for rows.Next() {
var mapID int
var mapName string
var gameID int
score := ProfileScores{}
rows.Scan(&gameID, &mapID, &mapName, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date)
if gameID != 1 {
continue
}
// More than one record in one map
if len(records.P2Cooperative) != 0 && mapID == records.P2Cooperative[len(records.P2Cooperative)-1].MapID {
records.P2Cooperative[len(records.P2Cooperative)-1].Scores = append(records.P2Cooperative[len(records.P2Cooperative)-1].Scores, score)
continue
}
// New map
records.P2Cooperative = append(records.P2Cooperative, ProfileRecordsDetails{
MapID: mapID,
MapName: mapName,
Scores: []ProfileScores{},
})
records.P2Cooperative[len(records.P2Cooperative)-1].Scores = append(records.P2Cooperative[len(records.P2Cooperative)-1].Scores, score)
}
c.JSON(http.StatusOK, models.Response{
Success: true,
Message: "Successfully retrieved user scores.",
Data: ProfileResponse{
Profile: true,
SteamID: user.(models.User).SteamID,
UserName: user.(models.User).UserName,
AvatarLink: user.(models.User).AvatarLink,
CountryCode: user.(models.User).CountryCode,
Titles: user.(models.User).Titles,
Links: links,
Rankings: ProfileRankings{},
Records: records,
},
})
}
// GET User
//
// @Description Get profile page of another user.
// @Tags users
// @Accept json
// @Produce json
// @Param id path int true "User ID"
// @Success 200 {object} models.Response{data=ProfileResponse}
// @Failure 400 {object} models.Response
// @Failure 404 {object} models.Response
// @Router /users/{id} [get]
func FetchUser(c *gin.Context) {
id := c.Param("id")
// Check if id is all numbers and 17 length
match, _ := regexp.MatchString("^[0-9]{17}$", id)
if !match {
c.JSON(http.StatusNotFound, models.ErrorResponse("User not found."))
return
}
// Check if user exists
var user models.User
links := models.Links{}
sql := `SELECT u.steam_id, u.user_name, u.avatar_link, u.country_code, u.created_at, u.updated_at, u.p2sr, u.steam, u.youtube, u.twitch FROM users u WHERE u.steam_id = $1`
err := database.DB.QueryRow(sql, id).Scan(&user.SteamID, &user.UserName, &user.AvatarLink, &user.CountryCode, &user.CreatedAt, &user.UpdatedAt, &links.P2SR, &links.Steam, &links.YouTube, &links.Twitch)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
if user.SteamID == "" {
// User does not exist
c.JSON(http.StatusNotFound, models.ErrorResponse("User not found."))
return
}
// Get user titles
sql = `SELECT t.title_name, t.title_color FROM titles t
INNER JOIN user_titles ut ON t.id=ut.title_id WHERE ut.user_id = $1`
rows, err := database.DB.Query(sql, user.SteamID)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
for rows.Next() {
var title models.Title
if err := rows.Scan(&title.Name, &title.Color); err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
user.Titles = append(user.Titles, title)
}
// TODO: Get rankings (all maps done in one game)
records := ProfileRecords{
P2Singleplayer: []ProfileRecordsDetails{},
P2Cooperative: []ProfileRecordsDetails{},
}
// Get singleplayer records
sql = `SELECT m.game_id, sp.map_id, m."name", sp.score_count, sp.score_time, sp.demo_id, sp.record_date
FROM records_sp sp INNER JOIN maps m ON sp.map_id = m.id WHERE sp.user_id = $1 ORDER BY sp.map_id, sp.score_count, sp.score_time;`
rows, err = database.DB.Query(sql, user.SteamID)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
for rows.Next() {
var mapID int
var mapName string
var gameID int
score := ProfileScores{}
rows.Scan(&gameID, &mapID, &mapName, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date)
if gameID != 1 {
continue
}
// More than one record in one map
if len(records.P2Singleplayer) != 0 && mapID == records.P2Singleplayer[len(records.P2Singleplayer)-1].MapID {
records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores = append(records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores, score)
continue
}
// New map
records.P2Singleplayer = append(records.P2Singleplayer, ProfileRecordsDetails{
MapID: mapID,
MapName: mapName,
Scores: []ProfileScores{},
})
records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores = append(records.P2Singleplayer[len(records.P2Singleplayer)-1].Scores, score)
}
// Get multiplayer records
sql = `SELECT m.game_id, mp.map_id, m."name", mp.score_count, mp.score_time, CASE WHEN host_id = $1 THEN mp.host_demo_id WHEN partner_id = $1 THEN mp.partner_demo_id END demo_id, mp.record_date
FROM records_mp mp INNER JOIN maps m ON mp.map_id = m.id WHERE mp.host_id = $1 OR mp.partner_id = $1 ORDER BY mp.map_id, mp.score_count, mp.score_time;`
rows, err = database.DB.Query(sql, user.SteamID)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
for rows.Next() {
var mapID int
var mapName string
var gameID int
score := ProfileScores{}
rows.Scan(&gameID, &mapID, &mapName, &score.ScoreCount, &score.ScoreTime, &score.DemoID, &score.Date)
if gameID != 1 {
continue
}
// More than one record in one map
if len(records.P2Cooperative) != 0 && mapID == records.P2Cooperative[len(records.P2Cooperative)-1].MapID {
records.P2Cooperative[len(records.P2Cooperative)-1].Scores = append(records.P2Cooperative[len(records.P2Cooperative)-1].Scores, score)
continue
}
// New map
records.P2Cooperative = append(records.P2Cooperative, ProfileRecordsDetails{
MapID: mapID,
MapName: mapName,
Scores: []ProfileScores{},
})
records.P2Cooperative[len(records.P2Cooperative)-1].Scores = append(records.P2Cooperative[len(records.P2Cooperative)-1].Scores, score)
}
c.JSON(http.StatusOK, models.Response{
Success: true,
Message: "Successfully retrieved user scores.",
Data: ProfileResponse{
Profile: true,
SteamID: user.SteamID,
UserName: user.UserName,
AvatarLink: user.AvatarLink,
CountryCode: user.CountryCode,
Titles: user.Titles,
Links: links,
Rankings: ProfileRankings{},
Records: records,
},
})
}
// PUT Profile
//
// @Description Update profile page of session user.
// @Tags users
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT Token"
// @Success 200 {object} models.Response{data=ProfileResponse}
// @Failure 400 {object} models.Response
// @Failure 401 {object} models.Response
// @Router /profile [post]
func UpdateUser(c *gin.Context) {
// Check if user exists
user, exists := c.Get("user")
if !exists {
c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in."))
return
}
profile, err := GetPlayerSummaries(user.(models.User).SteamID, os.Getenv("API_KEY"))
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
// Update profile
_, err = database.DB.Exec(`UPDATE users SET username = $1, avatar_link = $2, country_code = $3, updated_at = $4
WHERE steam_id = $5`, profile.PersonaName, profile.AvatarFull, profile.LocCountryCode, time.Now().UTC(), user.(models.User).SteamID)
if err != nil {
c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
return
}
c.JSON(http.StatusOK, models.Response{
Success: true,
Message: "Successfully updated user.",
Data: ProfileResponse{
Profile: true,
SteamID: user.(models.User).SteamID,
UserName: profile.PersonaName,
AvatarLink: profile.AvatarFull,
CountryCode: profile.LocCountryCode,
},
})
}
// PUT Profile/CountryCode
//
// @Description Update country code of session user.
// @Tags users
// @Accept json
// @Produce json
// @Param Authorization header string true "JWT Token"
// @Param country_code query string true "Country Code [XX]"
// @Success 200 {object} models.Response
// @Failure 400 {object} models.Response
// @Failure 401 {object} models.Response
// @Router /profile [put]
func UpdateCountryCode(c *gin.Context) {
// Check if user exists
user, exists := c.Get("user")
if !exists {
c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in."))
return
}
code := c.Query("country_code")
if code == "" {
c.JSON(http.StatusNotFound, models.ErrorResponse("Enter a valid country code."))
return
}
var validCode string
err := database.DB.QueryRow(`SELECT country_code FROM countries WHERE country_code = $1`, code).Scan(&validCode)
if err != nil {
c.JSON(http.StatusNotFound, models.ErrorResponse(err.Error()))
return
}
// Valid code, update profile
_, err = database.DB.Exec(`UPDATE users SET country_code = $1 WHERE steam_id = $2`, validCode, user.(models.User).SteamID)
if err != nil {
c.JSON(http.StatusNotFound, models.ErrorResponse(err.Error()))
return
}
c.JSON(http.StatusOK, models.Response{
Success: true,
Message: "Successfully updated country code.",
})
}
|