123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- 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');
- // @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({
- where: { key: 'pinCategoriesByDefault' }
- });
- let category;
- if (pinCategories) {
- if (parseInt(pinCategories.value)) {
- category = await Category.create({
- ...req.body,
- isPinned: true
- })
- } else {
- category = await Category.create(req.body);
- }
- }
- res.status(201).json({
- success: true,
- data: category
- })
- })
- // @desc Get all categories
- // @route GET /api/categories
- // @access Public
- exports.getCategories = asyncWrapper(async (req, res, next) => {
- const categories = await Category.findAll({
- include: [{
- model: Bookmark,
- as: 'bookmarks'
- }],
- order: [['name', 'ASC']]
- });
- res.status(200).json({
- success: true,
- data: categories
- })
- })
- // @desc Get single category
- // @route GET /api/categories/:id
- // @access Public
- exports.getCategory = asyncWrapper(async (req, res, next) => {
- const category = await Category.findOne({
- where: { id: req.params.id },
- include: [{
- model: Bookmark,
- as: 'bookmarks'
- }]
- });
- if (!category) {
- return next(new ErrorResponse(`Category with id of ${req.params.id} was not found`, 404))
- }
- res.status(200).json({
- success: true,
- data: category
- })
- })
- // @desc Update category
- // @route PUT /api/categories/:id
- // @access Public
- exports.updateCategory = asyncWrapper(async (req, res, next) => {
- let category = await Category.findOne({
- where: { id: req.params.id }
- });
- if (!category) {
- return next(new ErrorResponse(`Category with id of ${req.params.id} was not found`, 404))
- }
- category = await category.update({ ...req.body });
- res.status(200).json({
- success: true,
- data: category
- })
- })
- // @desc Delete category
- // @route DELETE /api/categories/:id
- // @access Public
- exports.deleteCategory = asyncWrapper(async (req, res, next) => {
- const category = await Category.findOne({
- where: { id: req.params.id },
- include: [{
- model: Bookmark,
- as: 'bookmarks'
- }]
- });
- if (!category) {
- return next(new ErrorResponse(`Category with id of ${req.params.id} was not found`, 404))
- }
- category.bookmarks.forEach(async (bookmark) => {
- await Bookmark.destroy({
- where: { id: bookmark.id }
- })
- })
- await Category.destroy({
- where: { id: req.params.id }
- })
- res.status(200).json({
- success: true,
- data: {}
- })
- })
|