145 lines
3.4 KiB
JavaScript
Raw Normal View History

'use strict';
const _ = require('lodash');
/**
* 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'
]);
ctx.body = {
models: _.mapValues(strapi.models, pickData),
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 => {
2017-11-27 17:27:16 +01:00
const { limit, skip = 0, sort, query, queryAttribute, source } = ctx.request.query;
2017-04-11 11:34:59 +02:00
// Find entries using `queries` system
2017-11-27 17:27:16 +01:00
const entries = await strapi.query(ctx.params.model, source).find({
limit,
skip,
2017-06-18 17:23:58 +02:00
sort,
query,
queryAttribute
});
ctx.body = entries;
},
2017-05-11 10:54:44 +02:00
count: async ctx => {
2017-11-27 17:27:16 +01:00
const { source } = ctx.request.query;
// Count using `queries` system
2017-11-27 17:27:16 +01:00
const count = await strapi.query(ctx.params.model, source).count();
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
2017-11-27 17:27:16 +01:00
const entry = await strapi.query(ctx.params.model, source).findOne({
id: ctx.params.id
});
// 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:11:55 +01:00
console.log('bite');
try {
console.log('fuck');
// Create an entry using `queries` system
const entryCreated = await strapi.query(ctx.params.model, source).create({
values: ctx.request.body
});
2017-11-27 17:27:16 +01:00
2017-12-06 15:11:55 +01:00
ctx.body = entryCreated;
} catch(error) {
console.log('ok')
ctx.badRequest(null, [{ messages: [{ id: error }] }]);
}
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 {
// Add current model to the flow of updates.
const entry = strapi.query(ctx.params.model, source).update({
id: ctx.params.id,
values: ctx.request.body
});
2017-09-11 18:17:09 +02:00
2017-12-06 15:11:55 +01:00
// Return the last one which is the current model.
ctx.body = entry;
} catch(error) {
ctx.badRequest(null, [{ messages: [{ id: error }] }]);
}
2017-04-21 17:52:18 +02:00
},
2017-05-11 10:54:44 +02:00
delete: async ctx => {
2017-11-27 17:27:16 +01:00
const { source } = ctx.request.query;
2017-09-20 13:19:36 +02:00
const params = ctx.params;
2017-11-27 17:27:16 +01:00
const response = await strapi.query(params.model, source).findOne({
2017-09-20 13:19:36 +02:00
id: params.id
2017-09-20 18:14:18 +02:00
});
2017-09-20 13:19:36 +02:00
params.values = Object.keys(JSON.parse(JSON.stringify(response))).reduce((acc, current) => {
const association = (strapi.models[params.model] || strapi.plugins[source].models[params.model]).associations.filter(x => x.alias === current)[0];
2017-09-20 13:19:36 +02:00
// Remove relationships.
if (association) {
acc[current] = _.isArray(response[current]) ? [] : null;
}
return acc;
}, {});
2017-09-20 18:14:18 +02:00
if (!_.isEmpty(params.values)) {
// Run update to remove all relationships.
2017-11-27 17:27:16 +01:00
await strapi.query(params.model, source).update(params);
2017-09-20 18:14:18 +02:00
}
2017-09-20 13:19:36 +02:00
// Delete an entry using `queries` system
2017-11-27 17:27:16 +01:00
const entryDeleted = await strapi.query(params.model, source).delete({
2017-09-20 13:19:36 +02:00
id: params.id
});
2017-04-21 17:52:18 +02:00
ctx.body = entryDeleted;
2017-05-11 10:54:44 +02:00
},
};