2017-03-20 22:08:49 +01:00
|
|
|
'use strict';
|
|
|
|
|
2017-08-16 17:05:02 +02:00
|
|
|
const _ = require('lodash');
|
2019-08-01 17:19:49 +02:00
|
|
|
const parseMultipartBody = require('../utils/parse-multipart');
|
2019-08-01 08:52:35 +02:00
|
|
|
|
2017-03-20 22:08:49 +01:00
|
|
|
module.exports = {
|
2019-07-19 09:58:38 +02:00
|
|
|
/**
|
|
|
|
* Returns a list of entities of a content-type matching the query parameters
|
|
|
|
*/
|
|
|
|
async find(ctx) {
|
2020-01-08 17:00:29 +01:00
|
|
|
const contentManagerService =
|
|
|
|
strapi.plugins['content-manager'].services.contentmanager;
|
|
|
|
|
2019-07-19 09:58:38 +02:00
|
|
|
let entities = [];
|
2019-07-26 10:57:27 +02:00
|
|
|
if (_.has(ctx.request.query, '_q')) {
|
2019-07-19 09:58:38 +02:00
|
|
|
entities = await contentManagerService.search(
|
|
|
|
ctx.params,
|
|
|
|
ctx.request.query
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
entities = await contentManagerService.fetchAll(
|
|
|
|
ctx.params,
|
|
|
|
ctx.request.query
|
|
|
|
);
|
|
|
|
}
|
2020-01-28 17:14:53 +01:00
|
|
|
|
2019-07-19 09:58:38 +02:00
|
|
|
ctx.body = entities;
|
2017-03-20 22:08:49 +01:00
|
|
|
},
|
|
|
|
|
2019-07-19 09:58:38 +02:00
|
|
|
/**
|
|
|
|
* Returns an entity of a content type by id
|
|
|
|
*/
|
|
|
|
async findOne(ctx) {
|
2020-01-08 17:00:29 +01:00
|
|
|
const contentManagerService =
|
|
|
|
strapi.plugins['content-manager'].services.contentmanager;
|
|
|
|
|
2019-11-15 11:49:32 +01:00
|
|
|
const entry = await contentManagerService.fetch(ctx.params);
|
2018-06-07 14:35:09 +02:00
|
|
|
|
2019-07-19 09:58:38 +02:00
|
|
|
// Entry not found
|
|
|
|
if (!entry) {
|
|
|
|
return ctx.notFound('Entry not found');
|
2018-06-07 14:35:09 +02:00
|
|
|
}
|
|
|
|
|
2019-07-19 09:58:38 +02:00
|
|
|
ctx.body = entry;
|
2017-03-20 22:08:49 +01:00
|
|
|
},
|
|
|
|
|
2019-07-19 09:58:38 +02:00
|
|
|
/**
|
|
|
|
* Returns a count of entities of a content type matching query parameters
|
|
|
|
*/
|
|
|
|
async count(ctx) {
|
2020-01-08 17:00:29 +01:00
|
|
|
const contentManagerService =
|
|
|
|
strapi.plugins['content-manager'].services.contentmanager;
|
|
|
|
|
2019-07-19 09:58:38 +02:00
|
|
|
let count;
|
2019-07-26 10:57:27 +02:00
|
|
|
if (_.has(ctx.request.query, '_q')) {
|
2019-07-19 09:58:38 +02:00
|
|
|
count = await contentManagerService.countSearch(
|
|
|
|
ctx.params,
|
|
|
|
ctx.request.query
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
count = await contentManagerService.count(ctx.params, ctx.request.query);
|
|
|
|
}
|
2017-04-11 11:34:59 +02:00
|
|
|
|
|
|
|
ctx.body = {
|
2019-07-19 09:58:38 +02:00
|
|
|
count: _.isNumber(count) ? count : _.toNumber(count),
|
2017-04-11 11:34:59 +02:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2019-07-19 09:58:38 +02:00
|
|
|
/**
|
|
|
|
* Creates an entity of a content type
|
|
|
|
*/
|
|
|
|
async create(ctx) {
|
2020-01-08 17:00:29 +01:00
|
|
|
const contentManagerService =
|
|
|
|
strapi.plugins['content-manager'].services.contentmanager;
|
|
|
|
|
2019-08-01 08:52:35 +02:00
|
|
|
const { model } = ctx.params;
|
2017-11-27 17:27:16 +01:00
|
|
|
|
2019-07-19 09:58:38 +02:00
|
|
|
try {
|
2019-08-01 08:52:35 +02:00
|
|
|
if (ctx.is('multipart')) {
|
2019-08-01 17:19:49 +02:00
|
|
|
const { data, files } = parseMultipartBody(ctx);
|
|
|
|
ctx.body = await contentManagerService.create(data, {
|
2019-08-01 08:52:35 +02:00
|
|
|
files,
|
|
|
|
model,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Create an entry using `queries` system
|
2019-08-01 17:27:00 +02:00
|
|
|
ctx.body = await contentManagerService.create(ctx.request.body, {
|
|
|
|
model,
|
|
|
|
});
|
2019-08-01 08:52:35 +02:00
|
|
|
}
|
2017-03-20 22:08:49 +01:00
|
|
|
|
2019-11-15 11:49:32 +01:00
|
|
|
strapi.emit('didCreateFirstContentTypeEntry', ctx.params);
|
2019-07-19 09:58:38 +02:00
|
|
|
} catch (error) {
|
|
|
|
strapi.log.error(error);
|
2019-08-21 12:10:23 +02:00
|
|
|
ctx.badRequest(null, [
|
|
|
|
{
|
|
|
|
messages: [
|
|
|
|
{ id: error.message, message: error.message, field: error.field },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]);
|
2017-06-17 17:01:50 +02:00
|
|
|
}
|
2019-07-19 09:58:38 +02:00
|
|
|
},
|
2017-03-20 22:08:49 +01:00
|
|
|
|
2019-07-19 09:58:38 +02:00
|
|
|
/**
|
|
|
|
* Updates an entity of a content type
|
|
|
|
*/
|
|
|
|
async update(ctx) {
|
2020-01-08 11:12:41 +01:00
|
|
|
const { id, model } = ctx.params;
|
2019-07-19 09:58:38 +02:00
|
|
|
|
2020-01-08 17:00:29 +01:00
|
|
|
const contentManagerService =
|
|
|
|
strapi.plugins['content-manager'].services.contentmanager;
|
|
|
|
|
2019-07-19 09:58:38 +02:00
|
|
|
try {
|
2019-08-01 08:52:35 +02:00
|
|
|
if (ctx.is('multipart')) {
|
2019-08-01 17:19:49 +02:00
|
|
|
const { data, files } = parseMultipartBody(ctx);
|
2020-01-08 11:12:41 +01:00
|
|
|
ctx.body = await contentManagerService.edit({ id }, data, {
|
2019-08-01 08:52:35 +02:00
|
|
|
files,
|
|
|
|
model,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// Return the last one which is the current model.
|
2020-01-08 11:12:41 +01:00
|
|
|
ctx.body = await contentManagerService.edit({ id }, ctx.request.body, {
|
|
|
|
model,
|
|
|
|
});
|
2019-08-01 08:52:35 +02:00
|
|
|
}
|
2019-07-19 09:58:38 +02:00
|
|
|
} catch (error) {
|
|
|
|
strapi.log.error(error);
|
2019-08-21 12:10:23 +02:00
|
|
|
ctx.badRequest(null, [
|
|
|
|
{
|
|
|
|
messages: [
|
|
|
|
{ id: error.message, message: error.message, field: error.field },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]);
|
2019-07-19 09:58:38 +02:00
|
|
|
}
|
|
|
|
},
|
2017-05-05 11:40:52 +02:00
|
|
|
|
2019-07-19 17:24:27 +02:00
|
|
|
/**
|
|
|
|
* Deletes one entity of a content type matching a query
|
|
|
|
*/
|
|
|
|
async delete(ctx) {
|
2020-01-08 17:00:29 +01:00
|
|
|
const contentManagerService =
|
|
|
|
strapi.plugins['content-manager'].services.contentmanager;
|
|
|
|
|
2019-11-15 11:49:32 +01:00
|
|
|
ctx.body = await contentManagerService.delete(ctx.params);
|
2018-07-05 17:57:30 +02:00
|
|
|
},
|
|
|
|
|
2019-07-19 17:24:27 +02:00
|
|
|
/**
|
|
|
|
* Deletes multiple entities of a content type matching a query
|
|
|
|
*/
|
|
|
|
async deleteMany(ctx) {
|
2020-01-08 17:00:29 +01:00
|
|
|
const contentManagerService =
|
|
|
|
strapi.plugins['content-manager'].services.contentmanager;
|
|
|
|
|
2019-07-19 17:24:27 +02:00
|
|
|
ctx.body = await contentManagerService.deleteMany(
|
|
|
|
ctx.params,
|
|
|
|
ctx.request.query
|
|
|
|
);
|
2019-07-19 09:58:38 +02:00
|
|
|
},
|
2017-03-20 22:08:49 +01:00
|
|
|
};
|