From 8faf56b62afd16ada55276cf975ca92ed20176d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20=C3=98rstad?= <tobiasio@ntnu.no> Date: Mon, 9 Mar 2020 11:58:20 +0100 Subject: [PATCH] Allow fetching a users active tournaments. Issue #4 --- backend/api/tournament.js | 48 ++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 28 deletions(-) diff --git a/backend/api/tournament.js b/backend/api/tournament.js index a4122e1..9e1837d 100644 --- a/backend/api/tournament.js +++ b/backend/api/tournament.js @@ -3,8 +3,9 @@ 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.get("/playeractive/:userid", (req, res) => { + // Connect to database MongoClient.connect( connectionUrl, { useNewUrlParser: true, useUnifiedTopology: true }, @@ -15,37 +16,28 @@ router.post("/new/:host/:games/:name/:tpr/:players/:rpg", (req, res) => { return; } - const games = req.params.games.split("&"); - + // Using the database gameWare and collection tournaments 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) => { + const id = mongo.ObjectId(req.params.userid); + + db.collection(collection) + .find({ + _id: id, + active: true + }) + .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); + }); } ); -});*/ +}); + +// Export API routes +module.exports = router; -- GitLab