From 9f58f902b8977cf3ce2c94b73b3dd54d0b82085a Mon Sep 17 00:00:00 2001 From: Felix Albrigtsen Date: Sat, 23 Apr 2022 11:46:39 +0200 Subject: [PATCH] Added unsetContestantAndWinner --- src/server/index.js | 19 +++++++++++++++++++ src/server/tmdb.js | 24 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/server/index.js b/src/server/index.js index 96e35e1..9edcb27 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -282,6 +282,25 @@ api.post("/match/:matchId/setWinner", (req, res) => { .then(match => res.send({"status": "OK", "data": match})) .catch(err => res.send({"status": "error", "data": err})); }); + +api.post("/match/:matchId/unsetContestant", (req, res) => { + let matchId = req.params.matchId; + let contestantId = req.body.teamId; + if (isNaN(matchId)) { + res.json({"status": "error", "data": "matchId must be a number"}); + return + } + if (contestantId == undefined || isNaN(contestantId)) { + res.json({"status": "error", "data": "contestantId must be a number"}); + return + } + + matchId = parseInt(matchId); + contestantId = parseInt(contestantId); + tmdb.unsetContestantAndWinner(matchId, contestantId) + .then(match => res.send({"status": "OK", "data": match})) + .catch(err => res.send({"status": "error", "data": err})); +}); // #endregion // #region team/:teamId diff --git a/src/server/tmdb.js b/src/server/tmdb.js index 885ee94..314db47 100644 --- a/src/server/tmdb.js +++ b/src/server/tmdb.js @@ -11,6 +11,7 @@ module.exports = { deleteTeam: deleteTeam, getMatch: getMatch, setMatchWinner: setMatchWinner, + unsetContestantAndWinner: unsetContestantAndWinner, createTournament: createTournament, deleteTournament: deleteTournament, editTournament: editTournament, @@ -161,6 +162,29 @@ async function setMatchWinner(matchId, winnerId) { }); } +async function unsetContestantAndWinner(matchId, teamId) { + let match = await getMatch(matchId); + return new Promise(function(resolve, reject) { + unsetContestant(matchId, teamId) + .then(() => { + // Find what the child match that supplied the team + connection.query("SELECT * FROM matches WHERE parentMatchId = ? AND winnerId = ?", [matchId, teamId], (err, childMatches) => { + if (err) { console.log(err); reject(err); } + if (childMatches.length != 1) { + reject("Error: Could not find the correct child match"); + return; + } + let childMatch = childMatches[0]; + // Remove the winner from the child match + setMatchWinner(childMatch.id, null) + .then(() => { resolve(); }) + .catch(err => { reject(err); }); + }); + }) + .catch(err => { reject(err); }); + }); +} + // #endregion // #region tournament