143 lines
3.3 KiB
JavaScript
Raw Normal View History

'use strict';
const _ = require('lodash');
const parseMultipartBody = require('../utils/parse-multipart');
2019-11-15 11:49:32 +01:00
const contentManagerService = require('../services/ContentManager');
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) {
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
);
}
ctx.body = entities;
},
2019-07-19 09:58:38 +02:00
/**
* Returns an entity of a content type by id
*/
async findOne(ctx) {
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;
},
2019-07-19 09:58:38 +02:00
/**
* Returns a count of entities of a content type matching query parameters
*/
async count(ctx) {
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) {
const { model } = ctx.params;
2017-11-27 17:27:16 +01:00
2019-07-19 09:58:38 +02:00
try {
if (ctx.is('multipart')) {
const { data, files } = parseMultipartBody(ctx);
ctx.body = await contentManagerService.create(data, {
files,
model,
});
} else {
// Create an entry using `queries` system
ctx.body = await contentManagerService.create(ctx.request.body, {
model,
});
}
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);
ctx.badRequest(null, [
{
messages: [
{ id: error.message, message: error.message, field: error.field },
],
},
]);
}
2019-07-19 09:58:38 +02:00
},
2019-07-19 09:58:38 +02:00
/**
* Updates an entity of a content type
*/
async update(ctx) {
const { model } = ctx.params;
2019-07-19 09:58:38 +02:00
try {
if (ctx.is('multipart')) {
const { data, files } = parseMultipartBody(ctx);
ctx.body = await contentManagerService.edit(ctx.params, data, {
files,
model,
});
} else {
// Return the last one which is the current model.
ctx.body = await contentManagerService.edit(
ctx.params,
ctx.request.body,
2019-11-15 11:49:32 +01:00
{ model }
);
}
2019-07-19 09:58:38 +02:00
} catch (error) {
strapi.log.error(error);
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
/**
* Deletes one entity of a content type matching a query
*/
async delete(ctx) {
2019-11-15 11:49:32 +01:00
ctx.body = await contentManagerService.delete(ctx.params);
2018-07-05 17:57:30 +02:00
},
/**
* Deletes multiple entities of a content type matching a query
*/
async deleteMany(ctx) {
ctx.body = await contentManagerService.deleteMany(
ctx.params,
ctx.request.query
);
2019-07-19 09:58:38 +02:00
},
};