mirror of
https://github.com/pawelmalak/flame.git
synced 2026-03-07 04:53:12 +08:00
API routes to get and add themes
This commit is contained in:
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'),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user