diff --git a/packages/strapi-generate-api/json/routes.json.js b/packages/strapi-generate-api/json/routes.json.js index 355d3c0e28..45dfd6dead 100755 --- a/packages/strapi-generate-api/json/routes.json.js +++ b/packages/strapi-generate-api/json/routes.json.js @@ -16,53 +16,42 @@ const _ = require('lodash'); */ module.exports = scope => { - const newRoutes = { - routes: {} + return { + routes: [{ + method: 'GET', + path: '/ ' + scope.humanizeId, + handler: scope.globalID + '.find', + config: { + policies: [] + } + }, { + method: 'GET', + path: '/ ' + scope.humanizeId + '/:id', + handler: scope.globalID + '.findOne', + config: { + policies: [] + } + }, { + method: 'POST', + path: '/ ' + scope.humanizeId, + handler: scope.globalID + '.create', + config: { + policies: [] + } + }, { + method: 'PUT', + path: '/ ' + scope.humanizeId + '/:id', + handler: scope.globalID + '.update', + config: { + policies: [] + } + }, { + method: 'DELETE', + path: '/ ' + scope.humanizeId + '/:id', + handler: scope.globalID + '.destroy', + config: { + policies: [] + } + }] }; - - newRoutes.routes['GET /' + scope.humanizeId] = { - controller: scope.globalID, - action: 'find', - policies: [] - }; - - newRoutes.routes['GET /' + scope.humanizeId + '/:id'] = { - controller: scope.globalID, - action: 'findOne', - policies: [] - }; - - newRoutes.routes['POST /' + scope.humanizeId] = { - controller: scope.globalID, - action: 'create', - policies: [] - }; - - newRoutes.routes['PUT /' + scope.humanizeId + '/:id'] = { - controller: scope.globalID, - action: 'update', - policies: [] - }; - - newRoutes.routes['DELETE /' + scope.humanizeId + '/:id'] = { - controller: scope.globalID, - action: 'destroy', - policies: [] - }; - - if (scope.template && scope.template !== 'mongoose') { - newRoutes.routes['POST /' + scope.humanizeId + '/:id/relationships/:relation'] = { - controller: scope.globalID, - action: 'createRelation', - policies: [] - }; - - newRoutes.routes['DELETE /' + scope.humanizeId + '/:id/relationships/:relation'] = { - controller: scope.globalID, - action: 'destroyRelation', - policies: [] - }; - } - - return newRoutes; }; diff --git a/packages/strapi-generate-api/templates/mongoose/controller.template b/packages/strapi-generate-api/templates/mongoose/controller.template index 3dc23e72df..cb44c0baba 100755 --- a/packages/strapi-generate-api/templates/mongoose/controller.template +++ b/packages/strapi-generate-api/templates/mongoose/controller.template @@ -11,11 +11,11 @@ module.exports = { * @return {Object|Array} */ - find: function * () { + find: async (ctx, next) => { try { - this.body = yield strapi.services.<%= humanizeId %>.fetchAll(this.query); + ctx.body = await strapi.services.<%= humanizeId %>.fetchAll(ctx.query); } catch (err) { - this.body = err; + ctx.body = err; } }, @@ -25,11 +25,11 @@ module.exports = { * @return {Object|Array} */ - findOne: function * () { + findOne: async (ctx, next) => { try { - this.body = yield strapi.services.<%= humanizeId %>.fetch(this.params) + ctx.body = await strapi.services.<%= humanizeId %>.fetch(ctx.params) } catch (err) { - this.body = err; + ctx.body = err; } }, @@ -39,11 +39,11 @@ module.exports = { * @return {Object} */ - create: function * () { + create: async (ctx, next) => { try { - this.body = yield strapi.services.<%= humanizeId %>.add(this.request.body); + ctx.body = await strapi.services.<%= humanizeId %>.add(ctx.request.body); } catch (err) { - this.body = err; + ctx.body = err; } }, @@ -53,11 +53,11 @@ module.exports = { * @return {Object} */ - update: function * () { + update: async (ctx, next) => { try { - this.body = yield strapi.services.<%= humanizeId %>.edit(this.params, this.request.body) ; + ctx.body = await strapi.services.<%= humanizeId %>.edit(ctx.params, ctx.request.body) ; } catch (err) { - this.body = err; + ctx.body = err; } }, @@ -67,11 +67,11 @@ module.exports = { * @return {Object} */ - destroy: function * () { + destroy: async (ctx, next) => { try { - this.body = yield strapi.services.<%= humanizeId %>.remove(this.params); + ctx.body = await strapi.services.<%= humanizeId %>.remove(ctx.params); } catch (err) { - this.body = err; + ctx.body = err; } } };