111 lines
3.2 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 => {
2018-06-28 12:12:54 +02:00
const pluginsStore = strapi.store({
environment: '',
type: 'plugin',
name: 'content-manager',
});
const models = await pluginsStore.get({ key: 'schema' });
2017-11-27 17:27:16 +01:00
ctx.body = {
models,
2017-11-27 17:27:16 +01:00
};
},
2017-05-11 10:54:44 +02:00
find: async ctx => {
2018-06-07 14:35:09 +02:00
// Search
2018-06-12 11:43:41 +02:00
if (!_.isEmpty(ctx.request.query._q)) {
2018-06-07 14:35:09 +02:00
ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].search(ctx.params, ctx.request.query);
return;
}
// Default list with filters or not.
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 => {
2018-06-07 14:35:09 +02:00
// Search
2018-06-12 11:43:41 +02:00
const count = !_.isEmpty(ctx.request.query._q)
2018-06-07 14:35:09 +02:00
? await strapi.plugins['content-manager'].services['contentmanager'].countSearch(ctx.params, ctx.request.query)
: 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
},
2018-07-05 17:57:30 +02:00
updateSettings: async ctx => {
const { schema } = ctx.request.body;
const pluginStore = strapi.store({
environment: '',
type: 'plugin',
name: 'content-manager'
});
await pluginStore.set({ key: 'schema', value: schema });
return ctx.body = { ok: true };
},
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
},
2018-06-05 14:20:13 +02:00
deleteAll: async ctx => {
ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].deleteMany(ctx.params, ctx.request.query);
2018-06-05 14:20:13 +02:00
}
};