mirror of
https://github.com/pawelmalak/flame.git
synced 2025-08-02 01:15:17 +02:00
add bookmark sorting (#3)
This commit is contained in:
parent
31cf2bc5ad
commit
2c659d1e51
21 changed files with 1021 additions and 437 deletions
|
@ -1,17 +1,23 @@
|
|||
const asyncWrapper = require('../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../utils/ErrorResponse');
|
||||
const Bookmark = require('../models/Bookmark');
|
||||
const Config = require('../models/Config');
|
||||
const { Sequelize } = require('sequelize');
|
||||
|
||||
// @desc Create new bookmark
|
||||
// @route POST /api/bookmarks
|
||||
// @access Public
|
||||
exports.createBookmark = asyncWrapper(async (req, res, next) => {
|
||||
const pinBookmarks = await Config.findOne({
|
||||
where: { key: 'pinBookmarksByDefault' }
|
||||
});
|
||||
|
||||
let bookmark;
|
||||
|
||||
let _body = {
|
||||
...req.body,
|
||||
categoryId: parseInt(req.body.categoryId),
|
||||
isPinned = (pinBookmarks && parseInt(pinBookmarks.value)),
|
||||
};
|
||||
|
||||
if (req.file) {
|
||||
|
@ -30,10 +36,24 @@ exports.createBookmark = asyncWrapper(async (req, res, next) => {
|
|||
// @route GET /api/bookmarks
|
||||
// @access Public
|
||||
exports.getBookmarks = asyncWrapper(async (req, res, next) => {
|
||||
const bookmarks = await Bookmark.findAll({
|
||||
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
|
||||
// Get config from database
|
||||
const useOrdering = await Config.findOne({
|
||||
where: { key: 'useOrdering' }
|
||||
});
|
||||
|
||||
const orderType = useOrdering ? useOrdering.value : 'createdAt';
|
||||
let bookmarks;
|
||||
|
||||
if (orderType == 'name') {
|
||||
bookmarks = await Bookmark.findAll({
|
||||
order: [[ Sequelize.fn('lower', Sequelize.col('name')), 'ASC' ]]
|
||||
});
|
||||
} else {
|
||||
bookmarks = await Bookmark.findAll({
|
||||
order: [[ orderType, 'ASC' ]]
|
||||
});
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: bookmarks,
|
||||
|
@ -102,8 +122,24 @@ exports.updateBookmark = asyncWrapper(async (req, res, next) => {
|
|||
// @access Public
|
||||
exports.deleteBookmark = asyncWrapper(async (req, res, next) => {
|
||||
await Bookmark.destroy({
|
||||
where: { id: req.params.id },
|
||||
});
|
||||
where: { id: req.params.id }
|
||||
})
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {}
|
||||
})
|
||||
})
|
||||
|
||||
// @desc Reorder bookmarks
|
||||
// @route PUT /api/bookmarks/0/reorder
|
||||
// @access Public
|
||||
exports.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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue