Renamed lottery -> history.
This commit is contained in:
@@ -1,106 +1,109 @@
|
|||||||
const path = require('path');
|
const path = require("path");
|
||||||
|
|
||||||
const Highscore = require(path.join(__dirname, '/schemas/Highscore'));
|
const Highscore = require(path.join(__dirname, "/schemas/Highscore"));
|
||||||
const Wine = require(path.join(__dirname, '/schemas/Wine'));
|
const Wine = require(path.join(__dirname, "/schemas/Wine"));
|
||||||
|
|
||||||
// Utils
|
// Utils
|
||||||
const epochToDateString = date => new Date(parseInt(date)).toDateString();
|
const epochToDateString = date => new Date(parseInt(date)).toDateString();
|
||||||
|
|
||||||
const sortNewestFirst = (lotteries) => {
|
const sortNewestFirst = lotteries => {
|
||||||
return lotteries.sort((a, b) => parseInt(a.date) < parseInt(b.date) ? 1 : -1)
|
return lotteries.sort((a, b) => (parseInt(a.date) < parseInt(b.date) ? 1 : -1));
|
||||||
}
|
};
|
||||||
|
|
||||||
const groupHighscoreByDate = async (highscore=undefined) => {
|
const groupHighscoreByDate = async (highscore = undefined) => {
|
||||||
if (highscore == undefined)
|
if (highscore == undefined) highscore = await Highscore.find();
|
||||||
highscore = await Highscore.find();
|
|
||||||
|
|
||||||
const highscoreByDate = [];
|
const highscoreByDate = [];
|
||||||
|
|
||||||
highscore.forEach(person => {
|
highscore.forEach(person => {
|
||||||
person.wins.map(win => {
|
person.wins.map(win => {
|
||||||
const epochDate = new Date(win.date).setHours(0,0,0,0);
|
const epochDate = new Date(win.date).setHours(0, 0, 0, 0);
|
||||||
const winnerObject = {
|
const winnerObject = {
|
||||||
name: person.name,
|
name: person.name,
|
||||||
color: win.color,
|
color: win.color,
|
||||||
wine: win.wine,
|
wine: win.wine,
|
||||||
date: epochDate
|
date: epochDate
|
||||||
}
|
};
|
||||||
|
|
||||||
const existingDateIndex = highscoreByDate.findIndex(el => el.date == epochDate)
|
const existingDateIndex = highscoreByDate.findIndex(el => el.date == epochDate);
|
||||||
if (existingDateIndex > -1)
|
if (existingDateIndex > -1) highscoreByDate[existingDateIndex].winners.push(winnerObject);
|
||||||
highscoreByDate[existingDateIndex].winners.push(winnerObject);
|
|
||||||
else
|
else
|
||||||
highscoreByDate.push({
|
highscoreByDate.push({
|
||||||
date: epochDate,
|
date: epochDate,
|
||||||
winners: [winnerObject]
|
winners: [winnerObject]
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
return sortNewestFirst(highscoreByDate);
|
return sortNewestFirst(highscoreByDate);
|
||||||
}
|
};
|
||||||
|
|
||||||
const resolveWineReferences = (highscoreObject, key) => {
|
const resolveWineReferences = (highscoreObject, key) => {
|
||||||
const listWithWines = highscoreObject[key]
|
const listWithWines = highscoreObject[key];
|
||||||
|
|
||||||
return Promise.all(listWithWines.map(element =>
|
return Promise.all(
|
||||||
Wine.findById(element.wine)
|
listWithWines.map(element =>
|
||||||
.then(wine => {
|
Wine.findById(element.wine).then(wine => {
|
||||||
element.wine = wine
|
element.wine = wine;
|
||||||
return element
|
return element;
|
||||||
}))
|
})
|
||||||
)
|
)
|
||||||
.then(resolvedListWithWines => {
|
).then(resolvedListWithWines => {
|
||||||
highscoreObject[key] = resolvedListWithWines;
|
highscoreObject[key] = resolvedListWithWines;
|
||||||
return highscoreObject
|
return highscoreObject;
|
||||||
})
|
});
|
||||||
}
|
};
|
||||||
// end utils
|
// end utils
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
const all = (req, res) => {
|
const all = (req, res) => {
|
||||||
return Highscore.find()
|
return Highscore.find()
|
||||||
.then(highscore => groupHighscoreByDate(highscore))
|
.then(highscore => groupHighscoreByDate(highscore))
|
||||||
.then(lotteries => res.send({
|
.then(lotteries =>
|
||||||
message: "Lotteries by date!",
|
res.send({
|
||||||
lotteries
|
message: "Lotteries by date!",
|
||||||
}))
|
lotteries
|
||||||
}
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const latest = (req, res) => {
|
const latest = (req, res) => {
|
||||||
return groupHighscoreByDate()
|
return groupHighscoreByDate()
|
||||||
.then(lotteries => lotteries.shift()) // first element in list
|
.then(lotteries => lotteries.shift()) // first element in list
|
||||||
.then(latestLottery => resolveWineReferences(latestLottery, "winners"))
|
.then(latestLottery => resolveWineReferences(latestLottery, "winners"))
|
||||||
.then(lottery => res.send({
|
.then(lottery =>
|
||||||
|
res.send({
|
||||||
message: "Latest lottery!",
|
message: "Latest lottery!",
|
||||||
winners: lottery.winners
|
winners: lottery.winners
|
||||||
})
|
})
|
||||||
)
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
const byEpochDate = (req, res) => {
|
const byEpochDate = (req, res) => {
|
||||||
let { date } = req.params;
|
let { date } = req.params;
|
||||||
date = new Date(new Date(parseInt(date)).setHours(0,0,0,0)).getTime()
|
date = new Date(new Date(parseInt(date)).setHours(0, 0, 0, 0)).getTime();
|
||||||
const dateString = epochToDateString(date);
|
const dateString = epochToDateString(date);
|
||||||
|
|
||||||
return groupHighscoreByDate()
|
return groupHighscoreByDate()
|
||||||
.then(lotteries => {
|
.then(lotteries => {
|
||||||
const lottery = lotteries.filter(lottery => lottery.date == date)
|
const lottery = lotteries.filter(lottery => lottery.date == date);
|
||||||
if (lottery.length > 0) {
|
if (lottery.length > 0) {
|
||||||
return lottery[0]
|
return lottery[0];
|
||||||
} else {
|
} else {
|
||||||
return res.status(404).send({
|
return res.status(404).send({
|
||||||
message: `No lottery found for date: ${ dateString }`
|
message: `No lottery found for date: ${dateString}`
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(lottery => resolveWineReferences(lottery, "winners"))
|
.then(lottery => resolveWineReferences(lottery, "winners"))
|
||||||
.then(lottery => res.send({
|
.then(lottery =>
|
||||||
message: `Lottery for date: ${ dateString}`,
|
res.send({
|
||||||
date,
|
message: `Lottery for date: ${dateString}`,
|
||||||
winners: lottery.winners
|
date,
|
||||||
}))
|
winners: lottery.winners
|
||||||
}
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const byName = (req, res) => {
|
const byName = (req, res) => {
|
||||||
const { name } = req.params;
|
const { name } = req.params;
|
||||||
@@ -109,20 +112,22 @@ const byName = (req, res) => {
|
|||||||
return Highscore.find({ name })
|
return Highscore.find({ name })
|
||||||
.then(highscore => {
|
.then(highscore => {
|
||||||
if (highscore.length > 0) {
|
if (highscore.length > 0) {
|
||||||
return highscore[0]
|
return highscore[0];
|
||||||
} else {
|
} else {
|
||||||
return res.status(404).send({
|
return res.status(404).send({
|
||||||
message: `Name: ${ name } not found in leaderboards.`
|
message: `Name: ${name} not found in leaderboards.`
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(highscore => resolveWineReferences(highscore, "wins"))
|
.then(highscore => resolveWineReferences(highscore, "wins"))
|
||||||
.then(highscore => res.send({
|
.then(highscore =>
|
||||||
message: `Lottery winnings for name: ${ name }.`,
|
res.send({
|
||||||
name: highscore.name,
|
message: `Lottery winnings for name: ${name}.`,
|
||||||
highscore: sortNewestFirst(highscore.wins)
|
name: highscore.name,
|
||||||
}))
|
highscore: sortNewestFirst(highscore.wins)
|
||||||
}
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
all,
|
all,
|
||||||
Reference in New Issue
Block a user