2021-05-15 16:33:59 +00:00
|
|
|
const asyncWrapper = require('../middleware/asyncWrapper');
|
|
|
|
const ErrorResponse = require('../utils/ErrorResponse');
|
|
|
|
const Config = require('../models/Config');
|
2021-05-27 10:30:09 +00:00
|
|
|
const { Op } = require('sequelize');
|
2021-05-15 16:33:59 +00:00
|
|
|
|
|
|
|
// @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
|
2021-05-27 10:30:09 +00:00
|
|
|
// @route GET /api/config?keys=foo,bar,baz
|
2021-05-15 16:33:59 +00:00
|
|
|
// @access Public
|
|
|
|
exports.getAllPairs = asyncWrapper(async (req, res, next) => {
|
2021-05-27 10:30:09 +00:00
|
|
|
let pairs;
|
|
|
|
|
|
|
|
if (req.query.keys) {
|
|
|
|
// Check for specific keys to get in a single query
|
|
|
|
const keys = req.query.keys
|
|
|
|
.split(',')
|
|
|
|
.map((key) => { return { key } });
|
|
|
|
|
|
|
|
pairs = await Config.findAll({
|
|
|
|
where: {
|
|
|
|
[Op.or]: keys
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Else get all
|
|
|
|
pairs = await Config.findAll();
|
|
|
|
}
|
2021-05-15 16:33:59 +00:00
|
|
|
|
2021-05-17 16:26:01 +00:00
|
|
|
res.status(200).json({
|
2021-05-15 16:33:59 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-05-17 16:26:01 +00:00
|
|
|
res.status(200).json({
|
2021-05-15 16:33:59 +00:00
|
|
|
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: {}
|
|
|
|
})
|
|
|
|
})
|