1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-24 13:39:35 +02:00

Pushed version 1.6.7

This commit is contained in:
unknown 2021-10-04 12:11:41 +02:00
parent 4252457871
commit 6a6f1750b1
7 changed files with 217 additions and 162 deletions

View file

@ -3,7 +3,7 @@ const ErrorResponse = require('../utils/ErrorResponse');
const Category = require('../models/Category');
const Bookmark = require('../models/Bookmark');
const Config = require('../models/Config');
const { Sequelize } = require('sequelize')
const { Sequelize } = require('sequelize');
// @desc Create new category
// @route POST /api/categories
@ -11,7 +11,7 @@ const { Sequelize } = require('sequelize')
exports.createCategory = asyncWrapper(async (req, res, next) => {
// Get config from database
const pinCategories = await Config.findOne({
where: { key: 'pinCategoriesByDefault' }
where: { key: 'pinCategoriesByDefault' },
});
let category;
@ -20,8 +20,8 @@ exports.createCategory = asyncWrapper(async (req, res, next) => {
if (parseInt(pinCategories.value)) {
category = await Category.create({
...req.body,
isPinned: true
})
isPinned: true,
});
} else {
category = await Category.create(req.body);
}
@ -29,9 +29,9 @@ exports.createCategory = asyncWrapper(async (req, res, next) => {
res.status(201).json({
success: true,
data: category
})
})
data: category,
});
});
// @desc Get all categories
// @route GET /api/categories
@ -39,7 +39,7 @@ exports.createCategory = asyncWrapper(async (req, res, next) => {
exports.getCategories = asyncWrapper(async (req, res, next) => {
// Get config from database
const useOrdering = await Config.findOne({
where: { key: 'useOrdering' }
where: { key: 'useOrdering' },
});
const orderType = useOrdering ? useOrdering.value : 'createdAt';
@ -47,27 +47,31 @@ exports.getCategories = asyncWrapper(async (req, res, next) => {
if (orderType == 'name') {
categories = await Category.findAll({
include: [{
model: Bookmark,
as: 'bookmarks'
}],
order: [[ Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC' ]]
include: [
{
model: Bookmark,
as: 'bookmarks',
},
],
order: [[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC']],
});
} else {
categories = await Category.findAll({
include: [{
model: Bookmark,
as: 'bookmarks'
}],
order: [[ orderType, 'ASC' ]]
include: [
{
model: Bookmark,
as: 'bookmarks',
},
],
order: [[orderType, 'ASC']],
});
}
res.status(200).json({
success: true,
data: categories
})
})
data: categories,
});
});
// @desc Get single category
// @route GET /api/categories/:id
@ -75,41 +79,53 @@ exports.getCategories = asyncWrapper(async (req, res, next) => {
exports.getCategory = asyncWrapper(async (req, res, next) => {
const category = await Category.findOne({
where: { id: req.params.id },
include: [{
model: Bookmark,
as: 'bookmarks'
}]
include: [
{
model: Bookmark,
as: 'bookmarks',
},
],
});
if (!category) {
return next(new ErrorResponse(`Category with id of ${req.params.id} was not found`, 404))
return next(
new ErrorResponse(
`Category with id of ${req.params.id} was not found`,
404
)
);
}
res.status(200).json({
success: true,
data: category
})
})
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 }
where: { id: req.params.id },
});
if (!category) {
return next(new ErrorResponse(`Category with id of ${req.params.id} was not found`, 404))
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
})
})
data: category,
});
});
// @desc Delete category
// @route DELETE /api/categories/:id
@ -117,44 +133,54 @@ exports.updateCategory = asyncWrapper(async (req, res, next) => {
exports.deleteCategory = asyncWrapper(async (req, res, next) => {
const category = await Category.findOne({
where: { id: req.params.id },
include: [{
model: Bookmark,
as: 'bookmarks'
}]
include: [
{
model: Bookmark,
as: 'bookmarks',
},
],
});
if (!category) {
return next(new ErrorResponse(`Category with id of ${req.params.id} was not found`, 404))
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 }
})
})
where: { id: bookmark.id },
});
});
await Category.destroy({
where: { id: req.params.id }
})
where: { id: req.params.id },
});
res.status(200).json({
success: true,
data: {}
})
})
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 }) => {
await Category.update({ orderId }, {
where: { id }
})
})
await Category.update(
{ orderId },
{
where: { id },
}
);
});
res.status(200).json({
success: true,
data: {}
})
})
data: {},
});
});