178 lines
4.9 KiB
JavaScript
Raw Normal View History

'use strict';
const _ = require('lodash');
const parseMultipartBody = require('../utils/parse-multipart');
const {
validateGenerateUIDInput,
validateCheckUIDAvailabilityInput,
validateUIDField,
} = require('./validation');
module.exports = {
async generateUID(ctx) {
const { contentTypeUID, field, data } = await validateGenerateUIDInput(ctx.request.body);
await validateUIDField(contentTypeUID, field);
const uidService = strapi.plugins['content-manager'].services.uid;
ctx.body = {
data: await uidService.generateUIDField({ contentTypeUID, field, data }),
};
},
async checkUIDAvailability(ctx) {
const { contentTypeUID, field, value } = await validateCheckUIDAvailabilityInput(
ctx.request.body
);
await validateUIDField(contentTypeUID, field);
const uidService = strapi.plugins['content-manager'].services.uid;
const isAvailable = await uidService.checkUIDAvailability({ contentTypeUID, field, value });
ctx.body = {
isAvailable,
suggestion: !isAvailable
? await uidService.findUniqueUID({ contentTypeUID, field, value })
: null,
};
},
2019-07-19 09:58:38 +02:00
/**
* Returns a list of entities of a content-type matching the query parameters
*/
async find(ctx) {
const { model } = ctx.params;
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')) {
entities = await contentManagerService.search({ model }, ctx.request.query);
2019-07-19 09:58:38 +02:00
} else {
entities = await contentManagerService.fetchAll({ model }, ctx.request.query);
2019-07-19 09:58:38 +02:00
}
2019-07-19 09:58:38 +02:00
ctx.body = entities;
},
2019-07-19 09:58:38 +02:00
/**
* Returns an entity of a content type by id
*/
async findOne(ctx) {
const { model, id } = ctx.params;
const contentManagerService = strapi.plugins['content-manager'].services.contentmanager;
const entry = await contentManagerService.fetch({ model, id });
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) {
const { model } = ctx.params;
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')) {
count = await contentManagerService.countSearch({ model }, ctx.request.query);
2019-07-19 09:58:38 +02:00
} else {
count = await contentManagerService.count({ model }, ctx.request.query);
2019-07-19 09:58:38 +02:00
}
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 contentManagerService = strapi.plugins['content-manager'].services.contentmanager;
const { model } = ctx.params;
2017-11-27 17:27:16 +01: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 });
}
await strapi.telemetry.send('didCreateFirstContentTypeEntry', { model });
} catch (error) {
strapi.log.error(error);
ctx.badRequest(null, [
{
messages: [{ id: error.message, message: error.message, field: error.field }],
errors: _.get(error, 'data.errors'),
},
]);
}
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 { id, model } = ctx.params;
2019-07-19 09:58:38 +02:00
const contentManagerService = strapi.plugins['content-manager'].services.contentmanager;
try {
if (ctx.is('multipart')) {
const { data, files } = parseMultipartBody(ctx);
ctx.body = await contentManagerService.edit({ id }, data, {
files,
model,
});
} else {
// Return the last one which is the current model.
ctx.body = await contentManagerService.edit({ id }, ctx.request.body, {
model,
});
}
} catch (error) {
strapi.log.error(error);
ctx.badRequest(null, [
{
messages: [{ id: error.message, message: error.message, field: error.field }],
errors: _.get(error, 'data.errors'),
},
]);
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) {
const { id, model } = ctx.params;
const contentManagerService = strapi.plugins['content-manager'].services.contentmanager;
ctx.body = await contentManagerService.delete({ id, model });
2018-07-05 17:57:30 +02:00
},
/**
* Deletes multiple entities of a content type matching a query
*/
async deleteMany(ctx) {
const { model } = ctx.params;
const contentManagerService = strapi.plugins['content-manager'].services.contentmanager;
ctx.body = await contentManagerService.deleteMany({ model }, ctx.request.query);
2019-07-19 09:58:38 +02:00
},
};