Skip to content

Commit

Permalink
add api/filters
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Sep 11, 2024
1 parent d895b89 commit 89cc279
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
178 changes: 178 additions & 0 deletions database/filters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
const Database = require('./newclient');

async function getAlcoholoVolume() {
const alcoholVolumes = await Database
.collection('alcoholVolumes')
.aggregate([
{
$project: {
_id: 0,
id: 1,
name: 1,
slug: 1,
count: { $size: "$cocktailSlugs" }
}
},
{ $sort: { count: -1 } }
])
.toArray();

return alcoholVolumes
}

async function getTastes() {
const tastes = await Database
.collection('tastes')
.aggregate([
{
$project: {
_id: 0,
id: 1,
name: 1,
slug: 1,
count: { $size: "$cocktailSlugs" }
}
},
{ $sort: { count: -1 } }
])
.toArray();

return tastes
}

async function getGlasswares() {
const glasswares = await Database
.collection('glassware')
.aggregate([
{
$project: {
_id: 0,
id: 1,
name: 1,
slug: 1,
count: { $size: "$cocktailSlugs" }
}
},
{ $sort: { count: -1 } }
])
.toArray();

return glasswares
}

async function getGoods() {
const goods = await Database
.collection('goods')
.aggregate([
{
$project: {
_id: 0,
id: 1,
name: 1,
slug: 1,
count: { $size: "$cocktailSlugs" }
}
},
{ $sort: { count: -1 } }
])
.toArray();

return goods;
}

async function getTagsData() {
const tags = await Database
.collection('tags')
.aggregate([
{
$project: {
_id: 0,
id: 1,
name: 1,
slug: 1,
count: { $size: "$cocktailSlugs" }
}
},
{ $sort: { count: -1 } }
])
.toArray();

return tags;
}

async function getToolsData() {
const tools = await Database
.collection('tools')
.aggregate([
{
$project: {
_id: 0,
id: 1,
name: 1,
slug: 1,
count: { $size: "$cocktailSlugs" }
}
},
{ $sort: { count: -1 } }
])
.toArray();

return tools;
}

async function getFiltersData() {
return [
{
id: 4,
queryName: 'alcohol-volume',
name: 'Алкоголь',
items: await getAlcoholoVolume(),
selectionType: 'SINGLE',
sortOrder: 1,
},
{
id: 3,
queryName: 'taste',
name: 'Смак',
items: await getTastes(),
selectionType: 'MULTIPLE',
sortOrder: 2,
},
{
id: 5,
queryName: 'glassware',
name: 'Стакан',
items: await getGlasswares(),
selectionType: 'SINGLE',
sortOrder: 3,
},
{
id: 1,
queryName: 'goods',
name: 'Інгрідієнти',
items: await getGoods(),
selectionType: 'MULTIPLE',
sortOrder: 4,
},
{
id: 0,
queryName: 'tags',
name: 'Інше',
items: await getTagsData(),
selectionType: 'MULTIPLE',
sortOrder: 5,
},
{
id: 2,
queryName: 'tools',
name: 'Приладдя',
items: await getToolsData(),
selectionType: 'MULTIPLE',
sortOrder: 6,
}
]
}

module.exports = {
getFiltersData
}
13 changes: 13 additions & 0 deletions features/filters/rest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const express = require('express')
const { getFullCocktailByFilter } = require('../../database/filetrs');
const { getFiltersData } = require('../../database/filters');
const DescriptionBuilder = require('./description');
const router = express.Router();

Expand Down Expand Up @@ -36,4 +37,16 @@ router.get('/api/filter/*', async (req, res) => {
return res.status(200).send(response);
});

const filterDataCache = {};

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

return res.status(200).send(response);
});

module.exports = router;

0 comments on commit 89cc279

Please sign in to comment.