History actions now have a controller setup.
This commit is contained in:
92
api/controllers/historyController.js
Normal file
92
api/controllers/historyController.js
Normal file
@@ -0,0 +1,92 @@
|
||||
const path = require("path");
|
||||
const historyRepository = require(path.join(__dirname, "../history"));
|
||||
|
||||
const all = (req, res) => {
|
||||
return historyRepository
|
||||
.all()
|
||||
.then(lotteries =>
|
||||
res.send({
|
||||
lotteries: lotteries,
|
||||
success: true
|
||||
})
|
||||
)
|
||||
.catch(error => {
|
||||
const { statusCode, message } = error;
|
||||
|
||||
return res.status(statusCode || 500).send({
|
||||
success: false,
|
||||
message: message || "Unable to fetch history."
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const latest = (req, res) => {
|
||||
return historyRepository
|
||||
.latest()
|
||||
.then(lottery =>
|
||||
res.send({
|
||||
lottery: lottery,
|
||||
success: true
|
||||
})
|
||||
)
|
||||
.catch(error => {
|
||||
const { statusCode, message } = error;
|
||||
|
||||
return res.status(statusCode || 500).send({
|
||||
success: false,
|
||||
message: message || "Unable to fetch latest history."
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const byDate = (req, res) => {
|
||||
let { date } = req.params;
|
||||
date = new Date(new Date(parseInt(date)).setHours(0, 0, 0, 0)).getTime();
|
||||
|
||||
return historyRepository
|
||||
.byEpochDate(date)
|
||||
.then(winners =>
|
||||
res.send({
|
||||
date: date,
|
||||
winners: winners,
|
||||
success: true
|
||||
})
|
||||
)
|
||||
.catch(error => {
|
||||
const { statusCode, message } = error;
|
||||
|
||||
return res.status(statusCode || 500).send({
|
||||
success: false,
|
||||
message: message || "Unable to fetch history for date."
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const byName = (req, res) => {
|
||||
const { name } = req.params;
|
||||
|
||||
return historyRepository
|
||||
.byName(name)
|
||||
.then(lotteries =>
|
||||
res.send({
|
||||
name: name,
|
||||
lotteries: lotteries,
|
||||
success: true
|
||||
})
|
||||
)
|
||||
.catch(error => {
|
||||
const { statusCode, message } = error;
|
||||
|
||||
return res.status(statusCode || 500).send({
|
||||
success: false,
|
||||
message: message || "Unable to fetch history for name."
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
all,
|
||||
latest,
|
||||
byDate,
|
||||
byName
|
||||
};
|
||||
@@ -3,6 +3,22 @@ const path = require("path");
|
||||
const Highscore = require(path.join(__dirname, "/schemas/Highscore"));
|
||||
const Wine = require(path.join(__dirname, "/schemas/Wine"));
|
||||
|
||||
class HistoryByDateNotFound extends Error {
|
||||
constructor(message = "History for given date not found.") {
|
||||
super(message);
|
||||
this.name = "HistoryByDateNotFound";
|
||||
this.statusCode = 404;
|
||||
}
|
||||
}
|
||||
|
||||
class HistoryForUserNotFound extends Error {
|
||||
constructor(message = "History for given user not found.") {
|
||||
super(message);
|
||||
this.name = "HistoryForUserNotFound";
|
||||
this.statusCode = 404;
|
||||
}
|
||||
}
|
||||
|
||||
// Utils
|
||||
const epochToDateString = date => new Date(parseInt(date)).toDateString();
|
||||
|
||||
@@ -56,77 +72,41 @@ const resolveWineReferences = (highscoreObject, key) => {
|
||||
// end utils
|
||||
|
||||
// Routes
|
||||
const all = (req, res) => {
|
||||
return Highscore.find()
|
||||
.then(highscore => groupHighscoreByDate(highscore))
|
||||
.then(lotteries =>
|
||||
res.send({
|
||||
message: "Lotteries by date!",
|
||||
lotteries
|
||||
})
|
||||
);
|
||||
const all = () => {
|
||||
return Highscore.find().then(highscore => groupHighscoreByDate(highscore));
|
||||
};
|
||||
|
||||
const latest = (req, res) => {
|
||||
const latest = () => {
|
||||
return groupHighscoreByDate()
|
||||
.then(lotteries => lotteries.shift()) // first element in list
|
||||
.then(latestLottery => resolveWineReferences(latestLottery, "winners"))
|
||||
.then(lottery =>
|
||||
res.send({
|
||||
message: "Latest lottery!",
|
||||
winners: lottery.winners
|
||||
})
|
||||
);
|
||||
.then(latestLottery => resolveWineReferences(latestLottery, "winners"));
|
||||
};
|
||||
|
||||
const byEpochDate = (req, res) => {
|
||||
let { date } = req.params;
|
||||
date = new Date(new Date(parseInt(date)).setHours(0, 0, 0, 0)).getTime();
|
||||
const dateString = epochToDateString(date);
|
||||
|
||||
const byEpochDate = date => {
|
||||
return groupHighscoreByDate()
|
||||
.then(lotteries => {
|
||||
const lottery = lotteries.filter(lottery => lottery.date == date);
|
||||
if (lottery.length > 0) {
|
||||
return lottery[0];
|
||||
} else {
|
||||
return res.status(404).send({
|
||||
message: `No lottery found for date: ${dateString}`
|
||||
});
|
||||
throw new HistoryByDateNotFound();
|
||||
}
|
||||
})
|
||||
.then(lottery => resolveWineReferences(lottery, "winners"))
|
||||
.then(lottery =>
|
||||
res.send({
|
||||
message: `Lottery for date: ${dateString}`,
|
||||
date,
|
||||
winners: lottery.winners
|
||||
})
|
||||
);
|
||||
.then(lottery => lottery.winners);
|
||||
};
|
||||
|
||||
const byName = (req, res) => {
|
||||
const { name } = req.params;
|
||||
const regexName = new RegExp(name, "i"); // lowercase regex of the name
|
||||
|
||||
const byName = name => {
|
||||
return Highscore.find({ name })
|
||||
.then(highscore => {
|
||||
if (highscore.length > 0) {
|
||||
return highscore[0];
|
||||
} else {
|
||||
return res.status(404).send({
|
||||
message: `Name: ${name} not found in leaderboards.`
|
||||
});
|
||||
throw new HistoryForUserNotFound();
|
||||
}
|
||||
})
|
||||
.then(highscore => resolveWineReferences(highscore, "wins"))
|
||||
.then(highscore =>
|
||||
res.send({
|
||||
message: `Lottery winnings for name: ${name}.`,
|
||||
name: highscore.name,
|
||||
highscore: sortNewestFirst(highscore.wins)
|
||||
})
|
||||
);
|
||||
.then(highscore => sortNewestFirst(highscore.wins));
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
|
||||
Reference in New Issue
Block a user