Improved database interface

This commit is contained in:
Felix Albrigtsen 2022-03-16 15:40:33 +01:00
parent c92ecba883
commit 7eb631fd21
3 changed files with 70 additions and 42 deletions

View File

@ -1,7 +1,3 @@
# Database credentials
sql_creds.txt
config/config.json
# Logs # Logs
logs logs
*.log *.log

14
src/server/README.md Normal file
View File

@ -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`

View File

@ -23,42 +23,60 @@ app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "landing.html")); res.sendFile(path.join(__dirname, "public", "landing.html"));
}); });
app.get("/getMatches", (req, res) => { app.get("/tournament/:tournamentId/getMatches", (req, res) => {
connection.query("SELECT * FROM matches", (err, matches) => { 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) { if (err) {
console.log(err); console.log(err);
reject(err);
} else { } else {
res.send(matches); 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]});
});