Init strapi.service and use it where services where required

This commit is contained in:
Alexandre Bodin 2020-01-08 17:00:29 +01:00
parent 2eea8e9d9b
commit 7c94a2316d
6 changed files with 77 additions and 9 deletions

View File

@ -1,14 +1,15 @@
'use strict';
const { createModelConfigurationSchema } = require('./validation');
const contentTypeService = require('../services/ContentTypes');
const componentService = require('../services/Components');
module.exports = {
/**
* Returns the list of available components
*/
async listComponents(ctx) {
const contentTypeService =
strapi.plugins['content-manager'].services.contenttypes;
const data = Object.keys(strapi.components).map(uid => {
return {
category: strapi.components[uid].category,
@ -35,6 +36,9 @@ module.exports = {
return ctx.notFound('component.notFound');
}
const componentService =
strapi.plugins['content-manager'].services.components;
const data = await componentService.getComponentInformations(uid);
ctx.body = { data };
@ -56,6 +60,11 @@ module.exports = {
return ctx.notFound('component.notFound');
}
const componentService =
strapi.plugins['content-manager'].services.components;
const contentTypeService =
strapi.plugins['content-manager'].services.contenttypes;
const schema = contentTypeService.formatContentTypeSchema(component);
let input;
try {

View File

@ -2,13 +2,15 @@
const _ = require('lodash');
const parseMultipartBody = require('../utils/parse-multipart');
const contentManagerService = require('../services/ContentManager');
module.exports = {
/**
* 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 (_.has(ctx.request.query, '_q')) {
entities = await contentManagerService.search(
@ -28,6 +30,9 @@ module.exports = {
* Returns an entity of a content type by id
*/
async findOne(ctx) {
const contentManagerService =
strapi.plugins['content-manager'].services.contentmanager;
const entry = await contentManagerService.fetch(ctx.params);
// Entry not found
@ -42,6 +47,9 @@ module.exports = {
* 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 (_.has(ctx.request.query, '_q')) {
count = await contentManagerService.countSearch(
@ -61,6 +69,9 @@ module.exports = {
* Creates an entity of a content type
*/
async create(ctx) {
const contentManagerService =
strapi.plugins['content-manager'].services.contentmanager;
const { model } = ctx.params;
try {
@ -94,6 +105,9 @@ module.exports = {
* Updates an entity of a content type
*/
async update(ctx) {
const contentManagerService =
strapi.plugins['content-manager'].services.contentmanager;
const { model } = ctx.params;
try {
@ -127,6 +141,9 @@ module.exports = {
* Deletes one entity of a content type matching a query
*/
async delete(ctx) {
const contentManagerService =
strapi.plugins['content-manager'].services.contentmanager;
ctx.body = await contentManagerService.delete(ctx.params);
},
@ -134,6 +151,9 @@ module.exports = {
* Deletes multiple entities of a content type matching a query
*/
async deleteMany(ctx) {
const contentManagerService =
strapi.plugins['content-manager'].services.contentmanager;
ctx.body = await contentManagerService.deleteMany(
ctx.params,
ctx.request.query

View File

@ -1,14 +1,14 @@
'use strict';
const { createModelConfigurationSchema } = require('./validation');
const service = require('../services/ContentTypes');
const componentService = require('../services/Components');
module.exports = {
/**
* Returns the list of available content types
*/
listContentTypes(ctx) {
const service = strapi.plugins['content-manager'].services.contenttypes;
const contentTypes = Object.keys(strapi.contentTypes)
.filter(uid => {
if (uid.startsWith('strapi::')) return false;
@ -42,6 +42,10 @@ module.exports = {
return ctx.notFound('contentType.notFound');
}
const service = strapi.plugins['content-manager'].services.contenttypes;
const componentService =
strapi.plugins['content-manager'].services.components;
const contentTypeConfigurations = await service.getConfiguration(uid);
const data = {
@ -74,6 +78,8 @@ module.exports = {
return ctx.notFound('contentType.notFound');
}
const service = strapi.plugins['content-manager'].services.contenttypes;
const schema = service.formatContentTypeSchema(contentType);
let input;

View File

@ -1,7 +1,6 @@
'use strict';
const validateComponentCategory = require('./validation/component-category');
const componentCategoryService = require('../services/ComponentCategories');
module.exports = {
async editCategory(ctx) {
@ -17,17 +16,24 @@ module.exports = {
strapi.reload.isWatching = false;
const componentCategoryService =
strapi.plugins['content-type-builder'].services.componentcategories;
const newName = await componentCategoryService.editCategory(name, body);
setImmediate(() => strapi.reload());
ctx.send({ name: newName });
},
async deleteCategory(ctx) {
const { name } = ctx.params;
strapi.reload.isWatching = false;
const componentCategoryService =
strapi.plugins['content-type-builder'].services.componentcategories;
await componentCategoryService.deleteCategory(name);
setImmediate(() => strapi.reload());

View File

@ -6,7 +6,6 @@ const {
validateComponentInput,
validateUpdateComponentInput,
} = require('./validation/component');
const componentService = require('../services/Components');
/**
* Components controller
@ -19,6 +18,9 @@ module.exports = {
* @param {Object} ctx - koa context
*/
async getComponents(ctx) {
const componentService =
strapi.plugins['content-type-builder'].services.components;
const data = Object.keys(strapi.components).map(uid => {
return componentService.formatComponent(strapi.components[uid]);
});
@ -40,6 +42,9 @@ module.exports = {
return ctx.send({ error: 'component.notFound' }, 404);
}
const componentService =
strapi.plugins['content-type-builder'].services.components;
ctx.send({ data: componentService.formatComponent(component) });
},
@ -60,6 +65,9 @@ module.exports = {
try {
strapi.reload.isWatching = false;
const componentService =
strapi.plugins['content-type-builder'].services.components;
const component = await componentService.createComponent({
component: body.component,
components: body.components,
@ -96,6 +104,9 @@ module.exports = {
try {
strapi.reload.isWatching = false;
const componentService =
strapi.plugins['content-type-builder'].services.components;
const component = await componentService.editComponent(uid, {
component: body.component,
components: body.components,
@ -125,6 +136,9 @@ module.exports = {
try {
strapi.reload.isWatching = false;
const componentService =
strapi.plugins['content-type-builder'].services.components;
const component = await componentService.deleteComponent(uid);
setImmediate(() => strapi.reload());

View File

@ -7,10 +7,11 @@ const {
validateUpdateContentTypeInput,
} = require('./validation/content-type');
const contentTypeService = require('../services/ContentTypes');
module.exports = {
getContentTypes(ctx) {
const contentTypeService =
strapi.plugins['content-type-builder'].services.contenttypes;
const contentTypes = Object.keys(strapi.contentTypes)
.filter(uid => {
if (uid.startsWith('strapi::')) return false;
@ -36,6 +37,9 @@ module.exports = {
return ctx.send({ error: 'contentType.notFound' }, 404);
}
const contentTypeService =
strapi.plugins['content-type-builder'].services.contenttypes;
ctx.send({ data: contentTypeService.formatContentType(contentType) });
},
@ -51,6 +55,9 @@ module.exports = {
try {
strapi.reload.isWatching = false;
const contentTypeService =
strapi.plugins['content-type-builder'].services.contenttypes;
const component = await contentTypeService.createContentType({
contentType: body.contentType,
components: body.components,
@ -89,6 +96,9 @@ module.exports = {
try {
strapi.reload.isWatching = false;
const contentTypeService =
strapi.plugins['content-type-builder'].services.contenttypes;
const component = await contentTypeService.editContentType(uid, {
contentType: body.contentType,
components: body.components,
@ -113,6 +123,9 @@ module.exports = {
try {
strapi.reload.isWatching = false;
const contentTypeService =
strapi.plugins['content-type-builder'].services.contenttypes;
const component = await contentTypeService.deleteContentType(uid);
setImmediate(() => strapi.reload());