diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-01-18 22:12:38 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-01-18 22:12:38 +0300 |
| commit | dd39d8c86667c5446020458f8fe5171149d9ed5c (patch) | |
| tree | f8e8d6a80d60e6da672cdd4cde4e43f72ec4e9bb /backend/controllers/userController.go | |
| parent | added delete demo from drive, also delete if an error is encountered while su... (diff) | |
| download | lphub-dd39d8c86667c5446020458f8fe5171149d9ed5c.tar.gz lphub-dd39d8c86667c5446020458f8fe5171149d9ed5c.tar.bz2 lphub-dd39d8c86667c5446020458f8fe5171149d9ed5c.zip | |
feat: refresh/update user via steam (#30)
Diffstat (limited to 'backend/controllers/userController.go')
| -rw-r--r-- | backend/controllers/userController.go | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/backend/controllers/userController.go b/backend/controllers/userController.go index 6bca5b9..95b2b52 100644 --- a/backend/controllers/userController.go +++ b/backend/controllers/userController.go | |||
| @@ -2,7 +2,9 @@ package controllers | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "net/http" | 4 | "net/http" |
| 5 | "os" | ||
| 5 | "regexp" | 6 | "regexp" |
| 7 | "time" | ||
| 6 | 8 | ||
| 7 | "github.com/gin-gonic/gin" | 9 | "github.com/gin-gonic/gin" |
| 8 | "github.com/pektezol/leastportals/backend/database" | 10 | "github.com/pektezol/leastportals/backend/database" |
| @@ -176,6 +178,39 @@ func FetchUser(c *gin.Context) { | |||
| 176 | return | 178 | return |
| 177 | } | 179 | } |
| 178 | 180 | ||
| 181 | func UpdateUser(c *gin.Context) { | ||
| 182 | // Check if user exists | ||
| 183 | user, exists := c.Get("user") | ||
| 184 | if !exists { | ||
| 185 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) | ||
| 186 | return | ||
| 187 | } | ||
| 188 | profile, err := GetPlayerSummaries(user.(models.User).SteamID, os.Getenv("API_KEY")) | ||
| 189 | if err != nil { | ||
| 190 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 191 | return | ||
| 192 | } | ||
| 193 | // Update profile | ||
| 194 | _, err = database.DB.Exec(`UPDATE users SET username = $1, avatar_link = $2, country_code = $3, updated_at = $4 | ||
| 195 | WHERE steam_id = $5;`, profile.PersonaName, profile.AvatarFull, profile.LocCountryCode, time.Now().UTC(), user.(models.User).SteamID) | ||
| 196 | if err != nil { | ||
| 197 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 198 | return | ||
| 199 | } | ||
| 200 | c.JSON(http.StatusOK, models.Response{ | ||
| 201 | Success: true, | ||
| 202 | Message: "Successfully updated user.", | ||
| 203 | Data: models.ProfileResponse{ | ||
| 204 | Profile: true, | ||
| 205 | SteamID: user.(models.User).SteamID, | ||
| 206 | Username: profile.PersonaName, | ||
| 207 | AvatarLink: profile.AvatarFull, | ||
| 208 | CountryCode: profile.LocCountryCode, | ||
| 209 | }, | ||
| 210 | }) | ||
| 211 | return | ||
| 212 | } | ||
| 213 | |||
| 179 | func UpdateCountryCode(c *gin.Context) { | 214 | func UpdateCountryCode(c *gin.Context) { |
| 180 | // Check if user exists | 215 | // Check if user exists |
| 181 | user, exists := c.Get("user") | 216 | user, exists := c.Get("user") |