From f1f7b698f8f6ab6755bbc63e66a14a60c5fe852a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Malak?= Date: Mon, 22 Nov 2021 16:45:59 +0100 Subject: [PATCH] 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 --- controllers/apps/getAllApps.js | 20 +++++------ controllers/bookmarks/getAllBookmarks.js | 10 +++++- controllers/bookmarks/index.js | 1 + controllers/bookmarks/reorderBookmarks.js | 23 +++++++++++++ controllers/categories/getAllCategories.js | 38 ++++++++------------- controllers/categories/getSingleCategory.js | 10 ++++++ controllers/categories/reorderCategories.js | 1 + db/migrations/04_bookmarks-order.js | 19 +++++++++++ models/Bookmark.js | 5 +++ routes/bookmark.js | 3 ++ 10 files changed, 95 insertions(+), 35 deletions(-) create mode 100644 controllers/bookmarks/reorderBookmarks.js create mode 100644 db/migrations/04_bookmarks-order.js diff --git a/controllers/apps/getAllApps.js b/controllers/apps/getAllApps.js index 04a7585..36f4eb1 100644 --- a/controllers/apps/getAllApps.js +++ b/controllers/apps/getAllApps.js @@ -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 diff --git a/controllers/bookmarks/getAllBookmarks.js b/controllers/bookmarks/getAllBookmarks.js index aece14b..25af9d8 100644 --- a/controllers/bookmarks/getAllBookmarks.js +++ b/controllers/bookmarks/getAllBookmarks.js @@ -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, }); diff --git a/controllers/bookmarks/index.js b/controllers/bookmarks/index.js index f1ef588..5c1bd86 100644 --- a/controllers/bookmarks/index.js +++ b/controllers/bookmarks/index.js @@ -4,4 +4,5 @@ module.exports = { getSingleBookmark: require('./getSingleBookmark'), updateBookmark: require('./updateBookmark'), deleteBookmark: require('./deleteBookmark'), + reorderBookmarks: require('./reorderBookmarks'), }; diff --git a/controllers/bookmarks/reorderBookmarks.js b/controllers/bookmarks/reorderBookmarks.js new file mode 100644 index 0000000..3ea1c35 --- /dev/null +++ b/controllers/bookmarks/reorderBookmarks.js @@ -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; diff --git a/controllers/categories/getAllCategories.js b/controllers/categories/getAllCategories.js index c42db2d..eb0eb24 100644 --- a/controllers/categories/getAllCategories.js +++ b/controllers/categories/getAllCategories.js @@ -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; diff --git a/controllers/categories/getSingleCategory.js b/controllers/categories/getSingleCategory.js index 8eb5fb2..c854627 100644 --- a/controllers/categories/getSingleCategory.js +++ b/controllers/categories/getSingleCategory.js @@ -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) { diff --git a/controllers/categories/reorderCategories.js b/controllers/categories/reorderCategories.js index 492675b..8922125 100644 --- a/controllers/categories/reorderCategories.js +++ b/controllers/categories/reorderCategories.js @@ -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 diff --git a/db/migrations/04_bookmarks-order.js b/db/migrations/04_bookmarks-order.js new file mode 100644 index 0000000..26ddd69 --- /dev/null +++ b/db/migrations/04_bookmarks-order.js @@ -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, +}; diff --git a/models/Bookmark.js b/models/Bookmark.js index 159ea28..197a632 100644 --- a/models/Bookmark.js +++ b/models/Bookmark.js @@ -25,6 +25,11 @@ const Bookmark = sequelize.define( allowNull: true, defaultValue: 1, }, + orderId: { + type: DataTypes.INTEGER, + allowNull: true, + defaultValue: null, + }, }, { tableName: 'bookmarks', diff --git a/routes/bookmark.js b/routes/bookmark.js index ea1a344..6bc96ad 100644 --- a/routes/bookmark.js +++ b/routes/bookmark.js @@ -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;