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 => {
|
|
|
|
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];
|
2017-07-06 17:51:13 +02:00
|
|
|
const orm = _.get(strapi.plugins, ['content-manager', 'config', 'admin', 'schema', ctx.params.model, 'orm']) || model.orm;
|
2017-06-17 17:01:50 +02:00
|
|
|
const queries = _.get(strapi.plugins, ['content-manager', 'config', 'queries', orm]);
|
2017-05-06 17:27:24 +02:00
|
|
|
const primaryKey = model.primaryKey;
|
2017-06-18 17:23:58 +02:00
|
|
|
const {limit, skip = 0, sort = primaryKey, query, queryAttribute} = ctx.request.query;
|
2017-04-11 11:34:59 +02:00
|
|
|
|
2017-06-17 17:01:50 +02:00
|
|
|
// Find entries using `queries` system
|
|
|
|
const entries = await queries
|
|
|
|
.find({
|
|
|
|
model,
|
|
|
|
limit,
|
|
|
|
skip,
|
2017-06-18 17:23:58 +02:00
|
|
|
sort,
|
|
|
|
query,
|
|
|
|
queryAttribute
|
2017-06-17 17:01:50 +02:00
|
|
|
});
|
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-07-06 17:51:13 +02:00
|
|
|
const orm = _.get(strapi.plugins, ['content-manager', 'config', 'admin', 'schema', ctx.params.model, 'orm']) || model.orm;
|
2017-06-17 17:01:50 +02:00
|
|
|
const queries = _.get(strapi.plugins, ['content-manager', 'config', 'queries', orm]);
|
2017-04-11 11:34:59 +02:00
|
|
|
|
2017-06-17 17:01:50 +02:00
|
|
|
// Count using `queries` system
|
2017-06-18 17:23:58 +02:00
|
|
|
const count = await queries.count({model});
|
2017-04-11 11:34:59 +02:00
|
|
|
|
|
|
|
ctx.body = {
|
2017-06-17 17:01:50 +02:00
|
|
|
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];
|
2017-07-06 17:51:13 +02:00
|
|
|
const orm = _.get(strapi.plugins, ['content-manager', 'config', 'admin', 'schema', ctx.params.model, 'orm']) || model.orm;
|
2017-06-17 17:01:50 +02:00
|
|
|
const queries = _.get(strapi.plugins, ['content-manager', 'config', 'queries', orm]);
|
2017-05-06 17:27:24 +02:00
|
|
|
const primaryKey = model.primaryKey;
|
2017-06-17 17:01:50 +02:00
|
|
|
const id = ctx.params.id;
|
|
|
|
|
|
|
|
// Find an entry using `queries` system
|
|
|
|
const entry = await queries.findOne({
|
|
|
|
model,
|
|
|
|
primaryKey,
|
|
|
|
id
|
|
|
|
});
|
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-05-06 17:27:24 +02:00
|
|
|
const model = strapi.models[ctx.params.model];
|
2017-07-06 17:51:13 +02:00
|
|
|
const orm = _.get(strapi.plugins, ['content-manager', 'config', 'admin', 'schema', ctx.params.model, 'orm']) || model.orm;
|
2017-06-17 17:01:50 +02:00
|
|
|
const queries = _.get(strapi.plugins, ['content-manager', 'config', 'queries', orm]);
|
|
|
|
const values = ctx.request.body;
|
2017-05-05 11:40:52 +02:00
|
|
|
|
2017-06-17 17:01:50 +02:00
|
|
|
// Create an entry using `queries` system
|
|
|
|
const entryCreated = await queries.create({
|
|
|
|
model,
|
|
|
|
values
|
|
|
|
});
|
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];
|
2017-07-06 17:51:13 +02:00
|
|
|
const orm = _.get(strapi.plugins, ['content-manager', 'config', 'admin', 'schema', ctx.params.model, 'orm']) || model.orm;
|
2017-06-17 17:01:50 +02:00
|
|
|
const queries = _.get(strapi.plugins, ['content-manager', 'config', 'queries', orm]);
|
2017-05-06 17:27:24 +02:00
|
|
|
const primaryKey = model.primaryKey;
|
2017-06-17 17:01:50 +02:00
|
|
|
const id = ctx.params.id;
|
|
|
|
const values = ctx.request.body;
|
2017-05-06 17:27:24 +02:00
|
|
|
|
2017-06-17 17:01:50 +02:00
|
|
|
// Update an entry using `queries` system
|
|
|
|
const entryUpdated = await queries.update({
|
|
|
|
model,
|
|
|
|
primaryKey,
|
|
|
|
id,
|
|
|
|
values
|
|
|
|
});
|
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];
|
2017-07-06 17:51:13 +02:00
|
|
|
const orm = _.get(strapi.plugins, ['content-manager', 'config', 'admin', 'schema', ctx.params.model, 'orm']) || model.orm;
|
2017-06-17 17:01:50 +02:00
|
|
|
const queries = _.get(strapi.plugins, ['content-manager', 'config', 'queries', orm]);
|
2017-05-06 17:27:24 +02:00
|
|
|
const primaryKey = model.primaryKey;
|
2017-06-17 17:01:50 +02:00
|
|
|
const id = ctx.params.id;
|
|
|
|
|
|
|
|
// Delete an entry using `queries` system
|
|
|
|
const entryDeleted = await queries.delete({
|
|
|
|
model,
|
|
|
|
primaryKey,
|
|
|
|
id
|
|
|
|
});
|
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
|
|
|
};
|