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
|
// apps visibility
|
||||||
const where = req.isAuthenticated ? {} : { isPublic: true };
|
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||||
|
|
||||||
if (orderType == 'name') {
|
const order =
|
||||||
apps = await App.findAll({
|
orderType == 'name'
|
||||||
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
|
? [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']]
|
||||||
where,
|
: [[orderType, 'ASC']];
|
||||||
});
|
|
||||||
} else {
|
apps = await App.findAll({
|
||||||
apps = await App.findAll({
|
order,
|
||||||
order: [[orderType, 'ASC']],
|
where,
|
||||||
where,
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'production') {
|
if (process.env.NODE_ENV === 'production') {
|
||||||
// Set header to fetch containers info every time
|
// Set header to fetch containers info every time
|
||||||
|
|
|
@ -1,16 +1,24 @@
|
||||||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||||
const Bookmark = require('../../models/Bookmark');
|
const Bookmark = require('../../models/Bookmark');
|
||||||
const { Sequelize } = require('sequelize');
|
const { Sequelize } = require('sequelize');
|
||||||
|
const loadConfig = require('../../utils/loadConfig');
|
||||||
|
|
||||||
// @desc Get all bookmarks
|
// @desc Get all bookmarks
|
||||||
// @route GET /api/bookmarks
|
// @route GET /api/bookmarks
|
||||||
// @access Public
|
// @access Public
|
||||||
const getAllBookmarks = asyncWrapper(async (req, res, next) => {
|
const getAllBookmarks = asyncWrapper(async (req, res, next) => {
|
||||||
|
const { useOrdering: orderType } = await loadConfig();
|
||||||
|
|
||||||
// bookmarks visibility
|
// bookmarks visibility
|
||||||
const where = req.isAuthenticated ? {} : { isPublic: true };
|
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||||
|
|
||||||
|
const order =
|
||||||
|
orderType == 'name'
|
||||||
|
? [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']]
|
||||||
|
: [[orderType, 'ASC']];
|
||||||
|
|
||||||
const bookmarks = await Bookmark.findAll({
|
const bookmarks = await Bookmark.findAll({
|
||||||
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
|
order,
|
||||||
where,
|
where,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -4,4 +4,5 @@ module.exports = {
|
||||||
getSingleBookmark: require('./getSingleBookmark'),
|
getSingleBookmark: require('./getSingleBookmark'),
|
||||||
updateBookmark: require('./updateBookmark'),
|
updateBookmark: require('./updateBookmark'),
|
||||||
deleteBookmark: require('./deleteBookmark'),
|
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
|
// categories visibility
|
||||||
const where = req.isAuthenticated ? {} : { isPublic: true };
|
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||||
|
|
||||||
if (orderType == 'name') {
|
const order =
|
||||||
categories = await Category.findAll({
|
orderType == 'name'
|
||||||
include: [
|
? [[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']]
|
||||||
{
|
: [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']];
|
||||||
model: Bookmark,
|
|
||||||
as: 'bookmarks',
|
categories = categories = await Category.findAll({
|
||||||
},
|
include: [
|
||||||
],
|
{
|
||||||
order: [[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC']],
|
model: Bookmark,
|
||||||
where,
|
as: 'bookmarks',
|
||||||
});
|
},
|
||||||
} else {
|
],
|
||||||
categories = await Category.findAll({
|
order,
|
||||||
include: [
|
where,
|
||||||
{
|
});
|
||||||
model: Bookmark,
|
|
||||||
as: 'bookmarks',
|
|
||||||
},
|
|
||||||
],
|
|
||||||
order: [[orderType, 'ASC']],
|
|
||||||
where,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if (req.isAuthenticated) {
|
if (req.isAuthenticated) {
|
||||||
output = categories;
|
output = categories;
|
||||||
|
|
|
@ -2,13 +2,22 @@ const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||||
const Category = require('../../models/Category');
|
const Category = require('../../models/Category');
|
||||||
const Bookmark = require('../../models/Bookmark');
|
const Bookmark = require('../../models/Bookmark');
|
||||||
|
const { Sequelize } = require('sequelize');
|
||||||
|
const loadConfig = require('../../utils/loadConfig');
|
||||||
|
|
||||||
// @desc Get single category
|
// @desc Get single category
|
||||||
// @route GET /api/categories/:id
|
// @route GET /api/categories/:id
|
||||||
// @access Public
|
// @access Public
|
||||||
const getSingleCategory = asyncWrapper(async (req, res, next) => {
|
const getSingleCategory = asyncWrapper(async (req, res, next) => {
|
||||||
|
const { useOrdering: orderType } = await loadConfig();
|
||||||
|
|
||||||
const visibility = req.isAuthenticated ? {} : { isPublic: true };
|
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({
|
const category = await Category.findOne({
|
||||||
where: { id: req.params.id, ...visibility },
|
where: { id: req.params.id, ...visibility },
|
||||||
include: [
|
include: [
|
||||||
|
@ -18,6 +27,7 @@ const getSingleCategory = asyncWrapper(async (req, res, next) => {
|
||||||
where: visibility,
|
where: visibility,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
|
order,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!category) {
|
if (!category) {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||||
const Category = require('../../models/Category');
|
const Category = require('../../models/Category');
|
||||||
|
|
||||||
// @desc Reorder categories
|
// @desc Reorder categories
|
||||||
// @route PUT /api/categories/0/reorder
|
// @route PUT /api/categories/0/reorder
|
||||||
// @access Public
|
// @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,
|
allowNull: true,
|
||||||
defaultValue: 1,
|
defaultValue: 1,
|
||||||
},
|
},
|
||||||
|
orderId: {
|
||||||
|
type: DataTypes.INTEGER,
|
||||||
|
allowNull: true,
|
||||||
|
defaultValue: null,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
tableName: 'bookmarks',
|
tableName: 'bookmarks',
|
||||||
|
|
|
@ -10,6 +10,7 @@ const {
|
||||||
getSingleBookmark,
|
getSingleBookmark,
|
||||||
updateBookmark,
|
updateBookmark,
|
||||||
deleteBookmark,
|
deleteBookmark,
|
||||||
|
reorderBookmarks,
|
||||||
} = require('../controllers/bookmarks');
|
} = require('../controllers/bookmarks');
|
||||||
|
|
||||||
router
|
router
|
||||||
|
@ -23,4 +24,6 @@ router
|
||||||
.put(auth, requireAuth, upload, updateBookmark)
|
.put(auth, requireAuth, upload, updateBookmark)
|
||||||
.delete(auth, requireAuth, deleteBookmark);
|
.delete(auth, requireAuth, deleteBookmark);
|
||||||
|
|
||||||
|
router.route('/0/reorder').put(auth, requireAuth, reorderBookmarks);
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
Loading…
Reference in a new issue