API routes to get and add themes

This commit is contained in:
Paweł Malak 2022-03-24 14:48:10 +01:00
parent 48e28b9abd
commit b8af178cbf
6 changed files with 67 additions and 2 deletions

1
api.js
View file

@ -22,6 +22,7 @@ api.use('/api/categories', require('./routes/category'));
api.use('/api/bookmarks', require('./routes/bookmark')); api.use('/api/bookmarks', require('./routes/bookmark'));
api.use('/api/queries', require('./routes/queries')); api.use('/api/queries', require('./routes/queries'));
api.use('/api/auth', require('./routes/auth')); api.use('/api/auth', require('./routes/auth'));
api.use('/api/themes', require('./routes/themes'));
// Custom error handler // Custom error handler
api.use(errorHandler); api.use(errorHandler);

View file

@ -1,4 +1,5 @@
const asyncWrapper = require('../../middleware/asyncWrapper'); const asyncWrapper = require('../../middleware/asyncWrapper');
const ErrorResponse = require('../../utils/ErrorResponse');
const File = require('../../utils/File'); const File = require('../../utils/File');
// @desc Add custom search query // @desc Add custom search query
@ -8,6 +9,12 @@ const addQuery = asyncWrapper(async (req, res, next) => {
const file = new File('data/customQueries.json'); const file = new File('data/customQueries.json');
let content = JSON.parse(file.read()); 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 // Add new query
content.queries.push(req.body); content.queries.push(req.body);
file.write(content, true); file.write(content, true);

View 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;

View file

@ -1,3 +1,4 @@
module.exports = { module.exports = {
getThemes: require('./getThemes'), getThemes: require('./getThemes'),
addTheme: require('./addTheme'),
}; };

View file

@ -2,7 +2,7 @@ const express = require('express');
const router = express.Router(); const router = express.Router();
// middleware // middleware
const { auth, requireAuth } = require('../middleware'); const { auth, requireAuth, requireBody } = require('../middleware');
const { const {
getQueries, getQueries,
@ -11,7 +11,16 @@ const {
updateQuery, updateQuery,
} = require('../controllers/queries/'); } = require('../controllers/queries/');
router.route('/').post(auth, requireAuth, addQuery).get(getQueries); router
.route('/')
.post(
auth,
requireAuth,
requireBody(['name', 'prefix', 'template']),
addQuery
)
.get(getQueries);
router router
.route('/:prefix') .route('/:prefix')
.delete(auth, requireAuth, deleteQuery) .delete(auth, requireAuth, deleteQuery)

19
routes/themes.js Normal file
View 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;