mirror of
https://github.com/pawelmalak/flame.git
synced 2025-07-31 00:19:36 +02:00
Merge branch 'master' of https://github.com/pawelmalak/flame into merge_upstream_2020-12-06
This commit is contained in:
commit
021bd4e85a
266 changed files with 13470 additions and 7067 deletions
66
controllers/categories/getAllCategories.js
Normal file
66
controllers/categories/getAllCategories.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Category = require('../../models/Category');
|
||||
const App = require('../../models/App');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
const { Sequelize } = require('sequelize');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
// @desc Get all categories
|
||||
// @route GET /api/categories
|
||||
// @access Public
|
||||
const getAllCategories = asyncWrapper(async (req, res, next) => {
|
||||
const { useOrdering: orderType } = await loadConfig();
|
||||
|
||||
let categories;
|
||||
let output;
|
||||
|
||||
// categories visibility
|
||||
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const order =
|
||||
orderType == 'name'
|
||||
? [
|
||||
[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC'],
|
||||
[Sequelize.fn('lower', Sequelize.col('apps.name')), 'ASC'],
|
||||
[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC'],
|
||||
]
|
||||
: [
|
||||
[orderType, 'ASC'],
|
||||
[{ model: App, as: 'apps' }, orderType, 'ASC'],
|
||||
[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC'],
|
||||
];
|
||||
|
||||
categories = categories = await Category.findAll({
|
||||
include: [
|
||||
{
|
||||
model: App,
|
||||
as: 'apps',
|
||||
},
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
},
|
||||
],
|
||||
order,
|
||||
where,
|
||||
});
|
||||
|
||||
if (req.isAuthenticated) {
|
||||
output = categories;
|
||||
} else {
|
||||
// filter out private apps/bookmarks
|
||||
output = categories.map((c) => c.get({ plain: true }));
|
||||
output = output.map((c) => ({
|
||||
...c,
|
||||
apps: c.apps.filter((b) => b.isPublic),
|
||||
bookmarks: c.bookmarks.filter((b) => b.isPublic),
|
||||
}));
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: output,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getAllCategories;
|
Loading…
Add table
Add a link
Reference in a new issue