mirror of
https://github.com/pawelmalak/flame.git
synced 2025-08-02 17:35:17 +02:00
Merge branch 'master' of https://github.com/pawelmalak/flame into merge_upstream_2020-12-06
This commit is contained in:
commit
021bd4e85a
266 changed files with 13470 additions and 7067 deletions
22
controllers/categories/createCategory.js
Normal file
22
controllers/categories/createCategory.js
Normal file
|
@ -0,0 +1,22 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Category = require('../../models/Category');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
// @desc Create new category
|
||||
// @route POST /api/categories
|
||||
// @access Public
|
||||
const createCategory = asyncWrapper(async (req, res, next) => {
|
||||
const { pinCategoriesByDefault: pinCategories } = await loadConfig();
|
||||
|
||||
const category = await Category.create({
|
||||
...req.body,
|
||||
isPinned: pinCategories,
|
||||
});
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: category,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = createCategory;
|
56
controllers/categories/deleteCategory.js
Normal file
56
controllers/categories/deleteCategory.js
Normal file
|
@ -0,0 +1,56 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const Category = require('../../models/Category');
|
||||
const App = require('../../models/App');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
|
||||
// @desc Delete category
|
||||
// @route DELETE /api/categories/:id
|
||||
// @access Public
|
||||
const deleteCategory = asyncWrapper(async (req, res, next) => {
|
||||
const category = await Category.findOne({
|
||||
where: { id: req.params.id },
|
||||
include: [
|
||||
{
|
||||
model: App,
|
||||
as: 'apps',
|
||||
},
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
if (!category) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
`Category with id of ${req.params.id} was not found`,
|
||||
404
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
category.apps.forEach(async (app) => {
|
||||
await App.destroy({
|
||||
where: { id: app.id },
|
||||
});
|
||||
});
|
||||
|
||||
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: {},
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = deleteCategory;
|
66
controllers/categories/getAllCategories.js
Normal file
66
controllers/categories/getAllCategories.js
Normal file
|
@ -0,0 +1,66 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Category = require('../../models/Category');
|
||||
const App = require('../../models/App');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
const { Sequelize } = require('sequelize');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
// @desc Get all categories
|
||||
// @route GET /api/categories
|
||||
// @access Public
|
||||
const getAllCategories = asyncWrapper(async (req, res, next) => {
|
||||
const { useOrdering: orderType } = await loadConfig();
|
||||
|
||||
let categories;
|
||||
let output;
|
||||
|
||||
// categories visibility
|
||||
const where = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const order =
|
||||
orderType == 'name'
|
||||
? [
|
||||
[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC'],
|
||||
[Sequelize.fn('lower', Sequelize.col('apps.name')), 'ASC'],
|
||||
[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC'],
|
||||
]
|
||||
: [
|
||||
[orderType, 'ASC'],
|
||||
[{ model: App, as: 'apps' }, orderType, 'ASC'],
|
||||
[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC'],
|
||||
];
|
||||
|
||||
categories = categories = await Category.findAll({
|
||||
include: [
|
||||
{
|
||||
model: App,
|
||||
as: 'apps',
|
||||
},
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
},
|
||||
],
|
||||
order,
|
||||
where,
|
||||
});
|
||||
|
||||
if (req.isAuthenticated) {
|
||||
output = categories;
|
||||
} else {
|
||||
// filter out private apps/bookmarks
|
||||
output = categories.map((c) => c.get({ plain: true }));
|
||||
output = output.map((c) => ({
|
||||
...c,
|
||||
apps: c.apps.filter((b) => b.isPublic),
|
||||
bookmarks: c.bookmarks.filter((b) => b.isPublic),
|
||||
}));
|
||||
}
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: output,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getAllCategories;
|
60
controllers/categories/getSingleCategory.js
Normal file
60
controllers/categories/getSingleCategory.js
Normal file
|
@ -0,0 +1,60 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const Category = require('../../models/Category');
|
||||
const App = require('../../models/App');
|
||||
const Bookmark = require('../../models/Bookmark');
|
||||
const { Sequelize } = require('sequelize');
|
||||
const loadConfig = require('../../utils/loadConfig');
|
||||
|
||||
// @desc Get single category
|
||||
// @route GET /api/categories/:id
|
||||
// @access Public
|
||||
const getSingleCategory = asyncWrapper(async (req, res, next) => {
|
||||
const { useOrdering: orderType } = await loadConfig();
|
||||
|
||||
const visibility = req.isAuthenticated ? {} : { isPublic: true };
|
||||
|
||||
const order =
|
||||
orderType == 'name'
|
||||
? [
|
||||
[Sequelize.fn('lower', Sequelize.col('apps.name')), 'ASC'],
|
||||
[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']
|
||||
]
|
||||
: [
|
||||
[{ model: App, as: 'apps' }, orderType, 'ASC']
|
||||
[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']
|
||||
];
|
||||
|
||||
const category = await Category.findOne({
|
||||
where: { id: req.params.id, ...visibility },
|
||||
include: [
|
||||
{
|
||||
model: App,
|
||||
as: 'apps',
|
||||
where: visibility,
|
||||
},
|
||||
{
|
||||
model: Bookmark,
|
||||
as: 'bookmarks',
|
||||
where: visibility,
|
||||
},
|
||||
],
|
||||
order,
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = getSingleCategory;
|
8
controllers/categories/index.js
Normal file
8
controllers/categories/index.js
Normal file
|
@ -0,0 +1,8 @@
|
|||
module.exports = {
|
||||
createCategory: require('./createCategory'),
|
||||
getAllCategories: require('./getAllCategories'),
|
||||
getSingleCategory: require('./getSingleCategory'),
|
||||
updateCategory: require('./updateCategory'),
|
||||
deleteCategory: require('./deleteCategory'),
|
||||
reorderCategories: require('./reorderCategories'),
|
||||
};
|
23
controllers/categories/reorderCategories.js
Normal file
23
controllers/categories/reorderCategories.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const Category = require('../../models/Category');
|
||||
|
||||
// @desc Reorder categories
|
||||
// @route PUT /api/categories/0/reorder
|
||||
// @access Public
|
||||
const reorderCategories = asyncWrapper(async (req, res, next) => {
|
||||
req.body.categories.forEach(async ({ id, orderId }) => {
|
||||
await Category.update(
|
||||
{ orderId },
|
||||
{
|
||||
where: { id },
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {},
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = reorderCategories;
|
30
controllers/categories/updateCategory.js
Normal file
30
controllers/categories/updateCategory.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const Category = require('../../models/Category');
|
||||
|
||||
// @desc Update category
|
||||
// @route PUT /api/categories/:id
|
||||
// @access Public
|
||||
const 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,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = updateCategory;
|
Loading…
Add table
Add a link
Reference in a new issue