custom controllers for camelCase collection types in strapi

Viewed 432

I've created a bunch of collections in strapi using cammelCase: enter image description here

However, this created the following file structure: /api/class-resource/controllers/class-resource.js

Now, when I try to follow this guide to create a custom controller, the js compiler won't let me call the service I need because it thinks I'm subtracting:

module.exports = {
  /**
   * Create a record.
   *
   * @return {Object}
   */

  async create(ctx) {
    let entity;
    if (ctx.is('multipart')) {
      const { data, files } = parseMultipartData(ctx);
      data.author = ctx.state.user.id;
      entity = await strapi.services.class-resource.create(data, { files });  //problem here
    } else {
      ctx.request.body.author = ctx.state.user.id;
      entity = await strapi.services.class-resource.create(ctx.request.body); //problem here
    }
    return sanitizeEntity(entity, { model: strapi.models.class-resource}); //problem here
  },
};

is there any way around this, other than renaming all my content types?

2 Answers

This solution was given to me by Pascal Isman in the strapi community slack: strapi.services['camel-case'].create(...)

I used the Joey code to replace findOne per ID by slug seem to work

/controllers

const { sanitizeEntity } = require("strapi-utils");

module.exports = {
  async findOne(ctx) {
    const { slug } = ctx.params;

    const entity = await strapi.services["blog-post"].findOne({ slug });
    return sanitizeEntity(entity, { model: strapi.models["blog-post"] });
  },
};

/config

{
      "method": "GET",
      "path": "/blog-posts/:slug",
      "handler": "blog-post.findOne",
      "config": {
        "policies": []
      }
    },
Related