mirror of
https://github.com/jambonz/jambonz-api-server.git
synced 2026-07-06 04:01:54 +00:00
ed51d8b13f
major merge of features from the hosted branch that was created temporarily during the initial launch of jambonz.org
110 lines
3.0 KiB
JavaScript
110 lines
3.0 KiB
JavaScript
const assert = require('assert');
|
|
const sysError = require('../error');
|
|
|
|
module.exports = decorate;
|
|
|
|
const decorators = {
|
|
'list': list,
|
|
'add': add,
|
|
'retrieve': retrieve,
|
|
'update': update,
|
|
'delete': remove
|
|
};
|
|
|
|
function decorate(router, klass, methods, preconditions = {}) {
|
|
const decs = methods && Array.isArray(methods) && methods[0] !== '*' ? methods : Object.keys(decorators);
|
|
decs.forEach((m) => {
|
|
assert(m in decorators);
|
|
decorators[m](router, klass, preconditions);
|
|
});
|
|
}
|
|
|
|
function list(router, klass, preconditions) {
|
|
router.get('/', async(req, res) => {
|
|
const logger = req.app.locals.logger;
|
|
//logger.info(`user: ${JSON.stringify(req.user)}`);
|
|
//logger.info(`scope: ${JSON.stringify(req.authInfo.scope)}`);
|
|
try {
|
|
if ('list' in preconditions) {
|
|
assert(typeof preconditions.list === 'function');
|
|
await preconditions.list(req);
|
|
}
|
|
const results = await klass.retrieveAll();
|
|
res.status(200).json(results);
|
|
} catch (err) {
|
|
sysError(logger, res, err);
|
|
}
|
|
});
|
|
}
|
|
|
|
function add(router, klass, preconditions) {
|
|
router.post('/', async(req, res) => {
|
|
const logger = req.app.locals.logger;
|
|
try {
|
|
if ('add' in preconditions) {
|
|
assert(typeof preconditions.add === 'function');
|
|
await preconditions.add(req);
|
|
}
|
|
const uuid = await klass.make(req.body);
|
|
res.status(201).json({sid: uuid});
|
|
} catch (err) {
|
|
sysError(logger, res, err);
|
|
}
|
|
});
|
|
}
|
|
|
|
function retrieve(router, klass) {
|
|
router.get('/:sid', async(req, res) => {
|
|
const logger = req.app.locals.logger;
|
|
try {
|
|
const results = await klass.retrieve(req.params.sid);
|
|
if (results.length === 0) return res.sendStatus(404);
|
|
return res.status(200).json(results[0]);
|
|
}
|
|
catch (err) {
|
|
sysError(logger, res, err);
|
|
}
|
|
});
|
|
}
|
|
|
|
function update(router, klass, preconditions) {
|
|
router.put('/:sid', async(req, res) => {
|
|
const sid = req.params.sid;
|
|
const logger = req.app.locals.logger;
|
|
try {
|
|
if ('update' in preconditions) {
|
|
assert(typeof preconditions.update === 'function');
|
|
await preconditions.update(req, sid);
|
|
}
|
|
const rowsAffected = await klass.update(sid, req.body);
|
|
if (rowsAffected === 0) {
|
|
return res.sendStatus(404);
|
|
}
|
|
res.status(204).end();
|
|
} catch (err) {
|
|
sysError(logger, res, err);
|
|
}
|
|
});
|
|
}
|
|
|
|
function remove(router, klass, preconditions) {
|
|
router.delete('/:sid', async(req, res) => {
|
|
const sid = req.params.sid;
|
|
const logger = req.app.locals.logger;
|
|
try {
|
|
if ('delete' in preconditions) {
|
|
assert(typeof preconditions.delete === 'function');
|
|
await preconditions.delete(req, sid);
|
|
}
|
|
const rowsAffected = await klass.remove(sid);
|
|
if (rowsAffected === 0) {
|
|
logger.info(`unable to delete ${klass.name} with sid ${sid}: not found`);
|
|
return res.sendStatus(404);
|
|
}
|
|
res.sendStatus(204);
|
|
} catch (err) {
|
|
sysError(logger, res, err);
|
|
}
|
|
});
|
|
}
|