Winner controller, winners by date, color or name.
Winner controller replaces a lot of what happens in retrieve did for aggregating when and what had been won. Now this is more clearly defined in winner.js. Also leverage mongo's query more than sorting and aggregating data like previous implementation where a lot happened in js.
This commit is contained in:
@@ -73,7 +73,7 @@ const byDate = (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
const groupedByDate = (req, res) => {
|
||||
const groupByDate = (req, res) => {
|
||||
const { sort, includeWines } = req.query;
|
||||
|
||||
if (sort !== undefined && !sortOptions.includes(sort)) {
|
||||
@@ -156,7 +156,7 @@ const byName = (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
const byColor = (req, res) => {
|
||||
const groupByColor = (req, res) => {
|
||||
const { includeWines } = req.query;
|
||||
|
||||
if (includeWines !== undefined && !includeWinesOptions.includes(includeWines)) {
|
||||
@@ -167,7 +167,7 @@ const byColor = (req, res) => {
|
||||
}
|
||||
|
||||
return winnerRepository
|
||||
.byColor(includeWines == "true")
|
||||
.groupByColor(includeWines == "true")
|
||||
.then(colors =>
|
||||
res.send({
|
||||
colors: colors,
|
||||
@@ -184,11 +184,40 @@ const byColor = (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
const orderByWins = (req, res) => {
|
||||
const { includeWines } = req.query;
|
||||
|
||||
if (includeWines !== undefined && !includeWinesOptions.includes(includeWines)) {
|
||||
return res.status(400).send({
|
||||
message: `includeWines option must be: '${includeWinesOptions.join(", ")}'`,
|
||||
success: false
|
||||
});
|
||||
}
|
||||
|
||||
return winnerRepository
|
||||
.orderByWins(includeWines == "true")
|
||||
.then(winners =>
|
||||
res.send({
|
||||
winners: winners,
|
||||
success: true
|
||||
})
|
||||
)
|
||||
.catch(error => {
|
||||
const { statusCode, message } = error;
|
||||
|
||||
return res.status(statusCode || 500).send({
|
||||
success: false,
|
||||
message: message || "Unable to fetch winners by color."
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
all,
|
||||
byDate,
|
||||
groupedByDate,
|
||||
groupByDate,
|
||||
latest,
|
||||
byName,
|
||||
byColor
|
||||
groupByColor,
|
||||
orderByWins
|
||||
};
|
||||
|
||||
@@ -136,7 +136,7 @@ const latest = () => {
|
||||
return Winner.aggregate(query).then(winners => winners[0]);
|
||||
};
|
||||
|
||||
const groupedByDate = (includeWines = false, sort = "desc") => {
|
||||
const groupByDate = (includeWines = false, sort = "desc") => {
|
||||
const query = [
|
||||
{
|
||||
$unwind: "$wins"
|
||||
@@ -181,7 +181,7 @@ const groupedByDate = (includeWines = false, sort = "desc") => {
|
||||
return Winner.aggregate(query).then(lotteries => (sort != "asc" ? lotteries : lotteries.reverse()));
|
||||
};
|
||||
|
||||
const byColor = (includeWines = false) => {
|
||||
const groupByColor = (includeWines = false) => {
|
||||
const query = [
|
||||
{
|
||||
$unwind: "$wins"
|
||||
@@ -214,9 +214,6 @@ const byColor = (includeWines = false) => {
|
||||
}
|
||||
];
|
||||
|
||||
console.log("includeWines:", includeWines);
|
||||
console.log("includeWines:", includeWines == true);
|
||||
|
||||
if (includeWines) {
|
||||
query.splice(1, 0, {
|
||||
$lookup: {
|
||||
@@ -231,10 +228,60 @@ const byColor = (includeWines = false) => {
|
||||
return Winner.aggregate(query);
|
||||
};
|
||||
|
||||
const orderByWins = (includeWines = false) => {
|
||||
let query = [
|
||||
{
|
||||
$project: {
|
||||
name: "$name",
|
||||
wins: "$wins",
|
||||
totalWins: { $size: "$wins" }
|
||||
}
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
totalWins: -1,
|
||||
"wins.date": -1
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
if (includeWines) {
|
||||
const includeWinesSubQuery = [
|
||||
{
|
||||
$unwind: "$wins"
|
||||
},
|
||||
{
|
||||
$lookup: {
|
||||
from: "wines",
|
||||
localField: "wins.wine",
|
||||
foreignField: "_id",
|
||||
as: "wins.wine"
|
||||
}
|
||||
},
|
||||
{
|
||||
$unwind: "$wins._id"
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: "$_id",
|
||||
name: { $first: "$name" },
|
||||
totalWins: { $first: "$totalWins" },
|
||||
wins: { $push: "$wins" }
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
query = includeWinesSubQuery.concat(query);
|
||||
}
|
||||
|
||||
return Winner.aggregate(query);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
all,
|
||||
byDate,
|
||||
latest,
|
||||
groupedByDate,
|
||||
byColor
|
||||
groupByDate,
|
||||
groupByColor,
|
||||
orderByWins
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user