Skip to content

Commit

Permalink
add recomendation cocktalis
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Sep 15, 2024
1 parent 9a40d9f commit 71fdfaa
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion database/cocktail.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ const CocktailSchema = new Schema({
const Cocktail = mongoose.model('Cocktail', CocktailSchema);

async function getCocktailBySlug(slug) {
return Cocktail.findOne({slug}).lean();
return Cocktail.findOne({ slug }).lean();
}

module.exports = {
Expand Down
36 changes: 36 additions & 0 deletions features/cocktail/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
const { buildImages, buildOgImage } = require("../../utils/image");
const Database = require('../../database/newclient');
const { getCocktailBySlug } = require("../../database/cocktail");

async function getRecomendations(cocktail) {
const goodMatchSlugs = cocktail.matches.good;
const otherMatchSlugs = cocktail.matches.other;

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

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

const cocktails = goodCocktails.concat(otherCocktails).slice(0, 20);

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

cocktail.rating = cocktail.ratingCount ? cocktail.ratingValue / cocktail.ratingCount : 0;
if (cocktail.rating === 0) {
cocktail.rating = null;
}
delete cocktail.ratingCount;
delete cocktail.ratingValue;
});

return cocktails;
}

async function getFullCocktailBySlug(slug) {
try {
const cocktail = await getCocktailBySlug(slug);
Expand Down Expand Up @@ -97,6 +132,7 @@ async function getFullCocktailBySlug(slug) {
tools: glassware.concat(tools),
tags: taste.concat(tags).concat(alcohols).concat([alcoholVolumeTag]),
article: article,
recomendationCocktails: await getRecomendations(cocktail),
};
} catch (error) {
console.error(error);
Expand Down
2 changes: 1 addition & 1 deletion features/filters/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const filterDataCache = {};

router.get('/api/filters', async (req, res) => {
if (filterDataCache.data) {
// return res.status(200).send(filterDataCache.data);
return res.status(200).send(filterDataCache.data);
}
const response = await getFiltersData();
filterDataCache.data = response;
Expand Down

0 comments on commit 71fdfaa

Please sign in to comment.