Skip to content
Snippets Groups Projects

dev into fetch-tournament.

Merged Tobias Ingebrigt Ørstad requested to merge dev into 4-fetch-tournament
3 files
+ 300
2
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 217
0
 
const express = require("express");
 
const router = express.Router();
 
const mongo = require("mongodb");
 
const MongoClient = mongo.MongoClient;
 
const connectionUrl = process.env.MONGO_CONNECTION_STRING;
 
 
router.get("/:roundid", (req, res) => {
 
// Connect to database
 
MongoClient.connect(
 
connectionUrl,
 
{ useNewUrlParser: true, useUnifiedTopology: true },
 
(err, client) => {
 
// Unable to connect to database
 
if (err) {
 
res.sendStatus(500); // Internal server error
 
return;
 
}
 
 
// Using the database gameWare and collection rounds
 
const db = client.db("gameWare");
 
const collection = "rounds";
 
 
let id = undefined;
 
try {
 
id = mongo.ObjectID(req.params.roundid); // get roundId
 
} catch (error) {
 
res.status(404).send("No document with specified id was found");
 
return;
 
}
 
 
db.collection(collection)
 
.findOne({
 
_id: id
 
})
 
.then(result => {
 
if (!result) {
 
res.status(404).send("No document with specified id found");
 
return;
 
}
 
res.json(result);
 
})
 
.catch(err => console.log(err));
 
}
 
);
 
});
 
 
router.get("/:tournamentId/:playerId/:roundNr", (req, res) => {
 
// Connect to database
 
MongoClient.connect(
 
connectionUrl,
 
{ useNewUrlParser: true, useUnifiedTopology: true },
 
(err, client) => {
 
// Unable to connect to database
 
if (err) {
 
res.sendStatus(500); // Internal server error
 
return;
 
}
 
// Using the database gameWare and collection rounds
 
const db = client.db("gameWare");
 
const collection = "rounds";
 
 
let tournamentId,
 
playerId,
 
roundNr = undefined;
 
try {
 
tournamentId = mongo.ObjectID(req.params.tournamentId); // get tournamentId
 
playerId = mongo.ObjectID(req.params.playerId); // get playerId
 
roundNr = parseInt(req.params.roundNr); // get roundNr
 
} catch (error) {
 
res.status(404).send("No document with specified params found");
 
return;
 
}
 
 
db.collection(collection)
 
.findOne({
 
tournamentId,
 
playerId,
 
roundNr
 
})
 
.then(result => {
 
if (!result) {
 
res.status(404).send("No document with specified id params found");
 
return;
 
}
 
res.json(result);
 
})
 
.catch(err => console.log(err));
 
}
 
);
 
});
 
 
router.post("/", (req, res) => {
 
// Connect to database
 
MongoClient.connect(
 
connectionUrl,
 
{ useNewUrlParser: true, useUnifiedTopology: true },
 
(err, client) => {
 
// Unable to connect to database
 
if (err) {
 
res.sendStatus(500); // Internal server error
 
return;
 
}
 
 
// Using the database gameWare and collection rounds
 
const db = client.db("gameWare");
 
const collection = "rounds";
 
 
const values = {
 
tournamentId: null,
 
playerId: null,
 
gameId: null,
 
roundNr: null,
 
deadlineDate: null
 
};
 
 
try {
 
values.tournamentId = mongo.ObjectID(req.body.tournamentId);
 
values.playerId = mongo.ObjectID(req.body.playerId);
 
values.gameId = mongo.ObjectID(req.body.gameId);
 
values.roundNr = !isNaN(parseInt(req.body.roundNr))
 
? parseInt(req.body.roundNr)
 
: null;
 
values.deadlineDate = getDate(req.body.deadlineDate);
 
} catch (error) {
 
res.status(404).send("No document with specified id was found");
 
console.log(error);
 
return;
 
}
 
 
// validating body
 
Object.keys(values).forEach(val => {
 
if (!req.body[val]) {
 
values[val] = null;
 
}
 
});
 
// values contains a null field
 
if (Object.values(values).some(x => x === null)) {
 
res.status(404).send("Missing fields in request");
 
return;
 
}
 
 
values.scoreValue = 0;
 
values.hasPlayed = false;
 
 
db.collection(collection).insertOne(values, (err, result) => {
 
if (err) {
 
res.sendStatus(500); // Internal server error
 
return;
 
}
 
res.json(result.ops[0]);
 
});
 
}
 
);
 
});
 
 
router.put("/:roundid", (req, res) => {
 
// Connect to database
 
MongoClient.connect(
 
connectionUrl,
 
{ useNewUrlParser: true, useUnifiedTopology: true },
 
(err, client) => {
 
// Unable to connect to database
 
if (err) {
 
res.sendStatus(500); // Internal server error
 
return;
 
}
 
 
// Using the database gameWare and collection rounds
 
const db = client.db("gameWare");
 
const collection = "rounds";
 
 
let id,
 
scoreValue = null;
 
try {
 
id = mongo.ObjectID(req.params.roundid); // get roundId
 
scoreValue = !isNaN(parseInt(req.body.scoreValue))
 
? parseInt(req.body.scoreValue)
 
: null;
 
} catch (error) {
 
res.status(400).send("No document with specified id was found");
 
return;
 
}
 
if (scoreValue === null) {
 
res.status(400).send("Request body not valid");
 
return;
 
}
 
 
db.collection(collection)
 
.findOneAndUpdate(
 
{ _id: id },
 
{ $set: { scoreValue: scoreValue } },
 
{ returnOriginal: false }
 
)
 
.then(updatedDocument => {
 
if (updatedDocument.value) {
 
//console.log("Successfully updated document");
 
res.json(updatedDocument.value); // Return updated document
 
} else {
 
//console.log("Failed to update doucment");
 
res.status(404).send("No document with specified id was found");
 
}
 
})
 
.catch(err => console.log(err));
 
}
 
);
 
});
 
 
// Export API routes
 
module.exports = router;
 
 
function getDate(dateString) {
 
date = new Date(dateString);
 
if (isNaN(date.getTime())) {
 
return null;
 
}
 
return date;
 
}
Loading