diff --git a/src/server/.gitignore b/src/server/.gitignore index 1cd3789..c6bba59 100644 --- a/src/server/.gitignore +++ b/src/server/.gitignore @@ -1,7 +1,3 @@ -# Database credentials -sql_creds.txt -config/config.json - # Logs logs *.log diff --git a/src/server/README.md b/src/server/README.md new file mode 100644 index 0000000..0f340d5 --- /dev/null +++ b/src/server/README.md @@ -0,0 +1,14 @@ +## Server installation +* Clone the repository +** Checkout the "server" branch if not merged +* Enter the server directory: `cd src/server` +* Install the node dependencies: `npm install` +* Create the file `.env` containing your database login: +``` +DB_HOST=mysql.stud.ntnu.no +DB_USER=dbusername +DB_PASSWORD=dbpassword +DB_DATABASE=dbnamei +``` +* Build the client (separate instructions) +* Start the server `npm start` diff --git a/src/server/index.js b/src/server/index.js index 9aa7b45..582a29b 100644 --- a/src/server/index.js +++ b/src/server/index.js @@ -23,42 +23,60 @@ app.get("/", (req, res) => { res.sendFile(path.join(__dirname, "public", "landing.html")); }); -app.get("/getMatches", (req, res) => { - connection.query("SELECT * FROM matches", (err, matches) => { - if (err) { - console.log(err); - } else { - res.send(matches); - } +app.get("/tournament/:tournamentId/getMatches", (req, res) => { + let tournamentId = req.params.tournamentId; + getMatchesByTournamentId(tournamentId) + .then((result) => res.send({"status": "OK", "data": result})) + .catch((err) => res.send({"status": "ERROR", "data": err})); +}); + +// app.get("/getMatches", (req, res) => { +// connection.query("SELECT * FROM matches", (err, matches) => { +// if (err) { +// console.log(err); +// } else { +// res.send(matches); +// } +// }); +// }); + + +// let tournaments = { +// "1": { +// "name": "Tournament 1", +// "description": "This is the first tournament", +// "matches":[ +// {"id": "2", +// "player1": "Player 1", +// "player2": "Player 2", +// "winner": "Player 1", +// } +// ] +// }, +// "2": { +// "name": "Tournament 2", +// "description": "This is the second tournament", +// "matches":[ +// {"id": "2", +// "player1": "Player 1", +// "player2": "Player 2", +// "winner": "Player 1", +// }] +// } +// }; +// app.get("/tournament/:tournamentId", (req, res) => { +// res.render(path.join(__dirname, "public", "tournament.html"), {"tournament":tournaments[req.params.tournamentId]}); +// }); + +function getMatchesByTournamentId(tournamentId) { + return new Promise(function(resolve, reject) { + connection.query("SELECT * FROM matches WHERE tournament_id = ?", [mysql.escape(tournamentId)], (err, matches) => { + if (err) { + console.log(err); + reject(err); + } else { + resolve(matches); + } + }); }); -}); - - -let tournaments = { - "1": { - "name": "Tournament 1", - "description": "This is the first tournament", - "matches":[ - {"id": "2", - "player1": "Player 1", - "player2": "Player 2", - "winner": "Player 1", - } - ] - }, - "2": { - "name": "Tournament 2", - "description": "This is the second tournament", - "matches":[ - {"id": "2", - "player1": "Player 1", - "player2": "Player 2", - "winner": "Player 1", - }] - } -}; -app.get("/tournament/:tournamentId", (req, res) => { - res.render(path.join(__dirname, "public", "tournament.html"), {"tournament":tournaments[req.params.tournamentId]}); -}); - - +}