102 lines
2.7 KiB
JavaScript
Raw Normal View History

'use strict';
const _ = require('lodash');
/**
* A set of functions called "actions" for `ContentManager`
*/
module.exports = {
2018-03-14 10:22:15 +01:00
layout: async (ctx) => {
const {source} = ctx.query;
return ctx.send(_.get(strapi.plugins, [source, 'config', 'layout'], {}));
},
2017-05-11 10:54:44 +02:00
models: async ctx => {
2017-11-27 17:27:16 +01:00
const pickData = (model) => _.pick(model, [
'info',
'connection',
'collectionName',
'attributes',
'identity',
'globalId',
'globalName',
'orm',
'loadedModel',
'primaryKey',
'associations'
]);
const models = _.mapValues(strapi.models, pickData);
2018-02-06 15:08:25 +01:00
delete models['core_store'];
2017-11-27 17:27:16 +01:00
ctx.body = {
models,
2017-11-27 17:27:16 +01:00
plugins: Object.keys(strapi.plugins).reduce((acc, current) => {
acc[current] = {
models: _.mapValues(strapi.plugins[current].models, pickData)
};
return acc;
}, {})
};
},
2017-05-11 10:54:44 +02:00
find: async ctx => {
ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].fetchAll(ctx.params, ctx.request.query);
},
2017-05-11 10:54:44 +02:00
count: async ctx => {
// Count using `queries` system
2018-05-22 17:11:35 +02:00
const count = await strapi.plugins['content-manager'].services['contentmanager'].count(ctx.params, ctx.request.query);
2017-04-11 11:34:59 +02:00
ctx.body = {
count: _.isNumber(count) ? count : _.toNumber(count)
2017-04-11 11:34:59 +02:00
};
},
2017-05-11 10:54:44 +02:00
findOne: async ctx => {
2017-11-27 17:27:16 +01:00
const { source } = ctx.request.query;
// Find an entry using `queries` system
const entry = await strapi.plugins['content-manager'].services['contentmanager'].fetch(ctx.params, source, null, false);
// Entry not found
if (!entry) {
return (ctx.notFound('Entry not found'));
}
2017-05-04 19:05:41 +02:00
ctx.body = entry;
2017-04-21 17:19:41 +02:00
},
2017-05-11 10:54:44 +02:00
create: async ctx => {
2017-11-27 17:27:16 +01:00
const { source } = ctx.request.query;
2017-12-06 15:58:20 +01:00
2017-12-06 15:11:55 +01:00
try {
// Create an entry using `queries` system
ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].add(ctx.params, ctx.request.body, source);
2017-12-06 15:11:55 +01:00
} catch(error) {
strapi.log.error(error);
2017-12-06 15:58:20 +01:00
ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: error.message, field: error.field }] }] : error.message);
2017-12-06 15:11:55 +01:00
}
2017-05-05 11:40:52 +02:00
},
2017-05-11 10:54:44 +02:00
update: async ctx => {
2017-11-27 17:27:16 +01:00
const { source } = ctx.request.query;
2017-12-06 15:11:55 +01:00
try {
// Return the last one which is the current model.
ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].edit(ctx.params, ctx.request.body, source);
2017-12-06 15:11:55 +01:00
} catch(error) {
2017-12-06 15:58:20 +01:00
// TODO handle error update
strapi.log.error(error);
2017-12-06 15:58:20 +01:00
ctx.badRequest(null, ctx.request.admin ? [{ messages: [{ id: error.message, field: error.field }] }] : error.message);
2017-12-06 15:11:55 +01:00
}
2017-04-21 17:52:18 +02:00
},
2017-05-11 10:54:44 +02:00
delete: async ctx => {
ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].delete(ctx.params, ctx.request.query);
2017-05-11 10:54:44 +02:00
},
};