1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-27 23:09:35 +02:00
flame/controllers/bookmark.js

149 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-05-23 17:18:04 +02:00
const asyncWrapper = require('../middleware/asyncWrapper');
const ErrorResponse = require('../utils/ErrorResponse');
const Bookmark = require('../models/Bookmark');
2021-06-26 16:22:54 -04:00
const Config = require('../models/Config');
const { Sequelize } = require('sequelize');
2021-05-23 17:18:04 +02:00
// @desc Create new bookmark
// @route POST /api/bookmarks
// @access Public
exports.createBookmark = asyncWrapper(async (req, res, next) => {
2021-06-26 16:22:54 -04:00
const pinBookmarks = await Config.findOne({
where: { key: 'pinBookmarksByDefault' }
});
2021-07-28 12:36:03 +02:00
let bookmark;
let _body = {
...req.body,
2021-10-04 12:11:41 +02:00
categoryId: parseInt(req.body.categoryId),
2021-06-26 16:22:54 -04:00
isPinned = (pinBookmarks && parseInt(pinBookmarks.value)),
2021-07-28 12:36:03 +02:00
};
if (req.file) {
_body.icon = req.file.filename;
}
bookmark = await Bookmark.create(_body);
2021-05-23 17:18:04 +02:00
res.status(201).json({
success: true,
2021-10-04 12:11:41 +02:00
data: bookmark,
});
});
2021-05-23 17:18:04 +02:00
// @desc Get all bookmarks
// @route GET /api/bookmarks
// @access Public
exports.getBookmarks = asyncWrapper(async (req, res, next) => {
2021-06-26 16:22:54 -04:00
// Get config from database
const useOrdering = await Config.findOne({
where: { key: 'useOrdering' }
2021-05-24 11:51:05 +02:00
});
2021-05-23 17:18:04 +02:00
2021-06-26 16:22:54 -04:00
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' ]]
});
}
2021-05-23 17:18:04 +02:00
res.status(200).json({
success: true,
2021-10-04 12:11:41 +02:00
data: bookmarks,
});
});
2021-05-23 17:18:04 +02:00
// @desc Get single bookmark
// @route GET /api/bookmarks/:id
// @access Public
exports.getBookmark = asyncWrapper(async (req, res, next) => {
const bookmark = await Bookmark.findOne({
2021-10-04 12:11:41 +02:00
where: { id: req.params.id },
2021-05-23 17:18:04 +02:00
});
if (!bookmark) {
2021-10-04 12:11:41 +02:00
return next(
new ErrorResponse(
`Bookmark with id of ${req.params.id} was not found`,
404
)
);
2021-05-23 17:18:04 +02:00
}
res.status(200).json({
success: true,
2021-10-04 12:11:41 +02:00
data: bookmark,
});
});
2021-05-23 17:18:04 +02:00
// @desc Update bookmark
// @route PUT /api/bookmarks/:id
// @access Public
exports.updateBookmark = asyncWrapper(async (req, res, next) => {
let bookmark = await Bookmark.findOne({
2021-10-04 12:11:41 +02:00
where: { id: req.params.id },
2021-05-23 17:18:04 +02:00
});
if (!bookmark) {
2021-10-04 12:11:41 +02:00
return next(
new ErrorResponse(
`Bookmark with id of ${req.params.id} was not found`,
404
)
);
2021-05-23 17:18:04 +02:00
}
2021-07-28 12:36:03 +02:00
let _body = {
...req.body,
2021-10-04 12:11:41 +02:00
categoryId: parseInt(req.body.categoryId),
2021-07-28 12:36:03 +02:00
};
if (req.file) {
_body.icon = req.file.filename;
}
bookmark = await bookmark.update(_body);
2021-05-23 17:18:04 +02:00
res.status(200).json({
success: true,
2021-10-04 12:11:41 +02:00
data: bookmark,
});
});
2021-05-23 17:18:04 +02:00
// @desc Delete bookmark
// @route DELETE /api/bookmarks/:id
// @access Public
exports.deleteBookmark = asyncWrapper(async (req, res, next) => {
await Bookmark.destroy({
2021-06-26 16:22:54 -04:00
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 }
})
})
2021-05-23 17:18:04 +02:00
res.status(200).json({
success: true,
2021-10-04 12:11:41 +02:00
data: {},
});
});