Skip to content
Snippets Groups Projects
Commit 82c6e106 authored by Ivar Nordvik Myrstad's avatar Ivar Nordvik Myrstad
Browse files

Merge branch '54-include-name-in-get-highscore' into 'dev'

Resolve "Include name in get highscore"

Closes #54

See merge request !51
parents 12cf90bf a2475b02
No related branches found
No related tags found
1 merge request!51Resolve "Include name in get highscore"
...@@ -45,9 +45,11 @@ router.get("/gamescore/:playerId/:gameId", (req, res) => { ...@@ -45,9 +45,11 @@ router.get("/gamescore/:playerId/:gameId", (req, res) => {
//console.log(err); //console.log(err);
return; return;
} }
res.json(result); addNameToResult(result, db).then(objects => {
res.json(objects);
client.close(); client.close();
}); });
});
} }
); );
}); });
...@@ -193,6 +195,7 @@ router.get("/gamescores/:gameId", (req, res) => { ...@@ -193,6 +195,7 @@ router.get("/gamescores/:gameId", (req, res) => {
if (gameId == null) { if (gameId == null) {
res.status(400).send("Fields missing"); res.status(400).send("Fields missing");
return;
} }
db.collection(collection) db.collection(collection)
...@@ -208,12 +211,49 @@ router.get("/gamescores/:gameId", (req, res) => { ...@@ -208,12 +211,49 @@ router.get("/gamescores/:gameId", (req, res) => {
//console.log(err); //console.log(err);
return; return;
} }
res.json(result); addNameToResult(result, db).then(objects => {
res.json(objects);
client.close(); client.close();
}); });
});
} }
); );
}); });
function addNameToResult(result, db) {
return new Promise((resolve, reject) => {
const playerIds = result.map(o => o.playerId);
db.collection("players")
.find({
_id: { $in: playerIds }
})
.project({ name: 1 })
.toArray((err, nameresult) => {
if (err) {
res.sendStatus(500);
//console.log(err);
return;
}
nameresult = nameresult.map(doc => {
// rename _id => playerId
doc.playerId = doc._id;
delete doc._id;
return doc;
});
let objects = [];
for (let i = 0; i < result.length; i++) {
objects.push({
...result[i],
...nameresult.find(
itmInner =>
String(itmInner.playerId) === String(result[i].playerId)
)
});
}
resolve(objects);
});
});
}
// Export API routes // Export API routes
module.exports = router; module.exports = router;
...@@ -6,15 +6,17 @@ public class Highscore { ...@@ -6,15 +6,17 @@ public class Highscore {
private String _id; private String _id;
private String gameId; private String gameId;
private String playerId; private String playerId;
private String name;
private double value; private double value;
public Highscore() { public Highscore() {
} }
public Highscore(String _id, String gameId, String userId, double value) { public Highscore(String _id, String gameId, String userId, String name, double value) {
this._id = _id; this._id = _id;
this.gameId = gameId; this.gameId = gameId;
this.playerId = userId; this.playerId = userId;
this.name = name;
this.value = value; this.value = value;
} }
...@@ -30,6 +32,10 @@ public class Highscore { ...@@ -30,6 +32,10 @@ public class Highscore {
return playerId; return playerId;
} }
public String getName() {
return name;
}
public double getValue() { public double getValue() {
return value; return value;
} }
...@@ -38,6 +44,7 @@ public class Highscore { ...@@ -38,6 +44,7 @@ public class Highscore {
_id = null; _id = null;
gameId = null; gameId = null;
playerId = null; playerId = null;
name = null;
value = Double.parseDouble(null); value = Double.parseDouble(null);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment