flame/controllers/category.js

187 lines
3.9 KiB
JavaScript
Raw Normal View History

2021-05-22 20:04:34 +03:00
const asyncWrapper = require('../middleware/asyncWrapper');
const ErrorResponse = require('../utils/ErrorResponse');
const Category = require('../models/Category');
const Bookmark = require('../models/Bookmark');
const Config = require('../models/Config');
2021-10-04 13:11:41 +03:00
const { Sequelize } = require('sequelize');
2021-05-22 20:04:34 +03:00
// @desc Create new category
// @route POST /api/categories
// @access Public
exports.createCategory = asyncWrapper(async (req, res, next) => {
// Get config from database
const pinCategories = await Config.findOne({
2021-10-04 13:11:41 +03:00
where: { key: 'pinCategoriesByDefault' },
});
let category;
if (pinCategories) {
if (parseInt(pinCategories.value)) {
category = await Category.create({
...req.body,
2021-10-04 13:11:41 +03:00
isPinned: true,
});
} else {
category = await Category.create(req.body);
}
}
2021-05-22 20:04:34 +03:00
res.status(201).json({
success: true,
2021-10-04 13:11:41 +03:00
data: category,
});
});
2021-05-22 20:04:34 +03:00
// @desc Get all categories
// @route GET /api/categories
// @access Public
exports.getCategories = asyncWrapper(async (req, res, next) => {
// Get config from database
const useOrdering = await Config.findOne({
2021-10-04 13:11:41 +03:00
where: { key: 'useOrdering' },
2021-05-23 18:18:04 +03:00
});
2021-05-22 20:04:34 +03:00
const orderType = useOrdering ? useOrdering.value : 'createdAt';
let categories;
if (orderType == 'name') {
categories = await Category.findAll({
2021-10-04 13:11:41 +03:00
include: [
{
model: Bookmark,
as: 'bookmarks',
},
],
order: [[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC']],
});
} else {
categories = await Category.findAll({
2021-10-04 13:11:41 +03:00
include: [
{
model: Bookmark,
as: 'bookmarks',
},
],
order: [[orderType, 'ASC']],
});
}
2021-05-22 20:04:34 +03:00
res.status(200).json({
success: true,
2021-10-04 13:11:41 +03:00
data: categories,
});
});
2021-05-22 20:04:34 +03:00
// @desc Get single category
// @route GET /api/categories/:id
// @access Public
exports.getCategory = asyncWrapper(async (req, res, next) => {
const category = await Category.findOne({
2021-05-23 18:18:04 +03:00
where: { id: req.params.id },
2021-10-04 13:11:41 +03:00
include: [
{
model: Bookmark,
as: 'bookmarks',
},
],
2021-05-22 20:04:34 +03:00
});
if (!category) {
2021-10-04 13:11:41 +03:00
return next(
new ErrorResponse(
`Category with id of ${req.params.id} was not found`,
404
)
);
2021-05-22 20:04:34 +03:00
}
res.status(200).json({
success: true,
2021-10-04 13:11:41 +03:00
data: category,
});
});
2021-05-22 20:04:34 +03:00
// @desc Update category
// @route PUT /api/categories/:id
// @access Public
exports.updateCategory = asyncWrapper(async (req, res, next) => {
let category = await Category.findOne({
2021-10-04 13:11:41 +03:00
where: { id: req.params.id },
2021-05-22 20:04:34 +03:00
});
if (!category) {
2021-10-04 13:11:41 +03:00
return next(
new ErrorResponse(
`Category with id of ${req.params.id} was not found`,
404
)
);
2021-05-22 20:04:34 +03:00
}
category = await category.update({ ...req.body });
res.status(200).json({
success: true,
2021-10-04 13:11:41 +03:00
data: category,
});
});
2021-05-22 20:04:34 +03:00
// @desc Delete category
// @route DELETE /api/categories/:id
// @access Public
exports.deleteCategory = asyncWrapper(async (req, res, next) => {
2021-05-25 15:05:53 +03:00
const category = await Category.findOne({
where: { id: req.params.id },
2021-10-04 13:11:41 +03:00
include: [
{
model: Bookmark,
as: 'bookmarks',
},
],
2021-05-25 15:05:53 +03:00
});
if (!category) {
2021-10-04 13:11:41 +03:00
return next(
new ErrorResponse(
`Category with id of ${req.params.id} was not found`,
404
)
);
2021-05-25 15:05:53 +03:00
}
category.bookmarks.forEach(async (bookmark) => {
await Bookmark.destroy({
2021-10-04 13:11:41 +03:00
where: { id: bookmark.id },
});
});
2021-05-25 15:05:53 +03:00
2021-05-22 20:04:34 +03:00
await Category.destroy({
2021-10-04 13:11:41 +03:00
where: { id: req.params.id },
});
2021-05-22 20:04:34 +03:00
res.status(200).json({
success: true,
2021-10-04 13:11:41 +03:00
data: {},
});
});
// @desc Reorder categories
// @route PUT /api/categories/0/reorder
// @access Public
exports.reorderCategories = asyncWrapper(async (req, res, next) => {
req.body.categories.forEach(async ({ id, orderId }) => {
2021-10-04 13:11:41 +03:00
await Category.update(
{ orderId },
{
where: { id },
}
);
});
2021-05-22 20:04:34 +03:00
res.status(200).json({
success: true,
2021-10-04 13:11:41 +03:00
data: {},
});
});