2017-03-20 22:08:49 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-08-16 17:05:02 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2017-03-20 22:08:49 +01:00
|
|
|
/**
|
|
|
|
* A set of functions called "actions" for `ContentManager`
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = {
|
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' });
|
2018-07-23 15:12:33 +02:00
|
|
|
|
2017-11-27 17:27:16 +01:00
|
|
|
ctx.body = {
|
2018-02-02 14:50:10 +01:00
|
|
|
models,
|
2017-11-27 17:27:16 +01:00
|
|
|
};
|
2017-03-20 22:08:49 +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.
|
2017-12-11 18:23:15 +01:00
|
|
|
ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].fetchAll(ctx.params, ctx.request.query);
|
2017-03-20 22:08:49 +01:00
|
|
|
},
|
|
|
|
|
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 = {
|
2017-09-14 16:33:56 +02:00
|
|
|
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;
|
|
|
|
|
2017-06-17 17:01:50 +02:00
|
|
|
// Find an entry using `queries` system
|
2018-04-24 15:24:42 +02:00
|
|
|
const entry = await strapi.plugins['content-manager'].services['contentmanager'].fetch(ctx.params, source, null, false);
|
2017-03-20 22:08:49 +01:00
|
|
|
|
2017-06-17 17:01:50 +02:00
|
|
|
// Entry not found
|
|
|
|
if (!entry) {
|
|
|
|
return (ctx.notFound('Entry not found'));
|
|
|
|
}
|
2017-03-20 22:08:49 +01:00
|
|
|
|
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
|
2017-12-11 18:23:15 +01:00
|
|
|
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) {
|
2018-02-27 11:52:18 +01:00
|
|
|
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;
|
2018-02-27 17:38:59 +01:00
|
|
|
|
2017-12-06 15:11:55 +01:00
|
|
|
try {
|
|
|
|
// Return the last one which is the current model.
|
2017-12-11 18:23:15 +01:00
|
|
|
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
|
2018-02-27 11:52:18 +01:00
|
|
|
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 => {
|
2017-12-11 18:23:15 +01:00
|
|
|
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 => {
|
2018-06-05 18:35:19 +02:00
|
|
|
ctx.body = await strapi.plugins['content-manager'].services['contentmanager'].deleteMany(ctx.params, ctx.request.query);
|
2018-06-05 14:20:13 +02:00
|
|
|
}
|
2017-03-20 22:08:49 +01:00
|
|
|
};
|