Skip to content

Commit

Permalink
Merge pull request #199 from julianpoy/fix-recipe-totalCount
Browse files Browse the repository at this point in the history
Added fromUser query for by-page route
  • Loading branch information
julianpoy authored Mar 24, 2019
2 parents a83dc1d + 60e7796 commit a3d318e
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions Backend/routes/recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,13 @@ router.get(

let recipeAttributes = ['id', 'title', 'description', 'source', 'url', 'image', 'folder', 'fromUserId', 'createdAt', 'updatedAt'];
let labelAttributes = ['id', 'title'];
let fromUserAttributes = ['name', 'email'];

let recipeSelect = recipeAttributes.map(el => `"Recipe"."${el}" AS "${el}"`).join(', ');
let labelSelect = labelAttributes.map(el => `"Label"."${el}" AS "labels.${el}"`).join(', ');
let fromUserSelect = fromUserAttributes.map(el => `"FromUser"."${el}" AS "fromUser.${el}"`).join(', ');
let fields = `${recipeSelect}, ${labelSelect}`;
if (req.query.folder === 'inbox') fields += `, ${fromUserSelect}`;

let countQuery = labelFilter.length > 0 ?
`SELECT count("Recipe".id)
Expand Down Expand Up @@ -208,6 +211,7 @@ router.get(
INNER JOIN "Recipes" AS "Recipe" ON "Recipe".id = pag.id
INNER JOIN "Recipe_Labels" AS "Recipe_Label" ON "Recipe_Label"."recipeId" = pag.id
INNER JOIN "Labels" AS "Label" ON "Label".id = "Recipe_Label"."labelId"
${req.query.folder === 'inbox' ? 'LEFT OUTER JOIN "Users" AS "FromUser" ON "FromUser".id = "Recipe"."fromUserId"' : ''}
ORDER BY ${sort}`
:
`SELECT ${fields} FROM (SELECT "Recipe".id
Expand All @@ -220,6 +224,7 @@ router.get(
INNER JOIN "Recipes" AS "Recipe" ON "Recipe".id = pag.id
LEFT OUTER JOIN "Recipe_Labels" AS "Recipe_Label" ON "Recipe_Label"."recipeId" = pag.id
LEFT OUTER JOIN "Labels" AS "Label" ON "Label".id = "Recipe_Label"."labelId"
${req.query.folder === 'inbox' ? 'LEFT OUTER JOIN "Users" AS "FromUser" ON "FromUser".id = "Recipe"."fromUserId"' : ''}
ORDER BY ${sort}`;

let countQueryOptions = {
Expand Down Expand Up @@ -249,6 +254,12 @@ router.get(
}]
}

if (req.query.folder === 'inbox') fetchQueryOptions.include.push({
model: User,
as: 'fromUser',
attributes: fromUserAttributes
})

Recipe._validateIncludedElements(fetchQueryOptions);

Promise.all([
Expand Down

0 comments on commit a3d318e

Please sign in to comment.