Skip to content

Commit

Permalink
auto commit
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Sep 3, 2024
1 parent c05e7a0 commit c14497c
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions database/filetrs.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,31 @@ async function getCocktailCountByFilter(filters) {
}

async function getCocktailSubsetByFilter(filters, skip, limit, sortType = 'most-popular') {
const cocktailIds = getCocktailIds(filters);
const sortField = sortType === 'most-popular' ? { visitCount: -1 } : { ratingValue: -1 };

const cocktails = await Database.collection('cocktails')
.find({ slug: { $in: Array.from(cocktailIds) } })
.sort(sortField)
.skip(skip)
.limit(limit)
.project({ _id: 0, id: 1, slug: 1, name: 1, ratingCount: 1, ratingValue: 1, visitCount: 1 })
.toArray();
let cocktails;
if (filters === undefined || Object.keys(filters).every(key => filters[key].length === 0)) {
// return all cocktails with paggnation and sorting

cocktails = await Database.collection('cocktails')
.find()
.sort(sortField)
.skip(skip)
.limit(limit)
.project({ _id: 0, id: 1, slug: 1, name: 1, ratingCount: 1, ratingValue: 1, visitCount: 1 })
.toArray();

} else {
const cocktailIds = getCocktailIds(filters);

cocktails = await Database.collection('cocktails')
.find({ slug: { $in: Array.from(cocktailIds) } })
.sort(sortField)
.skip(skip)
.limit(limit)
.project({ _id: 0, id: 1, slug: 1, name: 1, ratingCount: 1, ratingValue: 1, visitCount: 1 })
.toArray();
}

cocktails.forEach(cocktail => {
cocktail.images = buildImages(cocktail.id, 'COCKTAIL');
Expand Down

0 comments on commit c14497c

Please sign in to comment.