From 31ad1dfbb76bb8b6d6039b5933b76dc54cbc5965 Mon Sep 17 00:00:00 2001 From: Pierre Burgy Date: Sat, 6 May 2017 17:27:24 +0200 Subject: [PATCH] Dynamic collection selection --- .../controllers/ContentManager.js | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/packages/strapi-plugin-content-manager/controllers/ContentManager.js b/packages/strapi-plugin-content-manager/controllers/ContentManager.js index 1f8db59618..bb072ad2ba 100644 --- a/packages/strapi-plugin-content-manager/controllers/ContentManager.js +++ b/packages/strapi-plugin-content-manager/controllers/ContentManager.js @@ -11,8 +11,9 @@ module.exports = { }, find: async(ctx) => { - const model = ctx.params.model; - const primaryKey = strapi.models[model].primaryKey; + const model = strapi.models[ctx.params.model]; + const collection = global[model.globalName]; + const primaryKey = model.primaryKey; const { limit = 10, @@ -20,7 +21,7 @@ module.exports = { sort = primaryKey } = ctx.request.query; - const entries = await User + const entries = await collection .find() .limit(Number(limit)) .sort(sort) @@ -30,9 +31,10 @@ module.exports = { }, count: async(ctx) => { - const model = ctx.params.model; + const model = strapi.models[ctx.params.model]; + const collection = global[model.globalName]; - const count = await User + const count = await collection .count(); ctx.body = { @@ -41,45 +43,50 @@ module.exports = { }, findOne: async(ctx) => { - const model = ctx.params.model; - const primaryKey = strapi.models[model].primaryKey; + const model = strapi.models[ctx.params.model]; + const collection = global[model.globalName]; + const primaryKey = model.primaryKey; const params = {}; params[primaryKey] = ctx.params.id; - const entry = await User + const entry = await collection .findOne(params); ctx.body = entry; }, create: async(ctx) => { - const model = ctx.params.model; + const model = strapi.models[ctx.params.model]; + const collection = global[model.globalName]; - const entryCreated = await User + const entryCreated = await collection .create(ctx.request.body); ctx.body = entryCreated; }, update: async(ctx) => { - const model = ctx.params.model; - const primaryKey = strapi.models[model].primaryKey; + const model = strapi.models[ctx.params.model]; + const collection = global[model.globalName]; + const primaryKey = model.primaryKey; const params = {}; + params[primaryKey] = ctx.params.id; - const entryUpdated = await User + const entryUpdated = await collection .update(params, ctx.request.body); ctx.body = entryUpdated; }, delete: async(ctx) => { - const model = ctx.params.model; - const primaryKey = strapi.models[model].primaryKey; + const model = strapi.models[ctx.params.model]; + const collection = global[model.globalName]; + const primaryKey = model.primaryKey; const params = {}; params[primaryKey] = ctx.params.id; - const entryDeleted = await User + const entryDeleted = await collection .remove(params); ctx.body = entryDeleted;