Skip to content

Commit f1f7b69

Browse files
committed
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
1 parent dfdd49c commit f1f7b69

File tree

10 files changed

+95
-35
lines changed

10 files changed

+95
-35
lines changed

controllers/apps/getAllApps.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,15 @@ const getAllApps = asyncWrapper(async (req, res, next) => {
2828
// apps visibility
2929
const where = req.isAuthenticated ? {} : { isPublic: true };
3030

31-
if (orderType == 'name') {
32-
apps = await App.findAll({
33-
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
34-
where,
35-
});
36-
} else {
37-
apps = await App.findAll({
38-
order: [[orderType, 'ASC']],
39-
where,
40-
});
41-
}
31+
const order =
32+
orderType == 'name'
33+
? [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']]
34+
: [[orderType, 'ASC']];
35+
36+
apps = await App.findAll({
37+
order,
38+
where,
39+
});
4240

4341
if (process.env.NODE_ENV === 'production') {
4442
// Set header to fetch containers info every time

controllers/bookmarks/getAllBookmarks.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
const asyncWrapper = require('../../middleware/asyncWrapper');
22
const Bookmark = require('../../models/Bookmark');
33
const { Sequelize } = require('sequelize');
4+
const loadConfig = require('../../utils/loadConfig');
45

56
// @desc Get all bookmarks
67
// @route GET /api/bookmarks
78
// @access Public
89
const getAllBookmarks = asyncWrapper(async (req, res, next) => {
10+
const { useOrdering: orderType } = await loadConfig();
11+
912
// bookmarks visibility
1013
const where = req.isAuthenticated ? {} : { isPublic: true };
1114

15+
const order =
16+
orderType == 'name'
17+
? [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']]
18+
: [[orderType, 'ASC']];
19+
1220
const bookmarks = await Bookmark.findAll({
13-
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
21+
order,
1422
where,
1523
});
1624

controllers/bookmarks/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ module.exports = {
44
getSingleBookmark: require('./getSingleBookmark'),
55
updateBookmark: require('./updateBookmark'),
66
deleteBookmark: require('./deleteBookmark'),
7+
reorderBookmarks: require('./reorderBookmarks'),
78
};
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const asyncWrapper = require('../../middleware/asyncWrapper');
2+
const Bookmark = require('../../models/Bookmark');
3+
4+
// @desc Reorder bookmarks
5+
// @route PUT /api/bookmarks/0/reorder
6+
// @access Public
7+
const reorderBookmarks = asyncWrapper(async (req, res, next) => {
8+
req.body.bookmarks.forEach(async ({ id, orderId }) => {
9+
await Bookmark.update(
10+
{ orderId },
11+
{
12+
where: { id },
13+
}
14+
);
15+
});
16+
17+
res.status(200).json({
18+
success: true,
19+
data: {},
20+
});
21+
});
22+
23+
module.exports = reorderBookmarks;

controllers/categories/getAllCategories.js

+15-23
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,21 @@ const getAllCategories = asyncWrapper(async (req, res, next) => {
1616
// categories visibility
1717
const where = req.isAuthenticated ? {} : { isPublic: true };
1818

19-
if (orderType == 'name') {
20-
categories = await Category.findAll({
21-
include: [
22-
{
23-
model: Bookmark,
24-
as: 'bookmarks',
25-
},
26-
],
27-
order: [[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC']],
28-
where,
29-
});
30-
} else {
31-
categories = await Category.findAll({
32-
include: [
33-
{
34-
model: Bookmark,
35-
as: 'bookmarks',
36-
},
37-
],
38-
order: [[orderType, 'ASC']],
39-
where,
40-
});
41-
}
19+
const order =
20+
orderType == 'name'
21+
? [[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']]
22+
: [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']];
23+
24+
categories = categories = await Category.findAll({
25+
include: [
26+
{
27+
model: Bookmark,
28+
as: 'bookmarks',
29+
},
30+
],
31+
order,
32+
where,
33+
});
4234

4335
if (req.isAuthenticated) {
4436
output = categories;

controllers/categories/getSingleCategory.js

+10
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@ const asyncWrapper = require('../../middleware/asyncWrapper');
22
const ErrorResponse = require('../../utils/ErrorResponse');
33
const Category = require('../../models/Category');
44
const Bookmark = require('../../models/Bookmark');
5+
const { Sequelize } = require('sequelize');
6+
const loadConfig = require('../../utils/loadConfig');
57

68
// @desc Get single category
79
// @route GET /api/categories/:id
810
// @access Public
911
const getSingleCategory = asyncWrapper(async (req, res, next) => {
12+
const { useOrdering: orderType } = await loadConfig();
13+
1014
const visibility = req.isAuthenticated ? {} : { isPublic: true };
1115

16+
const order =
17+
orderType == 'name'
18+
? [[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']]
19+
: [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']];
20+
1221
const category = await Category.findOne({
1322
where: { id: req.params.id, ...visibility },
1423
include: [
@@ -18,6 +27,7 @@ const getSingleCategory = asyncWrapper(async (req, res, next) => {
1827
where: visibility,
1928
},
2029
],
30+
order,
2131
});
2232

2333
if (!category) {

controllers/categories/reorderCategories.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const asyncWrapper = require('../../middleware/asyncWrapper');
22
const Category = require('../../models/Category');
3+
34
// @desc Reorder categories
45
// @route PUT /api/categories/0/reorder
56
// @access Public

db/migrations/04_bookmarks-order.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const { DataTypes } = require('sequelize');
2+
const { INTEGER } = DataTypes;
3+
4+
const up = async (query) => {
5+
await query.addColumn('bookmarks', 'orderId', {
6+
type: INTEGER,
7+
allowNull: true,
8+
defaultValue: null,
9+
});
10+
};
11+
12+
const down = async (query) => {
13+
await query.removeColumn('bookmarks', 'orderId');
14+
};
15+
16+
module.exports = {
17+
up,
18+
down,
19+
};

models/Bookmark.js

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ const Bookmark = sequelize.define(
2525
allowNull: true,
2626
defaultValue: 1,
2727
},
28+
orderId: {
29+
type: DataTypes.INTEGER,
30+
allowNull: true,
31+
defaultValue: null,
32+
},
2833
},
2934
{
3035
tableName: 'bookmarks',

routes/bookmark.js

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const {
1010
getSingleBookmark,
1111
updateBookmark,
1212
deleteBookmark,
13+
reorderBookmarks,
1314
} = require('../controllers/bookmarks');
1415

1516
router
@@ -23,4 +24,6 @@ router
2324
.put(auth, requireAuth, upload, updateBookmark)
2425
.delete(auth, requireAuth, deleteBookmark);
2526

27+
router.route('/0/reorder').put(auth, requireAuth, reorderBookmarks);
28+
2629
module.exports = router;

0 commit comments

Comments
 (0)