Feat/controllers - refactor entire backend and new admin interface #75

Merged
KevinMidboe merged 117 commits from feat/controllers into master 2021-02-19 00:19:52 +00:00
2 changed files with 88 additions and 12 deletions
Showing only changes of commit e07e6ae09a - Show all commits

View File

@@ -73,7 +73,7 @@ const byDate = (req, res) => {
}); });
}; };
const groupedByDate = (req, res) => { const groupByDate = (req, res) => {
const { sort, includeWines } = req.query; const { sort, includeWines } = req.query;
if (sort !== undefined && !sortOptions.includes(sort)) { 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; const { includeWines } = req.query;
if (includeWines !== undefined && !includeWinesOptions.includes(includeWines)) { if (includeWines !== undefined && !includeWinesOptions.includes(includeWines)) {
@@ -167,7 +167,7 @@ const byColor = (req, res) => {
} }
return winnerRepository return winnerRepository
.byColor(includeWines == "true") .groupByColor(includeWines == "true")
.then(colors => .then(colors =>
res.send({ res.send({
colors: colors, 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 = { module.exports = {
all, all,
byDate, byDate,
groupedByDate, groupByDate,
latest, latest,
byName, byName,
byColor groupByColor,
orderByWins
}; };

View File

@@ -136,7 +136,7 @@ const latest = () => {
return Winner.aggregate(query).then(winners => winners[0]); return Winner.aggregate(query).then(winners => winners[0]);
}; };
const groupedByDate = (includeWines = false, sort = "desc") => { const groupByDate = (includeWines = false, sort = "desc") => {
const query = [ const query = [
{ {
$unwind: "$wins" $unwind: "$wins"
@@ -181,7 +181,7 @@ const groupedByDate = (includeWines = false, sort = "desc") => {
return Winner.aggregate(query).then(lotteries => (sort != "asc" ? lotteries : lotteries.reverse())); return Winner.aggregate(query).then(lotteries => (sort != "asc" ? lotteries : lotteries.reverse()));
}; };
const byColor = (includeWines = false) => { const groupByColor = (includeWines = false) => {
const query = [ const query = [
{ {
$unwind: "$wins" $unwind: "$wins"
@@ -214,9 +214,6 @@ const byColor = (includeWines = false) => {
} }
]; ];
console.log("includeWines:", includeWines);
console.log("includeWines:", includeWines == true);
if (includeWines) { if (includeWines) {
query.splice(1, 0, { query.splice(1, 0, {
$lookup: { $lookup: {
@@ -231,10 +228,60 @@ const byColor = (includeWines = false) => {
return Winner.aggregate(query); 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 = { module.exports = {
all, all,
byDate, byDate,
latest, latest,
groupedByDate, groupByDate,
byColor groupByColor,
orderByWins
}; };