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, [
|
2017-08-31 16:00:45 +02:00
|
|
|
'info',
|
2017-05-11 10:54:44 +02:00
|
|
|
'connection',
|
|
|
|
'collectionName',
|
|
|
|
'attributes',
|
|
|
|
'identity',
|
|
|
|
'globalId',
|
|
|
|
'globalName',
|
|
|
|
'orm',
|
|
|
|
'loadedModel',
|
|
|
|
'primaryKey',
|
2017-09-07 17:16:31 +02:00
|
|
|
'associations'
|
2017-05-11 10:54:44 +02:00
|
|
|
])
|
|
|
|
);
|
2017-03-20 22:08:49 +01:00
|
|
|
},
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
find: async ctx => {
|
2017-08-29 19:34:34 +02:00
|
|
|
const { limit, skip = 0, sort, 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
|
2017-08-29 19:34:34 +02:00
|
|
|
const entries = await strapi.query(ctx.params.model).find({
|
2017-06-17 17:01:50 +02:00
|
|
|
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-06-17 17:01:50 +02:00
|
|
|
// Count using `queries` system
|
2017-08-29 19:34:34 +02:00
|
|
|
const count = await strapi.query(ctx.params.model).count();
|
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-06-17 17:01:50 +02:00
|
|
|
// Find an entry using `queries` system
|
2017-08-29 19:34:34 +02:00
|
|
|
const entry = await strapi.query(ctx.params.model).findOne({
|
|
|
|
id: ctx.params.id
|
2017-06-17 17:01:50 +02:00
|
|
|
});
|
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-06-17 17:01:50 +02:00
|
|
|
// Create an entry using `queries` system
|
2017-08-29 19:34:34 +02:00
|
|
|
const entryCreated = await strapi.query(ctx.params.model).create({
|
|
|
|
values: ctx.request.body
|
2017-06-17 17:01:50 +02:00
|
|
|
});
|
2017-05-05 11:40:52 +02:00
|
|
|
|
|
|
|
ctx.body = entryCreated;
|
|
|
|
},
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
update: async ctx => {
|
2017-09-11 18:17:09 +02:00
|
|
|
const virtualFields = [];
|
|
|
|
const params = {
|
2017-08-30 17:56:52 +02:00
|
|
|
id: ctx.params.id,
|
2017-08-29 19:34:34 +02:00
|
|
|
values: ctx.request.body
|
2017-09-11 18:17:09 +02:00
|
|
|
};
|
2017-04-21 17:19:41 +02:00
|
|
|
|
2017-09-11 18:17:09 +02:00
|
|
|
// Retrieve current record.
|
|
|
|
const response = await strapi.query(ctx.params.model).findOne(params) || {};
|
|
|
|
// Save current model into variable to get virtual and p
|
|
|
|
const model = strapi.models[ctx.params.model];
|
|
|
|
|
2017-09-14 13:58:37 +02:00
|
|
|
|
2017-09-11 18:17:09 +02:00
|
|
|
// Only update fields which are on this document.
|
|
|
|
const values = Object.keys(params.values).reduce((acc, current) => {
|
|
|
|
if (!model.schema.virtuals.hasOwnProperty(current)) {
|
|
|
|
acc[current] = params.values[current];
|
|
|
|
} else if (response[current] && _.isArray(response[current]) && current !== 'id'){
|
|
|
|
const details = model.attributes[current];
|
|
|
|
|
2017-09-14 13:58:37 +02:00
|
|
|
const toAdd = _.differenceWith(params.values[current], response[current], (a, b) =>
|
|
|
|
a[model.primaryKey].toString() === b[model.primaryKey].toString()
|
|
|
|
);
|
|
|
|
const toRemove = _.differenceWith(response[current], params.values[current], (a, b) =>
|
|
|
|
a[model.primaryKey].toString() === b[model.primaryKey].toString()
|
|
|
|
)
|
2017-09-12 14:31:39 +02:00
|
|
|
.filter(x => toAdd.find(y => x.id === y.id) === undefined);
|
2017-09-11 18:17:09 +02:00
|
|
|
|
|
|
|
toAdd.forEach(value => {
|
|
|
|
value[details.via] = params.values[model.primaryKey];
|
|
|
|
|
|
|
|
virtualFields.push(strapi.query(details.model || details.collection).update({
|
2017-09-14 13:58:37 +02:00
|
|
|
id: value.id || value[model.primaryKey] || value._id,
|
2017-09-11 18:17:09 +02:00
|
|
|
values: value
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
|
|
|
toRemove.forEach(value => {
|
|
|
|
value[details.via] = null;
|
|
|
|
|
|
|
|
virtualFields.push(strapi.query(details.model || details.collection).update({
|
2017-09-14 13:58:37 +02:00
|
|
|
id: value.id || value[model.primaryKey] || value._id,
|
2017-09-11 18:17:09 +02:00
|
|
|
values: value
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
// Add current model to the flow of updates.
|
|
|
|
virtualFields.push(strapi.query(ctx.params.model).update({
|
|
|
|
id: params.id,
|
|
|
|
values
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Update virtuals fields.
|
|
|
|
const process = await Promise.all(virtualFields);
|
|
|
|
|
|
|
|
// Return the last one which is the current model.
|
|
|
|
ctx.body = process[process.length - 1];
|
2017-04-21 17:52:18 +02:00
|
|
|
},
|
|
|
|
|
2017-05-11 10:54:44 +02:00
|
|
|
delete: async ctx => {
|
2017-06-17 17:01:50 +02:00
|
|
|
// Delete an entry using `queries` system
|
2017-08-31 17:26:44 +02:00
|
|
|
const entryDeleted = await strapi.query(ctx.params.model).delete({
|
2017-08-29 19:34:34 +02:00
|
|
|
id: ctx.params.id
|
2017-06-17 17:01:50 +02:00
|
|
|
});
|
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
|
|
|
};
|