aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-04-24 18:34:36 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-04-24 18:34:36 +0300
commitbf3eb586ff7864957e85a067fa825b8a4fff3f71 (patch)
treeb8a16bc54a2ef871004d0dba83e42eedf3928717 /backend
parentfix: adjust record submission (#38) (diff)
downloadlphub-bf3eb586ff7864957e85a067fa825b8a4fff3f71.tar.gz
lphub-bf3eb586ff7864957e85a067fa825b8a4fff3f71.tar.bz2
lphub-bf3eb586ff7864957e85a067fa825b8a4fff3f71.zip
feat: db transaction for record submission (#33)
Diffstat (limited to 'backend')
-rw-r--r--backend/controllers/recordController.go22
1 files changed, 17 insertions, 5 deletions
diff --git a/backend/controllers/recordController.go b/backend/controllers/recordController.go
index cfd3e86..bafa844 100644
--- a/backend/controllers/recordController.go
+++ b/backend/controllers/recordController.go
@@ -107,6 +107,14 @@ func CreateRecordWithDemo(c *gin.Context) {
107 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 107 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
108 return 108 return
109 } 109 }
110 // Create database transaction for inserts
111 tx, err := database.DB.Begin()
112 if err != nil {
113 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
114 return
115 }
116 // Defer to a rollback in case anything fails
117 defer tx.Rollback()
110 fileID := "" 118 fileID := ""
111 for i, header := range files { 119 for i, header := range files {
112 uuid := uuid.New().String() 120 uuid := uuid.New().String()
@@ -134,7 +142,7 @@ func CreateRecordWithDemo(c *gin.Context) {
134 if i == 1 { 142 if i == 1 {
135 partnerDemoUUID = uuid 143 partnerDemoUUID = uuid
136 } 144 }
137 _, err = database.DB.Exec(`INSERT INTO demos (id,location_id) VALUES ($1,$2)`, uuid, file.Id) 145 _, err = tx.Exec(`INSERT INTO demos (id,location_id) VALUES ($1,$2)`, uuid, file.Id)
138 if err != nil { 146 if err != nil {
139 deleteFile(srv, file.Id) 147 deleteFile(srv, file.Id)
140 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 148 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
@@ -155,7 +163,7 @@ func CreateRecordWithDemo(c *gin.Context) {
155 partnerID = user.(models.User).SteamID 163 partnerID = user.(models.User).SteamID
156 hostID = record.PartnerID 164 hostID = record.PartnerID
157 } 165 }
158 _, err := database.DB.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, hostID, partnerID, hostDemoUUID, partnerDemoUUID) 166 _, err := tx.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, hostID, partnerID, hostDemoUUID, partnerDemoUUID)
159 if err != nil { 167 if err != nil {
160 deleteFile(srv, fileID) 168 deleteFile(srv, fileID)
161 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 169 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
@@ -163,7 +171,7 @@ func CreateRecordWithDemo(c *gin.Context) {
163 } 171 }
164 // If a new world record based on portal count 172 // If a new world record based on portal count
165 // if record.ScoreCount < wrScore { 173 // if record.ScoreCount < wrScore {
166 // _, err := database.DB.Exec(`UPDATE maps SET wr_score = $1, wr_time = $2 WHERE id = $3;`, record.ScoreCount, record.ScoreTime, mapId) 174 // _, err := tx.Exec(`UPDATE maps SET wr_score = $1, wr_time = $2 WHERE id = $3;`, record.ScoreCount, record.ScoreTime, mapId)
167 // if err != nil { 175 // if err != nil {
168 // c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 176 // c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
169 // return 177 // return
@@ -172,7 +180,7 @@ func CreateRecordWithDemo(c *gin.Context) {
172 } else { 180 } else {
173 sql := `INSERT INTO records_sp(map_id,score_count,score_time,user_id,demo_id) 181 sql := `INSERT INTO records_sp(map_id,score_count,score_time,user_id,demo_id)
174 VALUES($1, $2, $3, $4, $5);` 182 VALUES($1, $2, $3, $4, $5);`
175 _, err := database.DB.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, user.(models.User).SteamID, hostDemoUUID) 183 _, err := tx.Exec(sql, mapId, record.ScoreCount, record.ScoreTime, user.(models.User).SteamID, hostDemoUUID)
176 if err != nil { 184 if err != nil {
177 deleteFile(srv, fileID) 185 deleteFile(srv, fileID)
178 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 186 c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
@@ -180,13 +188,17 @@ func CreateRecordWithDemo(c *gin.Context) {
180 } 188 }
181 // If a new world record based on portal count 189 // If a new world record based on portal count
182 // if record.ScoreCount < wrScore { 190 // if record.ScoreCount < wrScore {
183 // _, err := database.DB.Exec(`UPDATE maps SET wr_score = $1, wr_time = $2 WHERE id = $3;`, record.ScoreCount, record.ScoreTime, mapId) 191 // _, err := tx.Exec(`UPDATE maps SET wr_score = $1, wr_time = $2 WHERE id = $3;`, record.ScoreCount, record.ScoreTime, mapId)
184 // if err != nil { 192 // if err != nil {
185 // c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) 193 // c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error()))
186 // return 194 // return
187 // } 195 // }
188 // } 196 // }
189 } 197 }
198 if err = tx.Commit(); err != nil {
199 c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error()))
200 return
201 }
190 c.JSON(http.StatusOK, models.Response{ 202 c.JSON(http.StatusOK, models.Response{
191 Success: true, 203 Success: true,
192 Message: "Successfully created record.", 204 Message: "Successfully created record.",