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
|
package controllers
import (
"net/http"
"regexp"
"github.com/gin-gonic/gin"
"github.com/pektezol/leastportals/backend/database"
"github.com/pektezol/leastportals/backend/models"
)
func Profile(c *gin.Context) {
// Check if user exists
user, exists := c.Get("user")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"output": gin.H{
"error": "User not logged in. Could be invalid token.",
},
})
return
} else {
user := user.(models.User)
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"output": gin.H{
"avatar": user.AvatarLink,
"country": user.CountryCode,
"types": user.TypeToString(),
"username": user.Username,
},
"profile": true,
})
return
}
}
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, gin.H{
"code": http.StatusNotFound,
"output": gin.H{
"error": "User not found.",
},
})
return
}
// Check if user exists
var targetUser models.User
database.DB.QueryRow(`SELECT * FROM users WHERE steam_id = $1;`, id).Scan(
&targetUser.SteamID, &targetUser.Username, &targetUser.AvatarLink, &targetUser.CountryCode,
&targetUser.CreatedAt, &targetUser.UpdatedAt, &targetUser.UserType)
if targetUser.SteamID == "" {
// User does not exist
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"output": gin.H{
"error": "User not found.",
},
})
return
}
// Target user exists
_, exists := c.Get("user")
if exists {
c.Redirect(http.StatusFound, "/api/v1/profile")
return
}
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"output": gin.H{
"avatar": targetUser.AvatarLink,
"country": targetUser.CountryCode,
"types": targetUser.TypeToString(),
"username": targetUser.Username,
},
"profile": false,
})
return
}
/*func UpdateUserCountry(c *gin.Context) {
id := c.Param("id")
cc := c.Param("country")
// Check if id is all numbers and 17 length
match, _ := regexp.MatchString("^[0-9]{17}$", id)
if !match {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"output": gin.H{
"error": "User not found.",
},
})
return
}
// Check if valid country code length
match, _ = regexp.MatchString("^[A-Z]{2}$", cc)
if !match {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"output": gin.H{
"error": "Invalid country code.",
},
})
return
}
// Check if user exists
var targetUser models.User
database.DB.QueryRow(`SELECT * FROM users WHERE steam_id = $1;`, id).Scan(
&targetUser.SteamID, &targetUser.Username, &targetUser.AvatarLink, &targetUser.CountryCode,
&targetUser.CreatedAt, &targetUser.UpdatedAt, &targetUser.UserType)
if targetUser.SteamID == "" {
// User does not exist
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusNotFound,
"output": gin.H{
"error": "User not found.",
},
})
return
}
// Target user exists
user, exists := c.Get("user")
if exists {
user := user.(models.User)
if user.SteamID == targetUser.SteamID {
// Can change because it's our own profile
// TODO:Check if country code exists in database // ADD countries TABLE
var existingCC string
database.DB.QueryRow(`SELECT country_code FROM countries WHERE country_code = $1;`, cc).Scan(&existingCC)
if existingCC == "" {
c.JSON(http.StatusNotFound, gin.H{
"code": http.StatusForbidden,
"output": gin.H{
"error": "Given country code is not found.",
},
})
return
}
// Valid to change
database.DB.Exec(`UPDATE users SET country_code = $1 WHERE steam_id = $2`, cc, user.SteamID)
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"output": gin.H{
"avatar": user.AvatarLink,
"country": user.CountryCode,
"types": user.TypeToString(),
"username": user.Username,
},
"profile": true,
})
return
}
c.JSON(http.StatusForbidden, gin.H{
"code": http.StatusForbidden,
"output": gin.H{
"error": "Can not change country of another user.",
},
})
return
}
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"output": gin.H{
"error": "User not logged in. Could be invalid token.",
},
})
return
}*/
|