aboutsummaryrefslogtreecommitdiff
path: root/backend/controllers/userController.go
diff options
context:
space:
mode:
Diffstat (limited to 'backend/controllers/userController.go')
-rw-r--r--backend/controllers/userController.go168
1 files changed, 32 insertions, 136 deletions
diff --git a/backend/controllers/userController.go b/backend/controllers/userController.go
index 87a9427..70a2a34 100644
--- a/backend/controllers/userController.go
+++ b/backend/controllers/userController.go
@@ -13,161 +13,57 @@ func Profile(c *gin.Context) {
13 // Check if user exists 13 // Check if user exists
14 user, exists := c.Get("user") 14 user, exists := c.Get("user")
15 if !exists { 15 if !exists {
16 c.JSON(http.StatusUnauthorized, gin.H{ 16 c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in."))
17 "code": http.StatusUnauthorized,
18 "output": gin.H{
19 "error": "User not logged in. Could be invalid token.",
20 },
21 })
22 return
23 } else {
24 user := user.(models.User)
25 c.JSON(http.StatusOK, gin.H{
26 "code": http.StatusOK,
27 "output": gin.H{
28 "avatar": user.AvatarLink,
29 "country": user.CountryCode,
30 "types": user.TypeToString(),
31 "username": user.Username,
32 },
33 "profile": true,
34 })
35 return
36 }
37}
38
39func FetchUser(c *gin.Context) {
40 id := c.Param("id")
41 // Check if id is all numbers and 17 length
42 match, _ := regexp.MatchString("^[0-9]{17}$", id)
43 if !match {
44 c.JSON(http.StatusNotFound, gin.H{
45 "code": http.StatusNotFound,
46 "output": gin.H{
47 "error": "User not found.",
48 },
49 })
50 return
51 }
52 // Check if user exists
53 var targetUser models.User
54 database.DB.QueryRow(`SELECT * FROM users WHERE steam_id = $1;`, id).Scan(
55 &targetUser.SteamID, &targetUser.Username, &targetUser.AvatarLink, &targetUser.CountryCode,
56 &targetUser.CreatedAt, &targetUser.UpdatedAt, &targetUser.UserType)
57 if targetUser.SteamID == "" {
58 // User does not exist
59 c.JSON(http.StatusNotFound, gin.H{
60 "code": http.StatusNotFound,
61 "output": gin.H{
62 "error": "User not found.",
63 },
64 })
65 return 17 return
66 } 18 }
67 // Target user exists 19 c.JSON(http.StatusOK, models.Response{
68 _, exists := c.Get("user") 20 Success: true,
69 if exists { 21 Message: "",
70 c.Redirect(http.StatusFound, "/api/v1/profile") 22 Data: models.ProfileResponse{
71 return 23 Profile: true,
72 } 24 SteamID: user.(models.User).SteamID,
73 c.JSON(http.StatusOK, gin.H{ 25 Username: user.(models.User).Username,
74 "code": http.StatusOK, 26 AvatarLink: user.(models.User).AvatarLink,
75 "output": gin.H{ 27 CountryCode: user.(models.User).CountryCode,
76 "avatar": targetUser.AvatarLink,
77 "country": targetUser.CountryCode,
78 "types": targetUser.TypeToString(),
79 "username": targetUser.Username,
80 }, 28 },
81 "profile": false,
82 }) 29 })
83 return 30 return
84} 31}
85 32
86/*func UpdateUserCountry(c *gin.Context) { 33func FetchUser(c *gin.Context) {
87 id := c.Param("id") 34 id := c.Param("id")
88 cc := c.Param("country")
89 // Check if id is all numbers and 17 length 35 // Check if id is all numbers and 17 length
90 match, _ := regexp.MatchString("^[0-9]{17}$", id) 36 match, _ := regexp.MatchString("^[0-9]{17}$", id)
91 if !match { 37 if !match {
92 c.JSON(http.StatusNotFound, gin.H{ 38 c.JSON(http.StatusNotFound, models.ErrorResponse("User not found."))
93 "code": http.StatusNotFound,
94 "output": gin.H{
95 "error": "User not found.",
96 },
97 })
98 return
99 }
100 // Check if valid country code length
101 match, _ = regexp.MatchString("^[A-Z]{2}$", cc)
102 if !match {
103 c.JSON(http.StatusNotFound, gin.H{
104 "code": http.StatusNotFound,
105 "output": gin.H{
106 "error": "Invalid country code.",
107 },
108 })
109 return 39 return
110 } 40 }
111 // Check if user exists 41 // Check if user exists
112 var targetUser models.User 42 var user models.User
113 database.DB.QueryRow(`SELECT * FROM users WHERE steam_id = $1;`, id).Scan( 43 err := database.DB.QueryRow(`SELECT * FROM users WHERE steam_id = $1;`, id).Scan(
114 &targetUser.SteamID, &targetUser.Username, &targetUser.AvatarLink, &targetUser.CountryCode, 44 &user.SteamID, &user.Username, &user.AvatarLink, &user.CountryCode,
115 &targetUser.CreatedAt, &targetUser.UpdatedAt, &targetUser.UserType) 45 &user.CreatedAt, &user.UpdatedAt)
116 if targetUser.SteamID == "" { 46 if user.SteamID == "" {
117 // User does not exist 47 // User does not exist
118 c.JSON(http.StatusNotFound, gin.H{ 48 c.JSON(http.StatusNotFound, models.ErrorResponse("User not found."))
119 "code": http.StatusNotFound,
120 "output": gin.H{
121 "error": "User not found.",
122 },
123 })
124 return 49 return
125 } 50 }
126 // Target user exists 51 if err != nil {
127 user, exists := c.Get("user") 52 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
128 if exists {
129 user := user.(models.User)
130 if user.SteamID == targetUser.SteamID {
131 // Can change because it's our own profile
132 // TODO:Check if country code exists in database // ADD countries TABLE
133 var existingCC string
134 database.DB.QueryRow(`SELECT country_code FROM countries WHERE country_code = $1;`, cc).Scan(&existingCC)
135 if existingCC == "" {
136 c.JSON(http.StatusNotFound, gin.H{
137 "code": http.StatusForbidden,
138 "output": gin.H{
139 "error": "Given country code is not found.",
140 },
141 })
142 return
143 }
144 // Valid to change
145 database.DB.Exec(`UPDATE users SET country_code = $1 WHERE steam_id = $2`, cc, user.SteamID)
146 c.JSON(http.StatusOK, gin.H{
147 "code": http.StatusOK,
148 "output": gin.H{
149 "avatar": user.AvatarLink,
150 "country": user.CountryCode,
151 "types": user.TypeToString(),
152 "username": user.Username,
153 },
154 "profile": true,
155 })
156 return
157 }
158 c.JSON(http.StatusForbidden, gin.H{
159 "code": http.StatusForbidden,
160 "output": gin.H{
161 "error": "Can not change country of another user.",
162 },
163 })
164 return 53 return
165 } 54 }
166 c.JSON(http.StatusUnauthorized, gin.H{ 55 // Target user exists
167 "code": http.StatusUnauthorized, 56 _, exists := c.Get("user")
168 "output": gin.H{ 57 c.JSON(http.StatusOK, models.Response{
169 "error": "User not logged in. Could be invalid token.", 58 Success: true,
59 Message: "",
60 Data: models.ProfileResponse{
61 Profile: exists,
62 SteamID: user.SteamID,
63 Username: user.Username,
64 AvatarLink: user.AvatarLink,
65 CountryCode: user.CountryCode,
170 }, 66 },
171 }) 67 })
172 return 68 return
173}*/ 69}