From 39c4d8f13470d31b34297c46c85a0d0d7b14697f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Rynning-T=C3=B8nnesen?= Date: Mon, 6 Apr 2020 09:36:33 +0200 Subject: [PATCH] Generalized some functions --- api/person.js | 35 +++++++++++++++++++++++++++++++++++ api/wine.js | 27 +++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 api/person.js create mode 100644 api/wine.js diff --git a/api/person.js b/api/person.js new file mode 100644 index 0000000..bfce82d --- /dev/null +++ b/api/person.js @@ -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; diff --git a/api/wine.js b/api/wine.js new file mode 100644 index 0000000..e844867 --- /dev/null +++ b/api/wine.js @@ -0,0 +1,27 @@ +const path = require("path"); +const Wine = require(path.join(__dirname + "/../schemas/Wine")); + +async function findSaveWine(prelotteryWine) { + let wonWine = await Wine.findOne({ name: prelotteryWine.name }); + if (wonWine == undefined) { + let newWonWine = new Wine({ + name: prelotteryWine.name, + vivinoLink: prelotteryWine.vivinoLink, + rating: prelotteryWine.rating, + occurences: 1, + image: prelotteryWine.image, + id: prelotteryWine.id + }); + await newWonWine.save(); + wonWine = newWonWine; + } else { + wonWine.occurences += 1; + wonWine.image = prelotteryWine.image; + wonWine.id = prelotteryWine.id; + await wonWine.save(); + } + + return wonWine; +} + +module.exports.findSaveWine = findSaveWine;