2021-05-23 18:18:04 +03:00
|
|
|
const asyncWrapper = require('../middleware/asyncWrapper');
|
|
|
|
const ErrorResponse = require('../utils/ErrorResponse');
|
|
|
|
const Bookmark = require('../models/Bookmark');
|
2021-06-15 13:36:23 +03:00
|
|
|
const { Sequelize } = require('sequelize');
|
2021-05-23 18:18:04 +03:00
|
|
|
|
|
|
|
// @desc Create new bookmark
|
|
|
|
// @route POST /api/bookmarks
|
|
|
|
// @access Public
|
|
|
|
exports.createBookmark = asyncWrapper(async (req, res, next) => {
|
2021-07-28 13:36:03 +03:00
|
|
|
let bookmark;
|
|
|
|
|
|
|
|
let _body = {
|
|
|
|
...req.body,
|
2021-10-04 13:11:41 +03:00
|
|
|
categoryId: parseInt(req.body.categoryId),
|
2021-07-28 13:36:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if (req.file) {
|
|
|
|
_body.icon = req.file.filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
bookmark = await Bookmark.create(_body);
|
2021-05-23 18:18:04 +03:00
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
success: true,
|
2021-10-04 13:11:41 +03:00
|
|
|
data: bookmark,
|
|
|
|
});
|
|
|
|
});
|
2021-05-23 18:18:04 +03:00
|
|
|
|
|
|
|
// @desc Get all bookmarks
|
|
|
|
// @route GET /api/bookmarks
|
|
|
|
// @access Public
|
|
|
|
exports.getBookmarks = asyncWrapper(async (req, res, next) => {
|
2021-05-24 12:51:05 +03:00
|
|
|
const bookmarks = await Bookmark.findAll({
|
2021-10-04 13:11:41 +03:00
|
|
|
order: [[Sequelize.fn('lower', Sequelize.col('name')), 'ASC']],
|
2021-05-24 12:51:05 +03:00
|
|
|
});
|
2021-05-23 18:18:04 +03:00
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
2021-10-04 13:11:41 +03:00
|
|
|
data: bookmarks,
|
|
|
|
});
|
|
|
|
});
|
2021-05-23 18:18:04 +03: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 13:11:41 +03:00
|
|
|
where: { id: req.params.id },
|
2021-05-23 18:18:04 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!bookmark) {
|
2021-10-04 13:11:41 +03:00
|
|
|
return next(
|
|
|
|
new ErrorResponse(
|
|
|
|
`Bookmark with id of ${req.params.id} was not found`,
|
|
|
|
404
|
|
|
|
)
|
|
|
|
);
|
2021-05-23 18:18:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
2021-10-04 13:11:41 +03:00
|
|
|
data: bookmark,
|
|
|
|
});
|
|
|
|
});
|
2021-05-23 18:18:04 +03: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 13:11:41 +03:00
|
|
|
where: { id: req.params.id },
|
2021-05-23 18:18:04 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!bookmark) {
|
2021-10-04 13:11:41 +03:00
|
|
|
return next(
|
|
|
|
new ErrorResponse(
|
|
|
|
`Bookmark with id of ${req.params.id} was not found`,
|
|
|
|
404
|
|
|
|
)
|
|
|
|
);
|
2021-05-23 18:18:04 +03:00
|
|
|
}
|
|
|
|
|
2021-07-28 13:36:03 +03:00
|
|
|
let _body = {
|
|
|
|
...req.body,
|
2021-10-04 13:11:41 +03:00
|
|
|
categoryId: parseInt(req.body.categoryId),
|
2021-07-28 13:36:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
if (req.file) {
|
|
|
|
_body.icon = req.file.filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
bookmark = await bookmark.update(_body);
|
2021-05-23 18:18:04 +03:00
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
2021-10-04 13:11:41 +03:00
|
|
|
data: bookmark,
|
|
|
|
});
|
|
|
|
});
|
2021-05-23 18:18:04 +03:00
|
|
|
|
|
|
|
// @desc Delete bookmark
|
|
|
|
// @route DELETE /api/bookmarks/:id
|
|
|
|
// @access Public
|
|
|
|
exports.deleteBookmark = asyncWrapper(async (req, res, next) => {
|
|
|
|
await Bookmark.destroy({
|
2021-10-04 13:11:41 +03:00
|
|
|
where: { id: req.params.id },
|
2021-05-23 18:18:04 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
2021-10-04 13:11:41 +03:00
|
|
|
data: {},
|
|
|
|
});
|
|
|
|
});
|