1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-07-19 11:39:36 +02:00

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

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;