95 lines
2.0 KiB
JavaScript
Raw Normal View History

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