API routes to get and add themes
This commit is contained in:
parent
48e28b9abd
commit
b8af178cbf
6 changed files with 67 additions and 2 deletions
1
api.js
1
api.js
|
@ -22,6 +22,7 @@ api.use('/api/categories', require('./routes/category'));
|
|||
api.use('/api/bookmarks', require('./routes/bookmark'));
|
||||
api.use('/api/queries', require('./routes/queries'));
|
||||
api.use('/api/auth', require('./routes/auth'));
|
||||
api.use('/api/themes', require('./routes/themes'));
|
||||
|
||||
// Custom error handler
|
||||
api.use(errorHandler);
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const File = require('../../utils/File');
|
||||
|
||||
// @desc Add custom search query
|
||||
|
@ -8,6 +9,12 @@ const addQuery = asyncWrapper(async (req, res, next) => {
|
|||
const file = new File('data/customQueries.json');
|
||||
let content = JSON.parse(file.read());
|
||||
|
||||
const prefixes = content.queries.map((q) => q.prefix);
|
||||
|
||||
if (prefixes.includes(req.body.prefix)) {
|
||||
return next(new ErrorResponse('Prefix must be unique', 400));
|
||||
}
|
||||
|
||||
// Add new query
|
||||
content.queries.push(req.body);
|
||||
file.write(content, true);
|
||||
|
|
28
controllers/themes/addTheme.js
Normal file
28
controllers/themes/addTheme.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
const asyncWrapper = require('../../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../../utils/ErrorResponse');
|
||||
const File = require('../../utils/File');
|
||||
|
||||
// @desc Create new theme
|
||||
// @route POST /api/themes
|
||||
// @access Private
|
||||
const addTheme = asyncWrapper(async (req, res, next) => {
|
||||
const file = new File('data/themes.json');
|
||||
let content = JSON.parse(file.read());
|
||||
|
||||
const themeNames = content.themes.map((t) => t.name);
|
||||
|
||||
if (themeNames.includes(req.body.name)) {
|
||||
return next(new ErrorResponse('Name must be unique', 400));
|
||||
}
|
||||
|
||||
// Add new theme
|
||||
content.themes.push(req.body);
|
||||
file.write(content, true);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: req.body,
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = addTheme;
|
|
@ -1,3 +1,4 @@
|
|||
module.exports = {
|
||||
getThemes: require('./getThemes'),
|
||||
addTheme: require('./addTheme'),
|
||||
};
|
||||
|
|
|
@ -2,7 +2,7 @@ const express = require('express');
|
|||
const router = express.Router();
|
||||
|
||||
// middleware
|
||||
const { auth, requireAuth } = require('../middleware');
|
||||
const { auth, requireAuth, requireBody } = require('../middleware');
|
||||
|
||||
const {
|
||||
getQueries,
|
||||
|
@ -11,7 +11,16 @@ const {
|
|||
updateQuery,
|
||||
} = require('../controllers/queries/');
|
||||
|
||||
router.route('/').post(auth, requireAuth, addQuery).get(getQueries);
|
||||
router
|
||||
.route('/')
|
||||
.post(
|
||||
auth,
|
||||
requireAuth,
|
||||
requireBody(['name', 'prefix', 'template']),
|
||||
addQuery
|
||||
)
|
||||
.get(getQueries);
|
||||
|
||||
router
|
||||
.route('/:prefix')
|
||||
.delete(auth, requireAuth, deleteQuery)
|
||||
|
|
19
routes/themes.js
Normal file
19
routes/themes.js
Normal file
|
@ -0,0 +1,19 @@
|
|||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
// middleware
|
||||
const { auth, requireAuth, requireBody } = require('../middleware');
|
||||
|
||||
const { getThemes, addTheme } = require('../controllers/themes/');
|
||||
|
||||
router
|
||||
.route('/')
|
||||
.get(getThemes)
|
||||
.post(
|
||||
auth,
|
||||
requireAuth,
|
||||
requireBody(['name', 'colors', 'isCustom']),
|
||||
addTheme
|
||||
);
|
||||
|
||||
module.exports = router;
|
Loading…
Reference in a new issue