205 lines
4.9 KiB
JavaScript
Raw Normal View History

'use strict';
const _ = require('lodash');
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) {
const contentManagerService =
strapi.plugins['content-manager'].services['contentmanager'];
let entities = [];
if (!_.isEmpty(ctx.request.query._q)) {
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) {
const { source } = ctx.request.query;
const contentManagerService =
strapi.plugins['content-manager'].services['contentmanager'];
const entry = await contentManagerService.fetch(ctx.params, source);
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 contentManagerService =
strapi.plugins['content-manager'].services['contentmanager'];
let count;
if (!_.isEmpty(ctx.request.query._q)) {
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) {
2017-11-27 17:27:16 +01:00
const { source } = ctx.request.query;
2019-07-19 09:58:38 +02:00
const contentManagerService =
strapi.plugins['content-manager'].services['contentmanager'];
2017-11-27 17:27:16 +01:00
2019-07-19 09:58:38 +02:00
try {
// Create an entry using `queries` system
ctx.body = await contentManagerService.add(
ctx.params,
ctx.request.body,
source
);
2019-07-19 09:58:38 +02:00
strapi.emit('didCreateFirstContentTypeEntry', ctx.params, source);
} catch (error) {
strapi.log.error(error);
ctx.badRequest(
null,
ctx.request.admin
? [{ messages: [{ id: error.message, field: error.field }] }]
: error.message
);
}
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 { source } = ctx.request.query;
const contentManagerService =
strapi.plugins['content-manager'].services['contentmanager'];
try {
// Return the last one which is the current model.
ctx.body = await contentManagerService.edit(
ctx.params,
ctx.request.body,
source
);
} catch (error) {
strapi.log.error(error);
ctx.badRequest(
null,
ctx.request.admin
? [{ messages: [{ id: error.message, field: error.field }] }]
: error.message
);
}
},
delete() {},
deleteMany() {},
};
module.exports = {
models: async ctx => {
const pluginsStore = strapi.store({
environment: '',
type: 'plugin',
name: 'content-manager',
});
const models = await pluginsStore.get({ key: 'schema' });
ctx.body = {
models,
};
2017-04-21 17:19:41 +02:00
},
2017-05-11 10:54:44 +02:00
create: async ctx => {
2017-11-27 17:27:16 +01:00
const { source } = ctx.request.query;
2017-12-06 15:58:20 +01:00
2017-12-06 15:11:55 +01:00
try {
// Create an entry using `queries` system
2019-07-19 09:58:38 +02:00
ctx.body = await strapi.plugins['content-manager'].services[
'contentmanager'
].add(ctx.params, ctx.request.body, source);
2019-01-23 15:33:30 +01:00
strapi.emit('didCreateFirstContentTypeEntry', ctx.params, source);
2019-07-19 09:58:38 +02:00
} catch (error) {
strapi.log.error(error);
2019-07-19 09:58:38 +02:00
ctx.badRequest(
null,
ctx.request.admin
? [{ messages: [{ id: error.message, field: error.field }] }]
: error.message
);
2017-12-06 15:11:55 +01:00
}
2017-05-05 11:40:52 +02:00
},
2017-05-11 10:54:44 +02:00
update: async ctx => {
2017-11-27 17:27:16 +01:00
const { source } = ctx.request.query;
2017-12-06 15:11:55 +01:00
try {
// Return the last one which is the current model.
2019-07-19 09:58:38 +02:00
ctx.body = await strapi.plugins['content-manager'].services[
'contentmanager'
].edit(ctx.params, ctx.request.body, source);
} catch (error) {
2017-12-06 15:58:20 +01:00
// TODO handle error update
strapi.log.error(error);
2019-07-19 09:58:38 +02:00
ctx.badRequest(
null,
ctx.request.admin
? [{ messages: [{ id: error.message, field: error.field }] }]
: error.message
);
2017-12-06 15:11:55 +01:00
}
2017-04-21 17:52:18 +02:00
},
2018-07-05 17:57:30 +02:00
updateSettings: async ctx => {
const { schema } = ctx.request.body;
const pluginStore = strapi.store({
environment: '',
type: 'plugin',
2019-07-19 09:58:38 +02:00
name: 'content-manager',
2018-07-05 17:57:30 +02:00
});
await pluginStore.set({ key: 'schema', value: schema });
2019-07-19 09:58:38 +02:00
return (ctx.body = { ok: true });
2018-07-05 17:57:30 +02:00
},
2017-05-11 10:54:44 +02:00
delete: async ctx => {
2019-07-19 09:58:38 +02:00
ctx.body = await strapi.plugins['content-manager'].services[
'contentmanager'
].delete(ctx.params, ctx.request.query);
2017-05-11 10:54:44 +02:00
},
2018-06-05 14:20:13 +02:00
deleteAll: async ctx => {
2019-07-19 09:58:38 +02:00
ctx.body = await strapi.plugins['content-manager'].services[
'contentmanager'
].deleteMany(ctx.params, ctx.request.query);
},
};