aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--backend/controllers/userController.go173
1 files changed, 173 insertions, 0 deletions
diff --git a/backend/controllers/userController.go b/backend/controllers/userController.go
new file mode 100644
index 0000000..bd6cef4
--- /dev/null
+++ b/backend/controllers/userController.go
@@ -0,0 +1,173 @@
1package controllers
2
3import (
4 "net/http"
5 "regexp"
6
7 "github.com/gin-gonic/gin"
8 "github.com/pektezol/leastportals/backend/database"
9 "github.com/pektezol/leastportals/backend/models"
10)
11
12func Profile(c *gin.Context) {
13 // Check if user exists
14 user, exists := c.Get("user")
15 if !exists {
16 c.JSON(http.StatusUnauthorized, gin.H{
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
66 }
67 // Target user exists
68 _, exists := c.Get("user")
69 if exists {
70 c.Redirect(http.StatusFound, "/api/v1/profile")
71 return
72 }
73 c.JSON(http.StatusOK, gin.H{
74 "code": http.StatusOK,
75 "output": gin.H{
76 "avatar": targetUser.AvatarLink,
77 "country": targetUser.CountryCode,
78 "types": targetUser.TypeToString(),
79 "username": targetUser.Username,
80 },
81 "profile": false,
82 })
83 return
84}
85
86func UpdateUserCountry(c *gin.Context) {
87 id := c.Param("id")
88 cc := c.Param("country")
89 // Check if id is all numbers and 17 length
90 match, _ := regexp.MatchString("^[0-9]{17}$", id)
91 if !match {
92 c.JSON(http.StatusNotFound, gin.H{
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
110 }
111 // Check if user exists
112 var targetUser models.User
113 database.DB.QueryRow(`SELECT * FROM users WHERE steam_id = $1;`, id).Scan(
114 &targetUser.SteamID, &targetUser.Username, &targetUser.AvatarLink, &targetUser.CountryCode,
115 &targetUser.CreatedAt, &targetUser.UpdatedAt, &targetUser.UserType)
116 if targetUser.SteamID == "" {
117 // User does not exist
118 c.JSON(http.StatusNotFound, gin.H{
119 "code": http.StatusNotFound,
120 "output": gin.H{
121 "error": "User not found.",
122 },
123 })
124 return
125 }
126 // Target user exists
127 user, exists := c.Get("user")
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
165 }
166 c.JSON(http.StatusUnauthorized, gin.H{
167 "code": http.StatusUnauthorized,
168 "output": gin.H{
169 "error": "User not logged in. Could be invalid token.",
170 },
171 })
172 return
173}