From f90e1cb0cf3995226eef1bae606b7ab5220ee6d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20=C3=98rstad?= <tobiasio@ntnu.no> Date: Mon, 9 Mar 2020 13:19:24 +0100 Subject: [PATCH] Update method for joining tournaments. Issue #28 --- backend/api/tournament.js | 44 ++++++++++++++------------------------- backend/index.js | 2 +- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/backend/api/tournament.js b/backend/api/tournament.js index a4122e1..d1ecf57 100644 --- a/backend/api/tournament.js +++ b/backend/api/tournament.js @@ -3,8 +3,8 @@ const router = express.Router(); const mongo = require("mongodb"); const MongoClient = mongo.MongoClient; const connectionUrl = process.env.MONGO_CONNECTION_STRING; -/* -router.post("/new/:host/:games/:name/:tpr/:players/:rpg", (req, res) => { + +router.post("/join", (req, res) => { MongoClient.connect( connectionUrl, { useNewUrlParser: true, useUnifiedTopology: true }, @@ -15,37 +15,25 @@ router.post("/new/:host/:games/:name/:tpr/:players/:rpg", (req, res) => { return; } - const games = req.params.games.split("&"); - const db = client.db("gameWare"); const collection = "tournaments"; - const tournamentid; - db.collection(collection).insertOne( - { - gameid: req.body.gameid, - players: [req.body.userid], - games: "todo", - name: req.body.name, - timePerRound: parseInt(req.body.timePerRound), - maxPlayers: parseInt(req.body.maxPlayers), - roundsPerGame: parseInt(req.body.roundsPerGame), - currentRound: 1, - dateCreated: new Date() - }, - (err, result) => { + db.collection(collection) + .find({ + $expr: { $lt: ["$maxPlayers", "$players.length"] } + }) + .toArray((err, result) => { if (err) { - res.sendStatus(500); // Internal server error + res.sendStatus(500); + console.log(err); return; } - tournamentid = result._id; - } - ); - db.collection("rounds").insertOne( - { - - } - ) + res.json(result); + }); + db.collection("rounds").insertOne({}); } ); -});*/ +}); + +// Export API routes +module.exports = router; diff --git a/backend/index.js b/backend/index.js index 9bfedb3..4c3115e 100644 --- a/backend/index.js +++ b/backend/index.js @@ -13,7 +13,7 @@ app.disable("x-powered-by"); // Minium security app.use("/api/games", require("./api/games")); // Use games.js for route /api/games app.use("/api/highscores", require("./api/highscores")); app.use("/api/players", require("./api/players")); -//app.use("/api/tournament", require("./api/tournament")); +app.use("/api/tournament", require("./api/tournament")); // Default route app.get("/api", (req, res) => { -- GitLab