/by-color endpoint for sort all winners by color.
Can also include query parameter includeWines for resolved wine references.
This commit is contained in:
@@ -5,7 +5,7 @@ const sortOptions = ["desc", "asc"];
|
||||
const includeWinesOptions = ["true", "false"];
|
||||
|
||||
const all = (req, res) => {
|
||||
let { sort, includeWines } = req.query;
|
||||
const { sort, includeWines } = req.query;
|
||||
|
||||
if (sort !== undefined && !sortOptions.includes(sort)) {
|
||||
return res.status(400).send({
|
||||
@@ -74,7 +74,7 @@ const byDate = (req, res) => {
|
||||
};
|
||||
|
||||
const groupedByDate = (req, res) => {
|
||||
let { sort, includeWines } = req.query;
|
||||
const { sort, includeWines } = req.query;
|
||||
|
||||
if (sort !== undefined && !sortOptions.includes(sort)) {
|
||||
return res.status(400).send({
|
||||
@@ -91,7 +91,7 @@ const groupedByDate = (req, res) => {
|
||||
}
|
||||
|
||||
return winnerRepository
|
||||
.groupedByDate(includeWines, sort)
|
||||
.groupedByDate(includeWines == "true", sort)
|
||||
.then(lotteries =>
|
||||
res.send({
|
||||
lotteries: lotteries,
|
||||
@@ -156,10 +156,39 @@ const byName = (req, res) => {
|
||||
});
|
||||
};
|
||||
|
||||
const byColor = (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
|
||||
.byColor(includeWines == "true")
|
||||
.then(colors =>
|
||||
res.send({
|
||||
colors: colors,
|
||||
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,
|
||||
latest,
|
||||
byName
|
||||
byName,
|
||||
byColor
|
||||
};
|
||||
|
||||
@@ -78,8 +78,6 @@ const byDate = date => {
|
||||
};
|
||||
|
||||
const byName = (name, sort = "desc") => {
|
||||
const populateOptions = { sort: "date" };
|
||||
|
||||
return Winner.findOne({ name }, ["name", "wins"])
|
||||
.sort("-wins.date")
|
||||
.populate("wins.wine")
|
||||
@@ -183,9 +181,60 @@ const groupedByDate = (includeWines = false, sort = "desc") => {
|
||||
return Winner.aggregate(query).then(lotteries => (sort != "asc" ? lotteries : lotteries.reverse()));
|
||||
};
|
||||
|
||||
const byColor = (includeWines = false) => {
|
||||
const query = [
|
||||
{
|
||||
$unwind: "$wins"
|
||||
},
|
||||
{
|
||||
$group: {
|
||||
_id: "$wins.color",
|
||||
winners: {
|
||||
$push: {
|
||||
_id: "$_id",
|
||||
name: "$name",
|
||||
date: "$wins.date",
|
||||
wine: "$wins.wine"
|
||||
}
|
||||
},
|
||||
count: { $sum: 1 }
|
||||
}
|
||||
},
|
||||
{
|
||||
$project: {
|
||||
color: "$_id",
|
||||
count: "$count",
|
||||
winners: "$winners"
|
||||
}
|
||||
},
|
||||
{
|
||||
$sort: {
|
||||
_id: -1
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
console.log("includeWines:", includeWines);
|
||||
console.log("includeWines:", includeWines == true);
|
||||
|
||||
if (includeWines) {
|
||||
query.splice(1, 0, {
|
||||
$lookup: {
|
||||
from: "wines",
|
||||
localField: "wins.wine",
|
||||
foreignField: "_id",
|
||||
as: "wins.wine"
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return Winner.aggregate(query);
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
all,
|
||||
byDate,
|
||||
latest,
|
||||
groupedByDate
|
||||
groupedByDate,
|
||||
byColor
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user