From 35ed2b617401b88606dee754ba6fd08ce46dbb57 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tobias=20=C3=98rstad?= <tobiasio@ntnu.no>
Date: Thu, 5 Mar 2020 15:49:07 +0100
Subject: [PATCH] Progress on creating new tournament. Issue #7

---
 backend/api/tournament.js | 35 +++++++++++++++++++++++++++--------
 backend/index.js          |  2 +-
 2 files changed, 28 insertions(+), 9 deletions(-)

diff --git a/backend/api/tournament.js b/backend/api/tournament.js
index a4122e1..0183a44 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("/new", (req, res) => {
   MongoClient.connect(
     connectionUrl,
     { useNewUrlParser: true, useUnifiedTopology: true },
@@ -19,19 +19,20 @@ router.post("/new/:host/:games/:name/:tpr/:players/:rpg", (req, res) => {
 
       const db = client.db("gameWare");
       const collection = "tournaments";
-      const tournamentid;
+      let tournamentid;
+      const tournamentdate = new Date();
 
       db.collection(collection).insertOne(
         {
           gameid: req.body.gameid,
           players: [req.body.userid],
-          games: "todo",
+          games: req.body.games,
           name: req.body.name,
           timePerRound: parseInt(req.body.timePerRound),
           maxPlayers: parseInt(req.body.maxPlayers),
           roundsPerGame: parseInt(req.body.roundsPerGame),
           currentRound: 1,
-          dateCreated: new Date()
+          dateCreated: tournamentdate
         },
         (err, result) => {
           if (err) {
@@ -41,11 +42,29 @@ router.post("/new/:host/:games/:name/:tpr/:players/:rpg", (req, res) => {
           tournamentid = result._id;
         }
       );
+      tournamentdate.setTime(
+        tournamentdate.getTime() + req.body.timePerRound * 60 * 60 * 1000
+      );
       db.collection("rounds").insertOne(
         {
-          
+          tournamentId: tournamentid,
+          playerId: req.body.userid,
+          gameId: req.body.game.get(0),
+          scoreValue: 0,
+          roundNr: 1,
+          hasPlayed: false,
+          deadlineDate: tournamentdate
+        },
+        err => {
+          if (err) {
+            res.sendStatus(500); // Internal server error
+            return;
+          }
+          res.json(result);
         }
-      )
+      );
     }
   );
-});*/
+});
+
+module.exports = router;
diff --git a/backend/index.js b/backend/index.js
index 428c61d..d2f10c9 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