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:
Paweł Malak 2021-11-22 16:45:59 +01:00
parent dfdd49cf4a
commit f1f7b698f8
10 changed files with 95 additions and 35 deletions

View file

@ -28,17 +28,15 @@ const getAllApps = asyncWrapper(async (req, res, next) => {
// apps visibility
const where = req.isAuthenticated ? {} : { isPublic: true };
if (orderType == 'name') {
const order =
orderType == 'name'
? [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']]
: [[orderType, 'ASC']];
apps = await App.findAll({
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
order,
where,
});
} else {
apps = await App.findAll({
order: [[orderType, 'ASC']],
where,
});
}
if (process.env.NODE_ENV === 'production') {
// Set header to fetch containers info every time

View file

@ -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,
});

View file

@ -4,4 +4,5 @@ module.exports = {
getSingleBookmark: require('./getSingleBookmark'),
updateBookmark: require('./updateBookmark'),
deleteBookmark: require('./deleteBookmark'),
reorderBookmarks: require('./reorderBookmarks'),
};

View 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;

View file

@ -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({
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: [[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC']],
order,
where,
});
} else {
categories = await Category.findAll({
include: [
{
model: Bookmark,
as: 'bookmarks',
},
],
order: [[orderType, 'ASC']],
where,
});
}
if (req.isAuthenticated) {
output = categories;

View file

@ -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) {

View file

@ -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

View 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,
};

View file

@ -25,6 +25,11 @@ const Bookmark = sequelize.define(
allowNull: true,
defaultValue: 1,
},
orderId: {
type: DataTypes.INTEGER,
allowNull: true,
defaultValue: null,
},
},
{
tableName: 'bookmarks',

View file

@ -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;