From 92d4926ed163e7c242beb90aeca8b1cf69010179 Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Sat, 14 Jan 2023 17:47:42 +0300 Subject: added countries, update country (#4) --- backend/controllers/userController.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'backend/controllers/userController.go') diff --git a/backend/controllers/userController.go b/backend/controllers/userController.go index b23a303..ab29e32 100644 --- a/backend/controllers/userController.go +++ b/backend/controllers/userController.go @@ -67,3 +67,33 @@ func FetchUser(c *gin.Context) { }) return } + +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.", + }) +} -- cgit v1.2.3