Config model and controllers
This commit is contained in:
parent
873faa6c97
commit
3fc3d07598
5 changed files with 144 additions and 1 deletions
93
controllers/config.js
Normal file
93
controllers/config.js
Normal file
|
@ -0,0 +1,93 @@
|
|||
const asyncWrapper = require('../middleware/asyncWrapper');
|
||||
const ErrorResponse = require('../utils/ErrorResponse');
|
||||
const Config = require('../models/Config');
|
||||
|
||||
// @desc Insert new key:value pair
|
||||
// @route POST /api/config
|
||||
// @access Public
|
||||
exports.createPair = asyncWrapper(async (req, res, next) => {
|
||||
const pair = await Config.create(req.body);
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: pair
|
||||
})
|
||||
})
|
||||
|
||||
// @desc Get all key:value pairs
|
||||
// @route GET /api/config
|
||||
// @access Public
|
||||
exports.getAllPairs = asyncWrapper(async (req, res, next) => {
|
||||
const pairs = await Config.findAll();
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: pairs
|
||||
})
|
||||
})
|
||||
|
||||
// @desc Get single key:value pair
|
||||
// @route GET /api/config/:key
|
||||
// @access Public
|
||||
exports.getSinglePair = asyncWrapper(async (req, res, next) => {
|
||||
const pair = await Config.findOne({
|
||||
where: { key: req.params.key }
|
||||
});
|
||||
|
||||
if (!pair) {
|
||||
return next(new ErrorResponse(`Key ${req.params.key} was not found`, 404));
|
||||
}
|
||||
|
||||
res.status(201).json({
|
||||
success: true,
|
||||
data: pair
|
||||
})
|
||||
})
|
||||
|
||||
// @desc Update value
|
||||
// @route PUT /api/config/:key
|
||||
// @access Public
|
||||
exports.updateValue = asyncWrapper(async (req, res, next) => {
|
||||
let pair = await Config.findOne({
|
||||
where: { key: req.params.key }
|
||||
});
|
||||
|
||||
if (!pair) {
|
||||
return next(new ErrorResponse(`Key ${req.params.key} was not found`, 404));
|
||||
}
|
||||
|
||||
if (pair.isLocked) {
|
||||
return next(new ErrorResponse(`Value of key ${req.params.key} is locked and can not be changed`, 400));
|
||||
}
|
||||
|
||||
pair = await pair.update({ ...req.body });
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: pair
|
||||
})
|
||||
})
|
||||
|
||||
// @desc Delete key:value pair
|
||||
// @route DELETE /api/config/:key
|
||||
// @access Public
|
||||
exports.deletePair = asyncWrapper(async (req, res, next) => {
|
||||
const pair = await Config.findOne({
|
||||
where: { key: req.params.key }
|
||||
});
|
||||
|
||||
if (!pair) {
|
||||
return next(new ErrorResponse(`Key ${req.params.key} was not found`, 404));
|
||||
}
|
||||
|
||||
if (pair.isLocked) {
|
||||
return next(new ErrorResponse(`Value of key ${req.params.key} is locked and can not be deleted`, 400));
|
||||
}
|
||||
|
||||
await pair.destroy();
|
||||
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
data: {}
|
||||
})
|
||||
})
|
|
@ -1,4 +1,4 @@
|
|||
const { Sequelize, DataTypes } = require('sequelize');
|
||||
const { DataTypes } = require('sequelize');
|
||||
const { sequelize } = require('../db');
|
||||
|
||||
const App = sequelize.define('App', {
|
||||
|
|
26
models/Config.js
Normal file
26
models/Config.js
Normal file
|
@ -0,0 +1,26 @@
|
|||
const { DataTypes } = require('sequelize');
|
||||
const { sequelize } = require('../db');
|
||||
|
||||
const Config = sequelize.define('Config', {
|
||||
key: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false,
|
||||
unique: true
|
||||
},
|
||||
value: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
valueType: {
|
||||
type: DataTypes.STRING,
|
||||
allowNull: false
|
||||
},
|
||||
isLocked: {
|
||||
type: DataTypes.BOOLEAN,
|
||||
defaultValue: false
|
||||
}
|
||||
}, {
|
||||
freezeTableName: true
|
||||
});
|
||||
|
||||
module.exports = Config;
|
23
routes/config.js
Normal file
23
routes/config.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
const express = require('express');
|
||||
const router = express.Router();
|
||||
|
||||
const {
|
||||
createPair,
|
||||
getAllPairs,
|
||||
getSinglePair,
|
||||
updateValue,
|
||||
deletePair
|
||||
} = require('../controllers/config');
|
||||
|
||||
router
|
||||
.route('')
|
||||
.post(createPair)
|
||||
.get(getAllPairs);
|
||||
|
||||
router
|
||||
.route('/:key')
|
||||
.get(getSinglePair)
|
||||
.put(updateValue)
|
||||
.delete(deletePair);
|
||||
|
||||
module.exports = router;
|
|
@ -18,6 +18,7 @@ app.use(express.json());
|
|||
|
||||
// Link controllers with routes
|
||||
app.use('/api/apps', require('./routes/apps'));
|
||||
app.use('/api/config', require('./routes/config'));
|
||||
|
||||
// Custom error handler
|
||||
app.use(errorHandler);
|
||||
|
|
Loading…
Reference in a new issue