diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-09-22 17:48:43 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-09-22 17:48:43 +0300 |
| commit | 3cda2073cc29317d6251e7b88102b70659aed6d6 (patch) | |
| tree | ec96bc99b6bc5d4a1f3a45efcfe6cb2e8514b7fa /backend/handlers/record.go | |
| parent | feat: completion count on summary for each cm entry (#63) (diff) | |
| download | lphub-3cda2073cc29317d6251e7b88102b70659aed6d6.tar.gz lphub-3cda2073cc29317d6251e7b88102b70659aed6d6.tar.bz2 lphub-3cda2073cc29317d6251e7b88102b70659aed6d6.zip | |
fix: change all status codes to 200 (#66)
Former-commit-id: ae632415e3f6f79a462240f151ada2e428318c6b
Diffstat (limited to 'backend/handlers/record.go')
| -rw-r--r-- | backend/handlers/record.go | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/backend/handlers/record.go b/backend/handlers/record.go index 3d29eb8..75b5583 100644 --- a/backend/handlers/record.go +++ b/backend/handlers/record.go | |||
| @@ -52,7 +52,7 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 52 | // Check if user exists | 52 | // Check if user exists |
| 53 | user, exists := c.Get("user") | 53 | user, exists := c.Get("user") |
| 54 | if !exists { | 54 | if !exists { |
| 55 | c.JSON(http.StatusUnauthorized, models.ErrorResponse("User not logged in.")) | 55 | c.JSON(http.StatusOK, models.ErrorResponse("User not logged in.")) |
| 56 | return | 56 | return |
| 57 | } | 57 | } |
| 58 | // Check if map is sp or mp | 58 | // Check if map is sp or mp |
| @@ -62,12 +62,12 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 62 | sql := `SELECT g.name, m.is_disabled FROM maps m INNER JOIN games g ON m.game_id=g.id WHERE m.id = $1` | 62 | sql := `SELECT g.name, m.is_disabled FROM maps m INNER JOIN games g ON m.game_id=g.id WHERE m.id = $1` |
| 63 | err := database.DB.QueryRow(sql, mapId).Scan(&gameName, &isDisabled) | 63 | err := database.DB.QueryRow(sql, mapId).Scan(&gameName, &isDisabled) |
| 64 | if err != nil { | 64 | if err != nil { |
| 65 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 65 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 66 | return | 66 | return |
| 67 | } | 67 | } |
| 68 | if isDisabled { | 68 | if isDisabled { |
| 69 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInvalidRequest) | 69 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInvalidRequest) |
| 70 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Map is not available for competitive boards.")) | 70 | c.JSON(http.StatusOK, models.ErrorResponse("Map is not available for competitive boards.")) |
| 71 | return | 71 | return |
| 72 | } | 72 | } |
| 73 | if gameName == "Portal 2 - Cooperative" { | 73 | if gameName == "Portal 2 - Cooperative" { |
| @@ -77,12 +77,12 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 77 | var record RecordRequest | 77 | var record RecordRequest |
| 78 | if err := c.ShouldBind(&record); err != nil { | 78 | if err := c.ShouldBind(&record); err != nil { |
| 79 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInvalidRequest) | 79 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInvalidRequest) |
| 80 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 80 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 81 | return | 81 | return |
| 82 | } | 82 | } |
| 83 | if isCoop && (record.PartnerDemo == nil || record.PartnerID == "") { | 83 | if isCoop && (record.PartnerDemo == nil || record.PartnerID == "") { |
| 84 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInvalidRequest) | 84 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInvalidRequest) |
| 85 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Invalid entry for coop record submission.")) | 85 | c.JSON(http.StatusOK, models.ErrorResponse("Invalid entry for coop record submission.")) |
| 86 | return | 86 | return |
| 87 | } | 87 | } |
| 88 | // Demo files | 88 | // Demo files |
| @@ -95,13 +95,13 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 95 | client := serviceAccount() | 95 | client := serviceAccount() |
| 96 | srv, err := drive.New(client) | 96 | srv, err := drive.New(client) |
| 97 | if err != nil { | 97 | if err != nil { |
| 98 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 98 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 99 | return | 99 | return |
| 100 | } | 100 | } |
| 101 | // Create database transaction for inserts | 101 | // Create database transaction for inserts |
| 102 | tx, err := database.DB.Begin() | 102 | tx, err := database.DB.Begin() |
| 103 | if err != nil { | 103 | if err != nil { |
| 104 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | 104 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 105 | return | 105 | return |
| 106 | } | 106 | } |
| 107 | // Defer to a rollback in case anything fails | 107 | // Defer to a rollback in case anything fails |
| @@ -112,27 +112,27 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 112 | err = c.SaveUploadedFile(header, "backend/parser/"+uuid+".dem") | 112 | err = c.SaveUploadedFile(header, "backend/parser/"+uuid+".dem") |
| 113 | if err != nil { | 113 | if err != nil { |
| 114 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailSaveDemo) | 114 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailSaveDemo) |
| 115 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 115 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 116 | return | 116 | return |
| 117 | } | 117 | } |
| 118 | defer os.Remove("backend/parser/" + uuid + ".dem") | 118 | defer os.Remove("backend/parser/" + uuid + ".dem") |
| 119 | f, err := os.Open("backend/parser/" + uuid + ".dem") | 119 | f, err := os.Open("backend/parser/" + uuid + ".dem") |
| 120 | if err != nil { | 120 | if err != nil { |
| 121 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailOpenDemo) | 121 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailOpenDemo) |
| 122 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 122 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 123 | return | 123 | return |
| 124 | } | 124 | } |
| 125 | defer f.Close() | 125 | defer f.Close() |
| 126 | file, err := createFile(srv, uuid+".dem", "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) | 126 | file, err := createFile(srv, uuid+".dem", "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID")) |
| 127 | if err != nil { | 127 | if err != nil { |
| 128 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailCreateDemo) | 128 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailCreateDemo) |
| 129 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 129 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 130 | return | 130 | return |
| 131 | } | 131 | } |
| 132 | hostDemoScoreCount, hostDemoScoreTime, err = parser.ProcessDemo("backend/parser/" + uuid + ".dem") | 132 | hostDemoScoreCount, hostDemoScoreTime, err = parser.ProcessDemo("backend/parser/" + uuid + ".dem") |
| 133 | if err != nil { | 133 | if err != nil { |
| 134 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailProcessDemo) | 134 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailProcessDemo) |
| 135 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 135 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 136 | return | 136 | return |
| 137 | } | 137 | } |
| 138 | if i == 0 { | 138 | if i == 0 { |
| @@ -146,7 +146,7 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 146 | if err != nil { | 146 | if err != nil { |
| 147 | deleteFile(srv, file.Id) | 147 | deleteFile(srv, file.Id) |
| 148 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInsertDemo) | 148 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInsertDemo) |
| 149 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 149 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 150 | return | 150 | return |
| 151 | } | 151 | } |
| 152 | } | 152 | } |
| @@ -168,14 +168,14 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 168 | deleteFile(srv, hostDemoFileID) | 168 | deleteFile(srv, hostDemoFileID) |
| 169 | deleteFile(srv, partnerDemoFileID) | 169 | deleteFile(srv, partnerDemoFileID) |
| 170 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInsertRecord) | 170 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInsertRecord) |
| 171 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 171 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 172 | return | 172 | return |
| 173 | } | 173 | } |
| 174 | // If a new world record based on portal count | 174 | // If a new world record based on portal count |
| 175 | // if record.ScoreCount < wrScore { | 175 | // if record.ScoreCount < wrScore { |
| 176 | // _, err := tx.Exec(`UPDATE maps SET wr_score = $1, wr_time = $2 WHERE id = $3`, record.ScoreCount, record.ScoreTime, mapId) | 176 | // _, err := tx.Exec(`UPDATE maps SET wr_score = $1, wr_time = $2 WHERE id = $3`, record.ScoreCount, record.ScoreTime, mapId) |
| 177 | // if err != nil { | 177 | // if err != nil { |
| 178 | // c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 178 | // c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 179 | // return | 179 | // return |
| 180 | // } | 180 | // } |
| 181 | // } | 181 | // } |
| @@ -186,20 +186,20 @@ func CreateRecordWithDemo(c *gin.Context) { | |||
| 186 | if err != nil { | 186 | if err != nil { |
| 187 | deleteFile(srv, hostDemoFileID) | 187 | deleteFile(srv, hostDemoFileID) |
| 188 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInsertRecord) | 188 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordFailInsertRecord) |
| 189 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 189 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 190 | return | 190 | return |
| 191 | } | 191 | } |
| 192 | // If a new world record based on portal count | 192 | // If a new world record based on portal count |
| 193 | // if record.ScoreCount < wrScore { | 193 | // if record.ScoreCount < wrScore { |
| 194 | // _, err := tx.Exec(`UPDATE maps SET wr_score = $1, wr_time = $2 WHERE id = $3`, record.ScoreCount, record.ScoreTime, mapId) | 194 | // _, err := tx.Exec(`UPDATE maps SET wr_score = $1, wr_time = $2 WHERE id = $3`, record.ScoreCount, record.ScoreTime, mapId) |
| 195 | // if err != nil { | 195 | // if err != nil { |
| 196 | // c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 196 | // c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 197 | // return | 197 | // return |
| 198 | // } | 198 | // } |
| 199 | // } | 199 | // } |
| 200 | } | 200 | } |
| 201 | if err = tx.Commit(); err != nil { | 201 | if err = tx.Commit(); err != nil { |
| 202 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | 202 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 203 | return | 203 | return |
| 204 | } | 204 | } |
| 205 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordSuccess) | 205 | CreateLog(user.(models.User).SteamID, LogTypeRecord, LogDescriptionRecordSuccess) |
| @@ -224,36 +224,36 @@ func DownloadDemoWithID(c *gin.Context) { | |||
| 224 | uuid := c.Query("uuid") | 224 | uuid := c.Query("uuid") |
| 225 | var locationID string | 225 | var locationID string |
| 226 | if uuid == "" { | 226 | if uuid == "" { |
| 227 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Invalid id given.")) | 227 | c.JSON(http.StatusOK, models.ErrorResponse("Invalid id given.")) |
| 228 | return | 228 | return |
| 229 | } | 229 | } |
| 230 | err := database.DB.QueryRow(`SELECT location_id FROM demos WHERE id = $1`, uuid).Scan(&locationID) | 230 | err := database.DB.QueryRow(`SELECT location_id FROM demos WHERE id = $1`, uuid).Scan(&locationID) |
| 231 | if err != nil { | 231 | if err != nil { |
| 232 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 232 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 233 | return | 233 | return |
| 234 | } | 234 | } |
| 235 | if locationID == "" { | 235 | if locationID == "" { |
| 236 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Invalid id given.")) | 236 | c.JSON(http.StatusOK, models.ErrorResponse("Invalid id given.")) |
| 237 | return | 237 | return |
| 238 | } | 238 | } |
| 239 | url := "https://drive.google.com/uc?export=download&id=" + locationID | 239 | url := "https://drive.google.com/uc?export=download&id=" + locationID |
| 240 | fileName := uuid + ".dem" | 240 | fileName := uuid + ".dem" |
| 241 | output, err := os.Create(fileName) | 241 | output, err := os.Create(fileName) |
| 242 | if err != nil { | 242 | if err != nil { |
| 243 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 243 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 244 | return | 244 | return |
| 245 | } | 245 | } |
| 246 | defer os.Remove(fileName) | 246 | defer os.Remove(fileName) |
| 247 | defer output.Close() | 247 | defer output.Close() |
| 248 | response, err := http.Get(url) | 248 | response, err := http.Get(url) |
| 249 | if err != nil { | 249 | if err != nil { |
| 250 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 250 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 251 | return | 251 | return |
| 252 | } | 252 | } |
| 253 | defer response.Body.Close() | 253 | defer response.Body.Close() |
| 254 | _, err = io.Copy(output, response.Body) | 254 | _, err = io.Copy(output, response.Body) |
| 255 | if err != nil { | 255 | if err != nil { |
| 256 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 256 | c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) |
| 257 | return | 257 | return |
| 258 | } | 258 | } |
| 259 | // Downloaded file | 259 | // Downloaded file |