2017-03-20 22:08:49 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A set of functions called "actions" for `ContentManager`
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = {
|
2017-05-11 10:54:44 +02:00
|
|
|
models: async ctx => {
|
|
|
|
ctx.body = _.mapValues(strapi.models, model =>
|
|
|
|
_.pick(model, [
|
|
|
|
'connection',
|
|
|
|
'collectionName',
|
|
|
|
'attributes',
|
|
|
|
'identity',
|
|
|
|
'globalId',
|
|
|
|
'globalName',
|
|
|
|
'orm',
|
|
|
|
'loadedModel',
|
|
|
|
'primaryKey',
|
|
|
|
])
|
|
|
|
);
|
2017-03-20 22:08:49 +01:00
|
|
|
},
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
find: async ctx => {
|
2017-05-06 17:27:24 +02:00
|
|
|
const model = strapi.models[ctx.params.model];
|
|
|
|
const primaryKey = model.primaryKey;
|
2017-03-20 22:08:49 +01:00
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
const { limit = 10, skip = 0, sort = primaryKey } = ctx.request.query;
|
2017-04-11 11:34:59 +02:00
|
|
|
|
2017-05-09 16:21:17 +02:00
|
|
|
const entries = await model
|
2017-04-11 11:34:59 +02:00
|
|
|
.find()
|
2017-05-03 17:10:23 +02:00
|
|
|
.limit(Number(limit))
|
2017-04-11 13:44:47 +02:00
|
|
|
.sort(sort)
|
2017-05-03 17:10:23 +02:00
|
|
|
.skip(Number(skip));
|
2017-03-20 22:08:49 +01:00
|
|
|
|
|
|
|
ctx.body = entries;
|
|
|
|
},
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
count: async ctx => {
|
2017-05-06 17:27:24 +02:00
|
|
|
const model = strapi.models[ctx.params.model];
|
2017-04-11 11:34:59 +02:00
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
const count = await model.count();
|
2017-04-11 11:34:59 +02:00
|
|
|
|
|
|
|
ctx.body = {
|
2017-05-11 10:54:44 +02:00
|
|
|
count: Number(count),
|
2017-04-11 11:34:59 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
findOne: async ctx => {
|
2017-05-06 17:27:24 +02:00
|
|
|
const model = strapi.models[ctx.params.model];
|
|
|
|
const primaryKey = model.primaryKey;
|
2017-05-04 19:05:41 +02:00
|
|
|
const params = {};
|
|
|
|
params[primaryKey] = ctx.params.id;
|
2017-03-20 22:08:49 +01:00
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
const entry = await model.findOne(params);
|
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-05-06 17:27:24 +02:00
|
|
|
const model = strapi.models[ctx.params.model];
|
2017-05-05 11:40:52 +02:00
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
const entryCreated = await model.create(ctx.request.body);
|
2017-05-05 11:40:52 +02:00
|
|
|
|
|
|
|
ctx.body = entryCreated;
|
|
|
|
},
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
update: async ctx => {
|
2017-05-06 17:27:24 +02:00
|
|
|
const model = strapi.models[ctx.params.model];
|
|
|
|
const primaryKey = model.primaryKey;
|
2017-05-04 19:05:41 +02:00
|
|
|
const params = {};
|
2017-05-06 17:27:24 +02:00
|
|
|
|
2017-05-04 19:05:41 +02:00
|
|
|
params[primaryKey] = ctx.params.id;
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
const entryUpdated = await model.update(params, ctx.request.body);
|
2017-04-21 17:19:41 +02:00
|
|
|
|
2017-04-21 17:52:18 +02:00
|
|
|
ctx.body = entryUpdated;
|
|
|
|
},
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
delete: async ctx => {
|
2017-05-06 17:27:24 +02:00
|
|
|
const model = strapi.models[ctx.params.model];
|
|
|
|
const primaryKey = model.primaryKey;
|
2017-05-04 19:05:41 +02:00
|
|
|
const params = {};
|
|
|
|
params[primaryKey] = ctx.params.id;
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
const entryDeleted = await model.remove(params);
|
2017-04-21 17:52:18 +02:00
|
|
|
|
|
|
|
ctx.body = entryDeleted;
|
2017-05-11 10:54:44 +02:00
|
|
|
},
|
2017-03-20 22:08:49 +01:00
|
|
|
};
|