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 Highscore = require(path.join(__dirname, "/schemas/Highscore"));
|
||||||
const Wine = require(path.join(__dirname, "/schemas/Wine"));
|
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
|
// Utils
|
||||||
const epochToDateString = date => new Date(parseInt(date)).toDateString();
|
const epochToDateString = date => new Date(parseInt(date)).toDateString();
|
||||||
|
|
||||||
@@ -56,77 +72,41 @@ const resolveWineReferences = (highscoreObject, key) => {
|
|||||||
// end utils
|
// end utils
|
||||||
|
|
||||||
// Routes
|
// Routes
|
||||||
const all = (req, res) => {
|
const all = () => {
|
||||||
return Highscore.find()
|
return Highscore.find().then(highscore => groupHighscoreByDate(highscore));
|
||||||
.then(highscore => groupHighscoreByDate(highscore))
|
|
||||||
.then(lotteries =>
|
|
||||||
res.send({
|
|
||||||
message: "Lotteries by date!",
|
|
||||||
lotteries
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const latest = (req, res) => {
|
const latest = () => {
|
||||||
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({
|
|
||||||
message: "Latest lottery!",
|
|
||||||
winners: lottery.winners
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const byEpochDate = (req, res) => {
|
const byEpochDate = date => {
|
||||||
let { date } = req.params;
|
|
||||||
date = new Date(new Date(parseInt(date)).setHours(0, 0, 0, 0)).getTime();
|
|
||||||
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({
|
throw new HistoryByDateNotFound();
|
||||||
message: `No lottery found for date: ${dateString}`
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(lottery => resolveWineReferences(lottery, "winners"))
|
.then(lottery => resolveWineReferences(lottery, "winners"))
|
||||||
.then(lottery =>
|
.then(lottery => lottery.winners);
|
||||||
res.send({
|
|
||||||
message: `Lottery for date: ${dateString}`,
|
|
||||||
date,
|
|
||||||
winners: lottery.winners
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const byName = (req, res) => {
|
const byName = name => {
|
||||||
const { name } = req.params;
|
|
||||||
const regexName = new RegExp(name, "i"); // lowercase regex of the name
|
|
||||||
|
|
||||||
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({
|
throw new HistoryForUserNotFound();
|
||||||
message: `Name: ${name} not found in leaderboards.`
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.then(highscore => resolveWineReferences(highscore, "wins"))
|
.then(highscore => resolveWineReferences(highscore, "wins"))
|
||||||
.then(highscore =>
|
.then(highscore => sortNewestFirst(highscore.wins));
|
||||||
res.send({
|
|
||||||
message: `Lottery winnings for name: ${name}.`,
|
|
||||||
name: highscore.name,
|
|
||||||
highscore: sortNewestFirst(highscore.wins)
|
|
||||||
})
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
Reference in New Issue
Block a user