diff options
Diffstat (limited to 'backend/handlers')
| -rw-r--r-- | backend/handlers/discussions.go | 24 | ||||
| -rw-r--r-- | backend/handlers/login.go | 3 | ||||
| -rw-r--r-- | backend/handlers/logs.go | 98 | ||||
| -rw-r--r-- | backend/handlers/mod.go | 36 | ||||
| -rw-r--r-- | backend/handlers/record.go | 31 | ||||
| -rw-r--r-- | backend/handlers/user.go | 28 |
6 files changed, 9 insertions, 211 deletions
diff --git a/backend/handlers/discussions.go b/backend/handlers/discussions.go index 604eb39..6267695 100644 --- a/backend/handlers/discussions.go +++ b/backend/handlers/discussions.go | |||
| @@ -160,11 +160,7 @@ func CreateMapDiscussion(c *gin.Context) { | |||
| 160 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 160 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 161 | return | 161 | return |
| 162 | } | 162 | } |
| 163 | user, exists := c.Get("user") | 163 | user, _ := c.Get("user") |
| 164 | if !exists { | ||
| 165 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 166 | return | ||
| 167 | } | ||
| 168 | var request CreateMapDiscussionRequest | 164 | var request CreateMapDiscussionRequest |
| 169 | if err := c.BindJSON(&request); err != nil { | 165 | if err := c.BindJSON(&request); err != nil { |
| 170 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 166 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| @@ -206,11 +202,7 @@ func CreateMapDiscussionComment(c *gin.Context) { | |||
| 206 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 202 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 207 | return | 203 | return |
| 208 | } | 204 | } |
| 209 | user, exists := c.Get("user") | 205 | user, _ := c.Get("user") |
| 210 | if !exists { | ||
| 211 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 212 | return | ||
| 213 | } | ||
| 214 | var request CreateMapDiscussionCommentRequest | 206 | var request CreateMapDiscussionCommentRequest |
| 215 | if err := c.BindJSON(&request); err != nil { | 207 | if err := c.BindJSON(&request); err != nil { |
| 216 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 208 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| @@ -258,11 +250,7 @@ func EditMapDiscussion(c *gin.Context) { | |||
| 258 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 250 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 259 | return | 251 | return |
| 260 | } | 252 | } |
| 261 | user, exists := c.Get("user") | 253 | user, _ := c.Get("user") |
| 262 | if !exists { | ||
| 263 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 264 | return | ||
| 265 | } | ||
| 266 | var request EditMapDiscussionRequest | 254 | var request EditMapDiscussionRequest |
| 267 | if err := c.BindJSON(&request); err != nil { | 255 | if err := c.BindJSON(&request); err != nil { |
| 268 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 256 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| @@ -311,11 +299,7 @@ func DeleteMapDiscussion(c *gin.Context) { | |||
| 311 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 299 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 312 | return | 300 | return |
| 313 | } | 301 | } |
| 314 | user, exists := c.Get("user") | 302 | user, _ := c.Get("user") |
| 315 | if !exists { | ||
| 316 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 317 | return | ||
| 318 | } | ||
| 319 | sql := `UPDATE map_discussions SET is_deleted = true WHERE id = $1 AND map_id = $2 AND user_id = $3` | 303 | sql := `UPDATE map_discussions SET is_deleted = true WHERE id = $1 AND map_id = $2 AND user_id = $3` |
| 320 | result, err := database.DB.Exec(sql, discussionID, mapID, user.(models.User).SteamID) | 304 | result, err := database.DB.Exec(sql, discussionID, mapID, user.(models.User).SteamID) |
| 321 | if err != nil { | 305 | if err != nil { |
diff --git a/backend/handlers/login.go b/backend/handlers/login.go index 408d950..51c90d0 100644 --- a/backend/handlers/login.go +++ b/backend/handlers/login.go | |||
| @@ -40,7 +40,6 @@ func Login(c *gin.Context) { | |||
| 40 | default: | 40 | default: |
| 41 | steamID, err := openID.ValidateAndGetID() | 41 | steamID, err := openID.ValidateAndGetID() |
| 42 | if err != nil { | 42 | if err != nil { |
| 43 | CreateLog(steamID, LogTypeUser, LogDescriptionUserLoginFailValidate, err.Error()) | ||
| 44 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 43 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 45 | return | 44 | return |
| 46 | } | 45 | } |
| @@ -51,7 +50,6 @@ func Login(c *gin.Context) { | |||
| 51 | if checkSteamID == 0 { | 50 | if checkSteamID == 0 { |
| 52 | user, err := GetPlayerSummaries(steamID, os.Getenv("API_KEY")) | 51 | user, err := GetPlayerSummaries(steamID, os.Getenv("API_KEY")) |
| 53 | if err != nil { | 52 | if err != nil { |
| 54 | CreateLog(steamID, LogTypeUser, LogDescriptionUserLoginFailSummary, err.Error()) | ||
| 55 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 53 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 56 | return | 54 | return |
| 57 | } | 55 | } |
| @@ -81,7 +79,6 @@ func Login(c *gin.Context) { | |||
| 81 | // Sign and get the complete encoded token as a string using the secret | 79 | // Sign and get the complete encoded token as a string using the secret |
| 82 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) | 80 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) |
| 83 | if err != nil { | 81 | if err != nil { |
| 84 | CreateLog(steamID, LogTypeUser, LogDescriptionUserLoginFailToken, err.Error()) | ||
| 85 | c.JSON(http.StatusOK, models.ErrorResponse("Failed to generate token.")) | 82 | c.JSON(http.StatusOK, models.ErrorResponse("Failed to generate token.")) |
| 86 | return | 83 | return |
| 87 | } | 84 | } |
diff --git a/backend/handlers/logs.go b/backend/handlers/logs.go index 76ddac4..693c448 100644 --- a/backend/handlers/logs.go +++ b/backend/handlers/logs.go | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | package handlers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "fmt" | ||
| 5 | "net/http" | 4 | "net/http" |
| 6 | "time" | 5 | "time" |
| 7 | 6 | ||
| @@ -11,42 +10,6 @@ import ( | |||
| 11 | "github.com/gin-gonic/gin" | 10 | "github.com/gin-gonic/gin" |
| 12 | ) | 11 | ) |
| 13 | 12 | ||
| 14 | const ( | ||
| 15 | LogTypeMod string = "Mod" | ||
| 16 | LogTypeUser string = "User" | ||
| 17 | LogTypeRecord string = "Record" | ||
| 18 | |||
| 19 | LogDescriptionUserLoginSuccess string = "LoginSuccess" | ||
| 20 | LogDescriptionUserLoginFailToken string = "LoginTokenFail" | ||
| 21 | LogDescriptionUserLoginFailValidate string = "LoginValidateFail" | ||
| 22 | LogDescriptionUserLoginFailSummary string = "LoginSummaryFail" | ||
| 23 | LogDescriptionUserUpdateSuccess string = "UpdateSuccess" | ||
| 24 | LogDescriptionUserUpdateFail string = "UpdateFail" | ||
| 25 | LogDescriptionUserUpdateSummaryFail string = "UpdateSummaryFail" | ||
| 26 | LogDescriptionUserUpdateCountrySuccess string = "UpdateCountrySuccess" | ||
| 27 | LogDescriptionUserUpdateCountryFail string = "UpdateCountryFail" | ||
| 28 | |||
| 29 | LogDescriptionMapSummaryCreateSuccess string = "MapSummaryCreateSuccess" | ||
| 30 | LogDescriptionMapSummaryCreateFail string = "MapSummaryCreateFail" | ||
| 31 | LogDescriptionMapSummaryEditSuccess string = "MapSummaryEditSuccess" | ||
| 32 | LogDescriptionMapSummaryEditFail string = "MapSummaryEditFail" | ||
| 33 | LogDescriptionMapSummaryEditImageSuccess string = "MapSummaryEditImageSuccess" | ||
| 34 | LogDescriptionMapSummaryEditImageFail string = "MapSummaryEditImageFail" | ||
| 35 | LogDescriptionMapSummaryDeleteSuccess string = "MapSummaryDeleteSuccess" | ||
| 36 | LogDescriptionMapSummaryDeleteFail string = "MapSummaryDeleteFail" | ||
| 37 | |||
| 38 | LogDescriptionCreateRecordSuccess string = "CreateRecordSuccess" | ||
| 39 | LogDescriptionCreateRecordInsertRecordFail string = "InsertRecordFail" | ||
| 40 | LogDescriptionCreateRecordInsertDemoFail string = "InsertDemoFail" | ||
| 41 | LogDescriptionCreateRecordProcessDemoFail string = "ProcessDemoFail" | ||
| 42 | LogDescriptionCreateRecordCreateDemoFail string = "CreateDemoFail" | ||
| 43 | LogDescriptionCreateRecordOpenDemoFail string = "OpenDemoFail" | ||
| 44 | LogDescriptionCreateRecordSaveDemoFail string = "SaveDemoFail" | ||
| 45 | LogDescriptionCreateRecordInvalidRequestFail string = "InvalidRequestFail" | ||
| 46 | LogDescriptionDeleteRecordSuccess string = "DeleteRecordSuccess" | ||
| 47 | LogDescriptionDeleteRecordFail string = "DeleteRecordFail" | ||
| 48 | ) | ||
| 49 | |||
| 50 | type Log struct { | 13 | type Log struct { |
| 51 | User models.UserShort `json:"user"` | 14 | User models.UserShort `json:"user"` |
| 52 | Type string `json:"type"` | 15 | Type string `json:"type"` |
| @@ -80,54 +43,6 @@ type ScoreLogsResponseDetails struct { | |||
| 80 | Date time.Time `json:"date"` | 43 | Date time.Time `json:"date"` |
| 81 | } | 44 | } |
| 82 | 45 | ||
| 83 | // GET Mod Logs | ||
| 84 | // | ||
| 85 | // @Description Get mod logs. | ||
| 86 | // @Tags logs | ||
| 87 | // @Produce json | ||
| 88 | // @Param Authorization header string true "JWT Token" | ||
| 89 | // @Success 200 {object} models.Response{data=LogsResponse} | ||
| 90 | // @Router /logs/mod [get] | ||
| 91 | func ModLogs(c *gin.Context) { | ||
| 92 | mod, exists := c.Get("mod") | ||
| 93 | if !exists || !mod.(bool) { | ||
| 94 | c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions.")) | ||
| 95 | return | ||
| 96 | } | ||
| 97 | response := LogsResponse{Logs: []LogsResponseDetails{}} | ||
| 98 | sql := `SELECT u.user_name, l.user_id, l.type, l.description, l.message, l.date | ||
| 99 | FROM logs l INNER JOIN users u ON l.user_id = u.steam_id WHERE type != 'Score' | ||
| 100 | ORDER BY l.date DESC LIMIT 100;` | ||
| 101 | rows, err := database.DB.Query(sql) | ||
| 102 | if err != nil { | ||
| 103 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 104 | return | ||
| 105 | } | ||
| 106 | for rows.Next() { | ||
| 107 | log := Log{} | ||
| 108 | err = rows.Scan(&log.User.UserName, &log.User.SteamID, &log.Type, &log.Description, &log.Message, &log.Date) | ||
| 109 | if err != nil { | ||
| 110 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | ||
| 111 | return | ||
| 112 | } | ||
| 113 | detail := fmt.Sprintf("%s.%s", log.Type, log.Description) | ||
| 114 | response.Logs = append(response.Logs, LogsResponseDetails{ | ||
| 115 | User: models.UserShort{ | ||
| 116 | SteamID: log.User.SteamID, | ||
| 117 | UserName: log.User.UserName, | ||
| 118 | }, | ||
| 119 | Log: detail, | ||
| 120 | Message: log.Message, | ||
| 121 | Date: log.Date, | ||
| 122 | }) | ||
| 123 | } | ||
| 124 | c.JSON(http.StatusOK, models.Response{ | ||
| 125 | Success: true, | ||
| 126 | Message: "Successfully retrieved logs.", | ||
| 127 | Data: response, | ||
| 128 | }) | ||
| 129 | } | ||
| 130 | |||
| 131 | // GET Score Logs | 46 | // GET Score Logs |
| 132 | // | 47 | // |
| 133 | // @Description Get score logs of every player. | 48 | // @Description Get score logs of every player. |
| @@ -186,16 +101,3 @@ func ScoreLogs(c *gin.Context) { | |||
| 186 | Data: response, | 101 | Data: response, |
| 187 | }) | 102 | }) |
| 188 | } | 103 | } |
| 189 | |||
| 190 | func CreateLog(userID string, logType string, logDescription string, logMessage ...string) (err error) { | ||
| 191 | message := "-" | ||
| 192 | if len(logMessage) == 1 { | ||
| 193 | message = logMessage[0] | ||
| 194 | } | ||
| 195 | sql := `INSERT INTO logs (user_id, "type", description, message) VALUES($1, $2, $3, $4)` | ||
| 196 | _, err = database.DB.Exec(sql, userID, logType, logDescription, message) | ||
| 197 | if err != nil { | ||
| 198 | return err | ||
| 199 | } | ||
| 200 | return nil | ||
| 201 | } | ||
diff --git a/backend/handlers/mod.go b/backend/handlers/mod.go index 4fdc78a..66e1437 100644 --- a/backend/handlers/mod.go +++ b/backend/handlers/mod.go | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | package handlers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "fmt" | ||
| 5 | "net/http" | 4 | "net/http" |
| 6 | "strconv" | 5 | "strconv" |
| 7 | "time" | 6 | "time" |
| @@ -49,12 +48,6 @@ type EditMapImageRequest struct { | |||
| 49 | // @Success 200 {object} models.Response{data=CreateMapSummaryRequest} | 48 | // @Success 200 {object} models.Response{data=CreateMapSummaryRequest} |
| 50 | // @Router /maps/{mapid}/summary [post] | 49 | // @Router /maps/{mapid}/summary [post] |
| 51 | func CreateMapSummary(c *gin.Context) { | 50 | func CreateMapSummary(c *gin.Context) { |
| 52 | // Check if user exists | ||
| 53 | user, exists := c.Get("user") | ||
| 54 | if !exists { | ||
| 55 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 56 | return | ||
| 57 | } | ||
| 58 | mod, exists := c.Get("mod") | 51 | mod, exists := c.Get("mod") |
| 59 | if !exists || !mod.(bool) { | 52 | if !exists || !mod.(bool) { |
| 60 | c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions.")) | 53 | c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions.")) |
| @@ -69,7 +62,6 @@ func CreateMapSummary(c *gin.Context) { | |||
| 69 | } | 62 | } |
| 70 | var request CreateMapSummaryRequest | 63 | var request CreateMapSummaryRequest |
| 71 | if err := c.BindJSON(&request); err != nil { | 64 | if err := c.BindJSON(&request); err != nil { |
| 72 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryCreateFail, fmt.Sprintf("BIND: %s", err.Error())) | ||
| 73 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 65 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 74 | return | 66 | return |
| 75 | } | 67 | } |
| @@ -85,7 +77,6 @@ func CreateMapSummary(c *gin.Context) { | |||
| 85 | sql := `SELECT m.id FROM maps m WHERE m.id = $1` | 77 | sql := `SELECT m.id FROM maps m WHERE m.id = $1` |
| 86 | err = database.DB.QueryRow(sql, mapID).Scan(&checkMapID) | 78 | err = database.DB.QueryRow(sql, mapID).Scan(&checkMapID) |
| 87 | if err != nil { | 79 | if err != nil { |
| 88 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryCreateFail, fmt.Sprintf("SELECT#maps: %s", err.Error())) | ||
| 89 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 80 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 90 | return | 81 | return |
| 91 | } | 82 | } |
| @@ -98,7 +89,6 @@ func CreateMapSummary(c *gin.Context) { | |||
| 98 | VALUES ($1,$2,$3,$4,$5,$6,$7)` | 89 | VALUES ($1,$2,$3,$4,$5,$6,$7)` |
| 99 | _, err = tx.Exec(sql, mapID, request.CategoryID, request.UserName, *request.ScoreCount, request.Description, request.Showcase, request.RecordDate) | 90 | _, err = tx.Exec(sql, mapID, request.CategoryID, request.UserName, *request.ScoreCount, request.Description, request.Showcase, request.RecordDate) |
| 100 | if err != nil { | 91 | if err != nil { |
| 101 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryCreateFail, fmt.Sprintf("INSERT#map_history: %s", err.Error())) | ||
| 102 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 92 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 103 | return | 93 | return |
| 104 | } | 94 | } |
| @@ -106,7 +96,6 @@ func CreateMapSummary(c *gin.Context) { | |||
| 106 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 96 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 107 | return | 97 | return |
| 108 | } | 98 | } |
| 109 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryCreateSuccess, fmt.Sprintf("MapID: %d | CategoryID: %d | ScoreCount: %d", mapID, request.CategoryID, *request.ScoreCount)) | ||
| 110 | c.JSON(http.StatusOK, models.Response{ | 99 | c.JSON(http.StatusOK, models.Response{ |
| 111 | Success: true, | 100 | Success: true, |
| 112 | Message: "Successfully created map summary.", | 101 | Message: "Successfully created map summary.", |
| @@ -125,12 +114,6 @@ func CreateMapSummary(c *gin.Context) { | |||
| 125 | // @Success 200 {object} models.Response{data=EditMapSummaryRequest} | 114 | // @Success 200 {object} models.Response{data=EditMapSummaryRequest} |
| 126 | // @Router /maps/{mapid}/summary [put] | 115 | // @Router /maps/{mapid}/summary [put] |
| 127 | func EditMapSummary(c *gin.Context) { | 116 | func EditMapSummary(c *gin.Context) { |
| 128 | // Check if user exists | ||
| 129 | user, exists := c.Get("user") | ||
| 130 | if !exists { | ||
| 131 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 132 | return | ||
| 133 | } | ||
| 134 | mod, exists := c.Get("mod") | 117 | mod, exists := c.Get("mod") |
| 135 | if !exists || !mod.(bool) { | 118 | if !exists || !mod.(bool) { |
| 136 | c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions.")) | 119 | c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions.")) |
| @@ -146,7 +129,6 @@ func EditMapSummary(c *gin.Context) { | |||
| 146 | } | 129 | } |
| 147 | var request EditMapSummaryRequest | 130 | var request EditMapSummaryRequest |
| 148 | if err := c.BindJSON(&request); err != nil { | 131 | if err := c.BindJSON(&request); err != nil { |
| 149 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditFail, fmt.Sprintf("BIND: %s", err.Error())) | ||
| 150 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 132 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 151 | return | 133 | return |
| 152 | } | 134 | } |
| @@ -161,7 +143,6 @@ func EditMapSummary(c *gin.Context) { | |||
| 161 | sql := `UPDATE map_history SET user_name = $2, score_count = $3, record_date = $4, description = $5, showcase = $6 WHERE id = $1` | 143 | sql := `UPDATE map_history SET user_name = $2, score_count = $3, record_date = $4, description = $5, showcase = $6 WHERE id = $1` |
| 162 | _, err = tx.Exec(sql, request.RouteID, request.UserName, *request.ScoreCount, request.RecordDate, request.Description, request.Showcase) | 144 | _, err = tx.Exec(sql, request.RouteID, request.UserName, *request.ScoreCount, request.RecordDate, request.Description, request.Showcase) |
| 163 | if err != nil { | 145 | if err != nil { |
| 164 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditFail, fmt.Sprintf("(HistoryID: %d) UPDATE#map_history: %s", request.RouteID, err.Error())) | ||
| 165 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 146 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 166 | return | 147 | return |
| 167 | } | 148 | } |
| @@ -187,12 +168,6 @@ func EditMapSummary(c *gin.Context) { | |||
| 187 | // @Success 200 {object} models.Response{data=DeleteMapSummaryRequest} | 168 | // @Success 200 {object} models.Response{data=DeleteMapSummaryRequest} |
| 188 | // @Router /maps/{mapid}/summary [delete] | 169 | // @Router /maps/{mapid}/summary [delete] |
| 189 | func DeleteMapSummary(c *gin.Context) { | 170 | func DeleteMapSummary(c *gin.Context) { |
| 190 | // Check if user exists | ||
| 191 | user, exists := c.Get("user") | ||
| 192 | if !exists { | ||
| 193 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 194 | return | ||
| 195 | } | ||
| 196 | mod, exists := c.Get("mod") | 171 | mod, exists := c.Get("mod") |
| 197 | if !exists || !mod.(bool) { | 172 | if !exists || !mod.(bool) { |
| 198 | c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions.")) | 173 | c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions.")) |
| @@ -208,7 +183,6 @@ func DeleteMapSummary(c *gin.Context) { | |||
| 208 | } | 183 | } |
| 209 | var request DeleteMapSummaryRequest | 184 | var request DeleteMapSummaryRequest |
| 210 | if err := c.BindJSON(&request); err != nil { | 185 | if err := c.BindJSON(&request); err != nil { |
| 211 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditFail, fmt.Sprintf("(RouteID: %d) BIND: %s", request.RouteID, err.Error())) | ||
| 212 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 186 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 213 | return | 187 | return |
| 214 | } | 188 | } |
| @@ -223,7 +197,6 @@ func DeleteMapSummary(c *gin.Context) { | |||
| 223 | sql := `DELETE FROM map_history mh WHERE mh.id = $1` | 197 | sql := `DELETE FROM map_history mh WHERE mh.id = $1` |
| 224 | _, err = tx.Exec(sql, request.RouteID) | 198 | _, err = tx.Exec(sql, request.RouteID) |
| 225 | if err != nil { | 199 | if err != nil { |
| 226 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryDeleteFail, fmt.Sprintf("(HistoryID: %d) DELETE#map_history: %s", request.RouteID, err.Error())) | ||
| 227 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 200 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 228 | return | 201 | return |
| 229 | } | 202 | } |
| @@ -249,12 +222,6 @@ func DeleteMapSummary(c *gin.Context) { | |||
| 249 | // @Success 200 {object} models.Response{data=EditMapImageRequest} | 222 | // @Success 200 {object} models.Response{data=EditMapImageRequest} |
| 250 | // @Router /maps/{mapid}/image [put] | 223 | // @Router /maps/{mapid}/image [put] |
| 251 | func EditMapImage(c *gin.Context) { | 224 | func EditMapImage(c *gin.Context) { |
| 252 | // Check if user exists | ||
| 253 | user, exists := c.Get("user") | ||
| 254 | if !exists { | ||
| 255 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 256 | return | ||
| 257 | } | ||
| 258 | mod, exists := c.Get("mod") | 225 | mod, exists := c.Get("mod") |
| 259 | if !exists || !mod.(bool) { | 226 | if !exists || !mod.(bool) { |
| 260 | c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions.")) | 227 | c.JSON(http.StatusOK, models.ErrorResponse("Insufficient permissions.")) |
| @@ -269,7 +236,6 @@ func EditMapImage(c *gin.Context) { | |||
| 269 | } | 236 | } |
| 270 | var request EditMapImageRequest | 237 | var request EditMapImageRequest |
| 271 | if err := c.BindJSON(&request); err != nil { | 238 | if err := c.BindJSON(&request); err != nil { |
| 272 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditImageFail, fmt.Sprintf("BIND: %s", err.Error())) | ||
| 273 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 239 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 274 | return | 240 | return |
| 275 | } | 241 | } |
| @@ -277,11 +243,9 @@ func EditMapImage(c *gin.Context) { | |||
| 277 | sql := `UPDATE maps SET image = $2 WHERE id = $1` | 243 | sql := `UPDATE maps SET image = $2 WHERE id = $1` |
| 278 | _, err = database.DB.Exec(sql, mapID, request.Image) | 244 | _, err = database.DB.Exec(sql, mapID, request.Image) |
| 279 | if err != nil { | 245 | if err != nil { |
| 280 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditImageFail, fmt.Sprintf("UPDATE#maps: %s", err.Error())) | ||
| 281 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 246 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 282 | return | 247 | return |
| 283 | } | 248 | } |
| 284 | CreateLog(user.(models.User).SteamID, LogTypeMod, LogDescriptionMapSummaryEditImageSuccess) | ||
| 285 | c.JSON(http.StatusOK, models.Response{ | 249 | c.JSON(http.StatusOK, models.Response{ |
| 286 | Success: true, | 250 | Success: true, |
| 287 | Message: "Successfully updated map image.", | 251 | Message: "Successfully updated map image.", |
diff --git a/backend/handlers/record.go b/backend/handlers/record.go index e43cc61..bedde57 100644 --- a/backend/handlers/record.go +++ b/backend/handlers/record.go | |||
| @@ -53,12 +53,7 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 53 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 53 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 54 | return | 54 | return |
| 55 | } | 55 | } |
| 56 | // Check if user exists | 56 | user, _ := c.Get("user") |
| 57 | user, exists := c.Get("user") | ||
| 58 | if !exists { | ||
| 59 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 60 | return | ||
| 61 | } | ||
| 62 | // Check if map is sp or mp | 57 | // Check if map is sp or mp |
| 63 | var gameName string | 58 | var gameName string |
| 64 | var isCoop bool | 59 | var isCoop bool |
| @@ -76,12 +71,10 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 76 | // Get record request | 71 | // Get record request |
| 77 | var record RecordRequest | 72 | var record RecordRequest |
| 78 | if err := c.ShouldBind(&record); err != nil { | 73 | if err := c.ShouldBind(&record); err != nil { |
| 79 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordInvalidRequestFail, "BIND: "+err.Error()) | ||
| 80 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 74 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 81 | return | 75 | return |
| 82 | } | 76 | } |
| 83 | if isCoop && record.PartnerDemo == nil { | 77 | if isCoop && record.PartnerDemo == nil { |
| 84 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordInvalidRequestFail) | ||
| 85 | c.JSON(http.StatusOK, models.ErrorResponse("Missing partner demo for coop submission.")) | 78 | c.JSON(http.StatusOK, models.ErrorResponse("Missing partner demo for coop submission.")) |
| 86 | return | 79 | return |
| 87 | } | 80 | } |
| @@ -112,21 +105,18 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 112 | // Upload & insert into demos | 105 | // Upload & insert into demos |
| 113 | err = c.SaveUploadedFile(header, "parser/"+uuid+".dem") | 106 | err = c.SaveUploadedFile(header, "parser/"+uuid+".dem") |
| 114 | if err != nil { | 107 | if err != nil { |
| 115 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordSaveDemoFail, err.Error()) | ||
| 116 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 108 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 117 | return | 109 | return |
| 118 | } | 110 | } |
| 119 | defer os.Remove("parser/" + uuid + ".dem") | 111 | defer os.Remove("parser/" + uuid + ".dem") |
| 120 | f, err := os.Open("parser/" + uuid + ".dem") | 112 | f, err := os.Open("parser/" + uuid + ".dem") |
| 121 | if err != nil { | 113 | if err != nil { |
| 122 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordOpenDemoFail, err.Error()) | ||
| 123 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 114 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 124 | return | 115 | return |
| 125 | } | 116 | } |
| 126 | defer f.Close() | 117 | defer f.Close() |
| 127 | parserResult, err := parser.ProcessDemo("parser/" + uuid + ".dem") | 118 | parserResult, err := parser.ProcessDemo("parser/" + uuid + ".dem") |
| 128 | if err != nil { | 119 | if err != nil { |
| 129 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordProcessDemoFail, err.Error()) | ||
| 130 | c.JSON(http.StatusOK, models.ErrorResponse("Error while processing demo: "+err.Error())) | 120 | c.JSON(http.StatusOK, models.ErrorResponse("Error while processing demo: "+err.Error())) |
| 131 | return | 121 | return |
| 132 | } | 122 | } |
| @@ -139,7 +129,6 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 139 | hostSteamID = parserResult.HostSteamID | 129 | hostSteamID = parserResult.HostSteamID |
| 140 | partnerSteamID = parserResult.PartnerSteamID | 130 | partnerSteamID = parserResult.PartnerSteamID |
| 141 | if hostDemoScoreCount == 0 && hostDemoScoreTime == 0 { | 131 | if hostDemoScoreCount == 0 && hostDemoScoreTime == 0 { |
| 142 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordProcessDemoFail, err.Error()) | ||
| 143 | c.JSON(http.StatusOK, models.ErrorResponse("Processing demo went wrong. Please contact a web admin and provide the demo in question.")) | 132 | c.JSON(http.StatusOK, models.ErrorResponse("Processing demo went wrong. Please contact a web admin and provide the demo in question.")) |
| 144 | return | 133 | return |
| 145 | } | 134 | } |
| @@ -161,7 +150,6 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 161 | } | 150 | } |
| 162 | file, err := createFile(srv, uuid+".dem", "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) | 151 | file, err := createFile(srv, uuid+".dem", "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) |
| 163 | if err != nil { | 152 | if err != nil { |
| 164 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordCreateDemoFail, err.Error()) | ||
| 165 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 153 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 166 | return | 154 | return |
| 167 | } | 155 | } |
| @@ -177,7 +165,6 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 177 | _, err = tx.Exec(`INSERT INTO demos (id,location_id) VALUES ($1,$2)`, uuid, file.Id) | 165 | _, err = tx.Exec(`INSERT INTO demos (id,location_id) VALUES ($1,$2)`, uuid, file.Id) |
| 178 | if err != nil { | 166 | if err != nil { |
| 179 | deleteFile(srv, file.Id) | 167 | deleteFile(srv, file.Id) |
| 180 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordInsertDemoFail, err.Error()) | ||
| 181 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 168 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 182 | return | 169 | return |
| 183 | } | 170 | } |
| @@ -229,7 +216,6 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 229 | if err != nil { | 216 | if err != nil { |
| 230 | deleteFile(srv, hostDemoFileID) | 217 | deleteFile(srv, hostDemoFileID) |
| 231 | deleteFile(srv, partnerDemoFileID) | 218 | deleteFile(srv, partnerDemoFileID) |
| 232 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordInsertRecordFail, err.Error()) | ||
| 233 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 219 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 234 | return | 220 | return |
| 235 | } | 221 | } |
| @@ -239,7 +225,6 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 239 | _, err := tx.Exec(sql, mapID, hostDemoScoreCount, hostDemoScoreTime, user.(models.User).SteamID, hostDemoUUID) | 225 | _, err := tx.Exec(sql, mapID, hostDemoScoreCount, hostDemoScoreTime, user.(models.User).SteamID, hostDemoUUID) |
| 240 | if err != nil { | 226 | if err != nil { |
| 241 | deleteFile(srv, hostDemoFileID) | 227 | deleteFile(srv, hostDemoFileID) |
| 242 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordInsertRecordFail, err.Error()) | ||
| 243 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 228 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 244 | return | 229 | return |
| 245 | } | 230 | } |
| @@ -248,7 +233,6 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 248 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 233 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 249 | return | 234 | return |
| 250 | } | 235 | } |
| 251 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionCreateRecordSuccess) | ||
| 252 | c.JSON(http.StatusOK, models.Response{ | 236 | c.JSON(http.StatusOK, models.Response{ |
| 253 | Success: true, | 237 | Success: true, |
| 254 | Message: "Successfully created record.", | 238 | Message: "Successfully created record.", |
| @@ -277,11 +261,7 @@ func DeleteRecord(c *gin.Context) { | |||
| 277 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 261 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 278 | return | 262 | return |
| 279 | } | 263 | } |
| 280 | user, exists := c.Get("user") | 264 | user, _ := c.Get("user") |
| 281 | if !exists { | ||
| 282 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 283 | return | ||
| 284 | } | ||
| 285 | // Validate map | 265 | // Validate map |
| 286 | var validateMapID int | 266 | var validateMapID int |
| 287 | var isCoop bool | 267 | var isCoop bool |
| @@ -302,12 +282,10 @@ func DeleteRecord(c *gin.Context) { | |||
| 302 | sql = `SELECT mp.id FROM records_mp mp WHERE mp.id = $1 AND mp.map_id = $2 AND (mp.host_id = $3 OR mp.partner_id = $3) AND is_deleted = false` | 282 | sql = `SELECT mp.id FROM records_mp mp WHERE mp.id = $1 AND mp.map_id = $2 AND (mp.host_id = $3 OR mp.partner_id = $3) AND is_deleted = false` |
| 303 | err = database.DB.QueryRow(sql, recordID, mapID, user.(models.User).SteamID).Scan(&validateRecordID) | 283 | err = database.DB.QueryRow(sql, recordID, mapID, user.(models.User).SteamID).Scan(&validateRecordID) |
| 304 | if err != nil { | 284 | if err != nil { |
| 305 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordFail, "SELECT#records_mp: "+err.Error()) | ||
| 306 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 285 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 307 | return | 286 | return |
| 308 | } | 287 | } |
| 309 | if recordID != validateRecordID { | 288 | if recordID != validateRecordID { |
| 310 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordFail, "recordID != validateRecordID") | ||
| 311 | c.JSON(http.StatusOK, models.ErrorResponse("Selected record does not exist.")) | 289 | c.JSON(http.StatusOK, models.ErrorResponse("Selected record does not exist.")) |
| 312 | return | 290 | return |
| 313 | } | 291 | } |
| @@ -315,7 +293,6 @@ func DeleteRecord(c *gin.Context) { | |||
| 315 | sql = `UPDATE records_mp SET is_deleted = true WHERE id = $1` | 293 | sql = `UPDATE records_mp SET is_deleted = true WHERE id = $1` |
| 316 | _, err = database.DB.Exec(sql, recordID) | 294 | _, err = database.DB.Exec(sql, recordID) |
| 317 | if err != nil { | 295 | if err != nil { |
| 318 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordFail, "UPDATE#records_mp: "+err.Error()) | ||
| 319 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 296 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 320 | return | 297 | return |
| 321 | } | 298 | } |
| @@ -325,12 +302,10 @@ func DeleteRecord(c *gin.Context) { | |||
| 325 | sql = `SELECT sp.id FROM records_sp sp WHERE sp.id = $1 AND sp.map_id = $2 AND sp.user_id = $3 AND is_deleted = false` | 302 | sql = `SELECT sp.id FROM records_sp sp WHERE sp.id = $1 AND sp.map_id = $2 AND sp.user_id = $3 AND is_deleted = false` |
| 326 | err = database.DB.QueryRow(sql, recordID, mapID, user.(models.User).SteamID).Scan(&validateRecordID) | 303 | err = database.DB.QueryRow(sql, recordID, mapID, user.(models.User).SteamID).Scan(&validateRecordID) |
| 327 | if err != nil { | 304 | if err != nil { |
| 328 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordFail, "SELECT#records_sp: "+err.Error()) | ||
| 329 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 305 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 330 | return | 306 | return |
| 331 | } | 307 | } |
| 332 | if recordID != validateRecordID { | 308 | if recordID != validateRecordID { |
| 333 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordFail, "recordID != validateRecordID") | ||
| 334 | c.JSON(http.StatusOK, models.ErrorResponse("Selected record does not exist.")) | 309 | c.JSON(http.StatusOK, models.ErrorResponse("Selected record does not exist.")) |
| 335 | return | 310 | return |
| 336 | } | 311 | } |
| @@ -338,12 +313,10 @@ func DeleteRecord(c *gin.Context) { | |||
| 338 | sql = `UPDATE records_sp SET is_deleted = true WHERE id = $1` | 313 | sql = `UPDATE records_sp SET is_deleted = true WHERE id = $1` |
| 339 | _, err = database.DB.Exec(sql, recordID) | 314 | _, err = database.DB.Exec(sql, recordID) |
| 340 | if err != nil { | 315 | if err != nil { |
| 341 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordFail, "UPDATE#records_sp: "+err.Error()) | ||
| 342 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 316 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 343 | return | 317 | return |
| 344 | } | 318 | } |
| 345 | } | 319 | } |
| 346 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionDeleteRecordSuccess) | ||
| 347 | c.JSON(http.StatusOK, models.Response{ | 320 | c.JSON(http.StatusOK, models.Response{ |
| 348 | Success: true, | 321 | Success: true, |
| 349 | Message: "Successfully deleted record.", | 322 | Message: "Successfully deleted record.", |
diff --git a/backend/handlers/user.go b/backend/handlers/user.go index 021a47f..53f0d06 100644 --- a/backend/handlers/user.go +++ b/backend/handlers/user.go | |||
| @@ -69,12 +69,7 @@ type ScoreResponse struct { | |||
| 69 | // @Success 200 {object} models.Response{data=ProfileResponse} | 69 | // @Success 200 {object} models.Response{data=ProfileResponse} |
| 70 | // @Router /profile [get] | 70 | // @Router /profile [get] |
| 71 | func Profile(c *gin.Context) { | 71 | func Profile(c *gin.Context) { |
| 72 | // Check if user exists | 72 | user, _ := c.Get("user") |
| 73 | user, exists := c.Get("user") | ||
| 74 | if !exists { | ||
| 75 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 76 | return | ||
| 77 | } | ||
| 78 | // Get user links | 73 | // Get user links |
| 79 | links := models.Links{} | 74 | links := models.Links{} |
| 80 | sql := `SELECT u.p2sr, u.steam, u.youtube, u.twitch FROM users u WHERE u.steam_id = $1` | 75 | sql := `SELECT u.p2sr, u.steam, u.youtube, u.twitch FROM users u WHERE u.steam_id = $1` |
| @@ -699,15 +694,9 @@ func FetchUser(c *gin.Context) { | |||
| 699 | // @Success 200 {object} models.Response{data=ProfileResponse} | 694 | // @Success 200 {object} models.Response{data=ProfileResponse} |
| 700 | // @Router /profile [post] | 695 | // @Router /profile [post] |
| 701 | func UpdateUser(c *gin.Context) { | 696 | func UpdateUser(c *gin.Context) { |
| 702 | // Check if user exists | 697 | user, _ := c.Get("user") |
| 703 | user, exists := c.Get("user") | ||
| 704 | if !exists { | ||
| 705 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 706 | return | ||
| 707 | } | ||
| 708 | profile, err := GetPlayerSummaries(user.(models.User).SteamID, os.Getenv("API_KEY")) | 698 | profile, err := GetPlayerSummaries(user.(models.User).SteamID, os.Getenv("API_KEY")) |
| 709 | if err != nil { | 699 | if err != nil { |
| 710 | CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateSummaryFail, err.Error()) | ||
| 711 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 700 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 712 | return | 701 | return |
| 713 | } | 702 | } |
| @@ -715,11 +704,9 @@ func UpdateUser(c *gin.Context) { | |||
| 715 | sql := `UPDATE users SET user_name = $1, avatar_link = $2, country_code = $3, updated_at = $4 WHERE steam_id = $5` | 704 | sql := `UPDATE users SET user_name = $1, avatar_link = $2, country_code = $3, updated_at = $4 WHERE steam_id = $5` |
| 716 | _, err = database.DB.Exec(sql, profile.PersonaName, profile.AvatarFull, profile.LocCountryCode, time.Now().UTC(), user.(models.User).SteamID) | 705 | _, err = database.DB.Exec(sql, profile.PersonaName, profile.AvatarFull, profile.LocCountryCode, time.Now().UTC(), user.(models.User).SteamID) |
| 717 | if err != nil { | 706 | if err != nil { |
| 718 | CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateFail, "UPDATE#users: "+err.Error()) | ||
| 719 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 707 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 720 | return | 708 | return |
| 721 | } | 709 | } |
| 722 | CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateSuccess) | ||
| 723 | c.JSON(http.StatusOK, models.Response{ | 710 | c.JSON(http.StatusOK, models.Response{ |
| 724 | Success: true, | 711 | Success: true, |
| 725 | Message: "Successfully updated user.", | 712 | Message: "Successfully updated user.", |
| @@ -744,33 +731,24 @@ func UpdateUser(c *gin.Context) { | |||
| 744 | // @Success 200 {object} models.Response | 731 | // @Success 200 {object} models.Response |
| 745 | // @Router /profile [put] | 732 | // @Router /profile [put] |
| 746 | func UpdateCountryCode(c *gin.Context) { | 733 | func UpdateCountryCode(c *gin.Context) { |
| 747 | // Check if user exists | 734 | user, _ := c.Get("user") |
| 748 | user, exists := c.Get("user") | ||
| 749 | if !exists { | ||
| 750 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) | ||
| 751 | return | ||
| 752 | } | ||
| 753 | code := c.Query("country_code") | 735 | code := c.Query("country_code") |
| 754 | if code == "" { | 736 | if code == "" { |
| 755 | CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateCountryFail) | ||
| 756 | c.JSON(http.StatusOK, models.ErrorResponse("Enter a valid country code.")) | 737 | c.JSON(http.StatusOK, models.ErrorResponse("Enter a valid country code.")) |
| 757 | return | 738 | return |
| 758 | } | 739 | } |
| 759 | var validCode string | 740 | var validCode string |
| 760 | err := database.DB.QueryRow(`SELECT country_code FROM countries WHERE country_code = $1`, code).Scan(&validCode) | 741 | err := database.DB.QueryRow(`SELECT country_code FROM countries WHERE country_code = $1`, code).Scan(&validCode) |
| 761 | if err != nil { | 742 | if err != nil { |
| 762 | CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateCountryFail) | ||
| 763 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 743 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 764 | return | 744 | return |
| 765 | } | 745 | } |
| 766 | // Valid code, update profile | 746 | // Valid code, update profile |
| 767 | _, err = database.DB.Exec(`UPDATE users SET country_code = $1 WHERE steam_id = $2`, validCode, user.(models.User).SteamID) | 747 | _, err = database.DB.Exec(`UPDATE users SET country_code = $1 WHERE steam_id = $2`, validCode, user.(models.User).SteamID) |
| 768 | if err != nil { | 748 | if err != nil { |
| 769 | CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateCountryFail) | ||
| 770 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) | 749 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 771 | return | 750 | return |
| 772 | } | 751 | } |
| 773 | CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateCountrySuccess) | ||
| 774 | c.JSON(http.StatusOK, models.Response{ | 752 | c.JSON(http.StatusOK, models.Response{ |
| 775 | Success: true, | 753 | Success: true, |
| 776 | Message: "Successfully updated country code.", | 754 | Message: "Successfully updated country code.", |