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
60
controllers/categories/getSingleCategory.js
Normal file
60
controllers/categories/getSingleCategory.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
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 single category
|
||||
// @route GET /api/categories/:id
|
||||
// @access Public
|
||||
const getSingleCategory = asyncWrapper(async (req, res, next) => {
|
||||
const { useOrdering: orderType } = await loadConfig();
|
||||
|
||||
const visibility = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const order =
|
||||
orderType == 'name'
|
||||
? [
|
||||
[Sequelize.fn('lower', Sequelize.col('apps.name')), 'ASC'],
|
||||
[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']
|
||||
]
|
||||
: [
|
||||
[{ model: App, as: 'apps' }, orderType, 'ASC']
|
||||
[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']
|
||||
];
|
||||
|
||||
const category = await Category.findOne({
|
||||
where: { id: req.params.id, ...visibility },
|
||||
include: [
|
||||
{
|
||||
model: App,
|
||||
as: 'apps',
|
||||
where: visibility,
|
||||
},
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
where: visibility,
|
||||
},
|
||||
],
|
||||
order,
|
||||
});
|
||||
|
||||
if (!category) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Category with id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: category,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getSingleCategory;
|
Loading…
Add table
Add a link
Reference in a new issue