mirror of
https://github.com/pawelmalak/flame.git
synced 2026-03-10 06:23:11 +08:00
Sorting and custom ordering for categories
This commit is contained in:
@@ -37,14 +37,32 @@ exports.createCategory = asyncWrapper(async (req, res, next) => {
|
||||
// @route GET /api/categories
|
||||
// @access Public
|
||||
exports.getCategories = asyncWrapper(async (req, res, next) => {
|
||||
const categories = await Category.findAll({
|
||||
include: [{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks'
|
||||
}],
|
||||
order: [[ Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC' ]]
|
||||
// Get config from database
|
||||
const useOrdering = await Config.findOne({
|
||||
where: { key: 'useOrdering' }
|
||||
});
|
||||
|
||||
const orderType = useOrdering ? useOrdering.value : 'createdAt';
|
||||
let categories;
|
||||
|
||||
if (orderType == 'name') {
|
||||
categories = await Category.findAll({
|
||||
include: [{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks'
|
||||
}],
|
||||
order: [[ Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC' ]]
|
||||
});
|
||||
} else {
|
||||
categories = await Category.findAll({
|
||||
include: [{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks'
|
||||
}],
|
||||
order: [[ orderType, 'ASC' ]]
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: categories
|
||||
@@ -119,6 +137,22 @@ exports.deleteCategory = asyncWrapper(async (req, res, next) => {
|
||||
where: { id: req.params.id }
|
||||
})
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {}
|
||||
})
|
||||
})
|
||||
|
||||
// @desc Reorder categories
|
||||
// @route PUT /api/categories/0/reorder
|
||||
// @access Public
|
||||
exports.reorderCategories = asyncWrapper(async (req, res, next) => {
|
||||
req.body.categories.forEach(async ({ id, orderId }) => {
|
||||
await Category.update({ orderId }, {
|
||||
where: { id }
|
||||
})
|
||||
})
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {}
|
||||
|
||||
Reference in New Issue
Block a user