From 1931074cc2890bc554f39a8ecb25110804559cd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Sun, 7 Nov 2021 01:21:18 +0100 Subject: [PATCH] mutation: Improve match logic It is no longer possible to cancel or finish a match that is not ongoing. --- graph/schema.resolvers.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/graph/schema.resolvers.go b/graph/schema.resolvers.go index 5c85ae2..637f13f 100644 --- a/graph/schema.resolvers.go +++ b/graph/schema.resolvers.go @@ -243,12 +243,21 @@ func (r *mutationResolver) CancelMatch(ctx context.Context, id int) (string, err query := ` UPDATE "Match" SET status = 'cancelled' - WHERE id = $1` - _, err := database.DB.Exec(query, id) + WHERE id = $1 AND status = 'ongoing'` + result, err := database.DB.Exec(query, id) + if err != nil { + return "CancelFail", err + } + + count, err := result.RowsAffected() if err != nil { return "CancelFail", err } + if count != 1 { + return "CancelFail", errors.New("match is not ongoing") + } + return "CancelSuccess", nil } @@ -258,10 +267,13 @@ func (r *mutationResolver) FinishMatch(ctx context.Context, id int, winner strin query := ` SELECT t1, t2, elo1, elo2 FROM "Match" - WHERE id = $1` + WHERE id = $1 and status = 'ongoing'` err := database.DB.QueryRow(query, id). Scan(&team1.ID, &team2.ID, &team1.Elo, &team2.Elo) if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return "FinishFail", errors.New("match is not ongoing") + } return "FinishFail", err } -- 2.44.0