Generalized some functions
This commit is contained in:
35
api/person.js
Normal file
35
api/person.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const path = require("path");
|
||||
const Highscore = require(path.join(__dirname + "/../schemas/Highscore"));
|
||||
|
||||
async function findSavePerson(foundWinner, wonWine) {
|
||||
let person = await Highscore.findOne({
|
||||
name: foundWinner.name
|
||||
});
|
||||
|
||||
if (person == undefined) {
|
||||
let newPerson = new Highscore({
|
||||
name: foundWinner.name,
|
||||
wins: [
|
||||
{
|
||||
color: foundWinner.color,
|
||||
date: date,
|
||||
wine: wonWine
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
await newPerson.save();
|
||||
} else {
|
||||
person.wins.push({
|
||||
color: foundWinner.color,
|
||||
date: date,
|
||||
wine: wonWine
|
||||
});
|
||||
person.markModified("wins");
|
||||
await person.save();
|
||||
}
|
||||
|
||||
return person;
|
||||
}
|
||||
|
||||
module.exports.findSavePerson = findSavePerson;
|
Reference in New Issue
Block a user