Skip to content

Commit 00559ab

Browse files
author
Thord Setsaas
committed
Initial work on undoleg
1 parent 721b906 commit 00559ab

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

controllers/leg_controller.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package controllers
22

33
import (
44
"encoding/json"
5+
"io"
56
"log"
67
"net/http"
78
"strconv"
@@ -315,7 +316,20 @@ func DeleteLeg(w http.ResponseWriter, r *http.Request) {
315316
return
316317
}
317318

318-
err = data.DeleteLeg(legID)
319+
body := struct {
320+
Abandoned *bool `json:"abandoned"`
321+
}{}
322+
abandoned := true
323+
err = json.NewDecoder(r.Body).Decode(&body)
324+
if err != nil && err != io.EOF {
325+
log.Println("Unable to deserialize delete leg body", err)
326+
http.Error(w, err.Error(), http.StatusBadRequest)
327+
return
328+
}
329+
if body.Abandoned != nil {
330+
abandoned = *body.Abandoned
331+
}
332+
err = data.DeleteLeg(legID, abandoned)
319333
if err != nil {
320334
log.Println("Unable to delete leg", err)
321335
http.Error(w, err.Error(), http.StatusInternalServerError)

data/leg.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,7 +1313,7 @@ func StartWarmup(legID int, venueID int) error {
13131313
}
13141314

13151315
// DeleteLeg will delete the current leg and update match with previous leg
1316-
func DeleteLeg(legID int) error {
1316+
func DeleteLeg(legID int, abandoned bool) error {
13171317
leg, err := GetLeg(legID)
13181318
if err != nil {
13191319
return err
@@ -1341,8 +1341,11 @@ func DeleteLeg(legID int) error {
13411341
}
13421342
log.Printf("Delete match without any leg %d", match.ID)
13431343
} else {
1344-
_, err = tx.Exec("UPDATE matches SET current_leg_id = ?, is_abandoned = 1, is_finished = 1 WHERE id = ?", previousLeg, match.ID)
1345-
if err != nil {
1344+
query := "UPDATE matches SET current_leg_id = ? WHERE id = ?"
1345+
if abandoned {
1346+
query = "UPDATE matches SET current_leg_id = ?, is_abandoned = 1, is_finished = 1 WHERE id = ?"
1347+
}
1348+
if _, err = tx.Exec(query, previousLeg, match.ID); err != nil {
13461349
return err
13471350
}
13481351
log.Printf("[%d] Updated current leg of match %d", previousLeg, match.ID)

0 commit comments

Comments
 (0)