Db migration to support custom order of bookmarks. Created route to reorder bookmarks. Added more sorting options to bookmark controllers. Simplified ordering in getAllApps controller
This commit is contained in:
parent
dfdd49cf4a
commit
f1f7b698f8
10 changed files with 95 additions and 35 deletions
|
@ -28,17 +28,15 @@ const getAllApps = asyncWrapper(async (req, res, next) => {
|
|||
// apps visibility
|
||||
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
if (orderType == 'name') {
|
||||
apps = await App.findAll({
|
||||
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
|
||||
where,
|
||||
});
|
||||
} else {
|
||||
apps = await App.findAll({
|
||||
order: [[orderType, 'ASC']],
|
||||
where,
|
||||
});
|
||||
}
|
||||
const order =
|
||||
orderType == 'name'
|
||||
? [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']]
|
||||
: [[orderType, 'ASC']];
|
||||
|
||||
apps = await App.findAll({
|
||||
order,
|
||||
where,
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV === 'production') {
|
||||
// Set header to fetch containers info every time
|
||||
|
|
|
@ -1,16 +1,24 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
const { Sequelize } = require('sequelize');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
// @desc Get all bookmarks
|
||||
// @route GET /api/bookmarks
|
||||
// @access Public
|
||||
const getAllBookmarks = asyncWrapper(async (req, res, next) => {
|
||||
const { useOrdering: orderType } = await loadConfig();
|
||||
|
||||
// bookmarks visibility
|
||||
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const order =
|
||||
orderType == 'name'
|
||||
? [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']]
|
||||
: [[orderType, 'ASC']];
|
||||
|
||||
const bookmarks = await Bookmark.findAll({
|
||||
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
|
||||
order,
|
||||
where,
|
||||
});
|
||||
|
||||
|
|
|
@ -4,4 +4,5 @@ module.exports = {
|
|||
getSingleBookmark: require('./getSingleBookmark'),
|
||||
updateBookmark: require('./updateBookmark'),
|
||||
deleteBookmark: require('./deleteBookmark'),
|
||||
reorderBookmarks: require('./reorderBookmarks'),
|
||||
};
|
||||
|
|
23
controllers/bookmarks/reorderBookmarks.js
Normal file
23
controllers/bookmarks/reorderBookmarks.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
|
||||
// @desc Reorder bookmarks
|
||||
// @route PUT /api/bookmarks/0/reorder
|
||||
// @access Public
|
||||
const reorderBookmarks = asyncWrapper(async (req, res, next) => {
|
||||
req.body.bookmarks.forEach(async ({ id, orderId }) => {
|
||||
await Bookmark.update(
|
||||
{ orderId },
|
||||
{
|
||||
where: { id },
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = reorderBookmarks;
|
|
@ -16,29 +16,21 @@ const getAllCategories = asyncWrapper(async (req, res, next) => {
|
|||
// categories visibility
|
||||
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
if (orderType == 'name') {
|
||||
categories = await Category.findAll({
|
||||
include: [
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
},
|
||||
],
|
||||
order: [[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC']],
|
||||
where,
|
||||
});
|
||||
} else {
|
||||
categories = await Category.findAll({
|
||||
include: [
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
},
|
||||
],
|
||||
order: [[orderType, 'ASC']],
|
||||
where,
|
||||
});
|
||||
}
|
||||
const order =
|
||||
orderType == 'name'
|
||||
? [[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']]
|
||||
: [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']];
|
||||
|
||||
categories = categories = await Category.findAll({
|
||||
include: [
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
},
|
||||
],
|
||||
order,
|
||||
where,
|
||||
});
|
||||
|
||||
if (req.isAuthenticated) {
|
||||
output = categories;
|
||||
|
|
|
@ -2,13 +2,22 @@ const asyncWrapper = require('../../middleware/asyncWrapper');
|
|||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const Category = require('../../models/Category');
|
||||
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('bookmarks.name')), 'ASC']]
|
||||
: [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']];
|
||||
|
||||
const category = await Category.findOne({
|
||||
where: { id: req.params.id, ...visibility },
|
||||
include: [
|
||||
|
@ -18,6 +27,7 @@ const getSingleCategory = asyncWrapper(async (req, res, next) => {
|
|||
where: visibility,
|
||||
},
|
||||
],
|
||||
order,
|
||||
});
|
||||
|
||||
if (!category) {
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Category = require('../../models/Category');
|
||||
|
||||
// @desc Reorder categories
|
||||
// @route PUT /api/categories/0/reorder
|
||||
// @access Public
|
||||
|
|
19
db/migrations/04_bookmarks-order.js
Normal file
19
db/migrations/04_bookmarks-order.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
const { DataTypes } = require('sequelize');
|
||||
const { INTEGER } = DataTypes;
|
||||
|
||||
const up = async (query) => {
|
||||
await query.addColumn('bookmarks', 'orderId', {
|
||||
type: INTEGER,
|
||||
allowNull: true,
|
||||
defaultValue: null,
|
||||
});
|
||||
};
|
||||
|
||||
const down = async (query) => {
|
||||
await query.removeColumn('bookmarks', 'orderId');
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
up,
|
||||
down,
|
||||
};
|
|
@ -25,6 +25,11 @@ const Bookmark = sequelize.define(
|
|||
allowNull: true,
|
||||
defaultValue: 1,
|
||||
},
|
||||
orderId: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: true,
|
||||
defaultValue: null,
|
||||
},
|
||||
},
|
||||
{
|
||||
tableName: 'bookmarks',
|
||||
|
|
|
@ -10,6 +10,7 @@ const {
|
|||
getSingleBookmark,
|
||||
updateBookmark,
|
||||
deleteBookmark,
|
||||
reorderBookmarks,
|
||||
} = require('../controllers/bookmarks');
|
||||
|
||||
router
|
||||
|
@ -23,4 +24,6 @@ router
|
|||
.put(auth, requireAuth, upload, updateBookmark)
|
||||
.delete(auth, requireAuth, deleteBookmark);
|
||||
|
||||
router.route('/0/reorder').put(auth, requireAuth, reorderBookmarks);
|
||||
|
||||
module.exports = router;
|
||||
|
|
Loading…
Reference in a new issue