2018-06-24 01:32:08 +03:00
|
|
|
const _ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
debug = require('ghost-ignition').debug('services:routing:controllers:static'),
|
|
|
|
api = require('../../../api'),
|
|
|
|
helpers = require('../helpers');
|
|
|
|
|
|
|
|
function processQuery(query) {
|
|
|
|
query = _.cloneDeep(query);
|
|
|
|
|
|
|
|
// Return a promise for the api query
|
|
|
|
return api[query.resource][query.type](query.options);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = function staticController(req, res, next) {
|
2018-06-26 02:12:50 +03:00
|
|
|
debug('staticController', res.routerOptions);
|
2018-06-24 01:32:08 +03:00
|
|
|
|
|
|
|
let props = {};
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
_.each(res.routerOptions.data, function (query, name) {
|
2018-06-24 01:32:08 +03:00
|
|
|
props[name] = processQuery(query);
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.props(props)
|
|
|
|
.then(function handleResult(result) {
|
|
|
|
let response = {};
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
if (res.routerOptions.data) {
|
2018-06-24 01:32:08 +03:00
|
|
|
response.data = {};
|
|
|
|
|
2018-06-26 02:12:50 +03:00
|
|
|
_.each(res.routerOptions.data, function (config, name) {
|
2018-06-24 01:32:08 +03:00
|
|
|
if (config.type === 'browse') {
|
|
|
|
response.data[name] = result[name];
|
|
|
|
} else {
|
|
|
|
response.data[name] = result[name][config.resource];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// @TODO: get rid of this O_O
|
|
|
|
_.each(response.data, function (data) {
|
|
|
|
helpers.secure(req, data);
|
|
|
|
});
|
|
|
|
|
|
|
|
helpers.renderer(req, res, helpers.formatResponse.entries(response));
|
|
|
|
})
|
|
|
|
.catch(helpers.handleError(next));
|
|
|
|
};
|