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 => {
|
2017-11-27 17:27:16 +01:00
|
|
|
const pickData = (model) => _.pick(model, [
|
|
|
|
'info',
|
|
|
|
'connection',
|
|
|
|
'collectionName',
|
|
|
|
'attributes',
|
|
|
|
'identity',
|
|
|
|
'globalId',
|
|
|
|
'globalName',
|
|
|
|
'orm',
|
|
|
|
'loadedModel',
|
|
|
|
'primaryKey',
|
|
|
|
'associations'
|
|
|
|
]);
|
|
|
|
|
2018-02-02 14:50:10 +01:00
|
|
|
const models = _.mapValues(strapi.models, pickData);
|
2018-02-06 15:08:25 +01:00
|
|
|
delete models['core_store'];
|
2018-02-02 14:50:10 +01: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
|
|
|
plugins: Object.keys(strapi.plugins).reduce((acc, current) => {
|
|
|
|
acc[current] = {
|
|
|
|
models: _.mapValues(strapi.plugins[current].models, pickData)
|
|
|
|
};
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {})
|
|
|
|
};
|
2017-03-20 22:08:49 +01:00
|
|
|
},
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
find: async ctx => {
|
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 => {
|
2017-11-27 17:27:16 +01:00
|
|
|
const { source } = ctx.request.query;
|
|
|
|
|
2017-06-17 17:01:50 +02:00
|
|
|
// Count using `queries` system
|
2017-12-11 18:23:15 +01:00
|
|
|
const count = await strapi.plugins['content-manager'].services['contentmanager'].count(ctx.params, source);
|
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
|
2017-12-11 18:23:15 +01:00
|
|
|
const entry = await strapi.plugins['content-manager'].services['contentmanager'].fetch(ctx.params, source);
|
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 16:24:46 +01:00
|
|
|
console.log('hhhh', ctx.request.body.files.pictures[0]);
|
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
|
|
|
},
|
|
|
|
|
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
|
|
|
},
|
2017-03-20 22:08:49 +01:00
|
|
|
};
|