afterCreateEntityControllerBase(
  context: dotnetTypes.DsgContext,
  eventParams: dotnet.CreateEntityControllerBaseParams,
  files: FileMap<Class>
): Promise<FileMap<Class>> {
  const { entity, resourceName, apisDir } = eventParams;
  const controllerBasePath = `${apisDir}/${entity.name}/Base/${pascalCase(entity.name)}ControllerBase.cs`;
  const controllerBaseFile = files.get(controllerBasePath);

  if (controllerBaseFile) {
    // Add a protected method to the base controller
    controllerBaseFile.code.addMethod(
      CsharpSupport.method({
        name: "ValidateEntityState",
        access: "protected",
        returnType: CsharpSupport.Types.boolean(),
        parameters: [
          CsharpSupport.parameter({
            name: "entity",
            type: CsharpSupport.Types.reference(entity.name),
          }),
        ],
        body: `
          if (entity == null)
            return false;

          // Add custom validation logic here
          return true;
        `,
      })
    );

    // Modify existing methods to use the new validation
    const methods = controllerBaseFile.code.getMethods();
    methods.forEach(method => {
      if (method.name === `Create${entity.name}` || method.name === `Update${entity.name}`) {
        const existingBody = method.body;
        method.body = `
          if (!ValidateEntityState(${camelCase(entity.name)}))
            return BadRequest("Invalid entity state");

          ${existingBody}
        `;
      }
    });
  }

  return files;
}

Event Name

CreateEntityControllerBase

Event Parameters

entity
Entity

The entity object for which the base controller is being created.

resourceName
string

The name of the resource (typically the entity name).

apisDir
string

The directory where the API controllers are being generated.

moduleActions
ModuleAction[]

An array of module actions available for the entity.

entities
Entity[]

An array of all entities in the application.

afterCreateEntityControllerBase(
  context: dotnetTypes.DsgContext,
  eventParams: dotnet.CreateEntityControllerBaseParams,
  files: FileMap<Class>
): Promise<FileMap<Class>> {
  const { entity, resourceName, apisDir } = eventParams;
  const controllerBasePath = `${apisDir}/${entity.name}/Base/${pascalCase(entity.name)}ControllerBase.cs`;
  const controllerBaseFile = files.get(controllerBasePath);

  if (controllerBaseFile) {
    // Add a protected method to the base controller
    controllerBaseFile.code.addMethod(
      CsharpSupport.method({
        name: "ValidateEntityState",
        access: "protected",
        returnType: CsharpSupport.Types.boolean(),
        parameters: [
          CsharpSupport.parameter({
            name: "entity",
            type: CsharpSupport.Types.reference(entity.name),
          }),
        ],
        body: `
          if (entity == null)
            return false;

          // Add custom validation logic here
          return true;
        `,
      })
    );

    // Modify existing methods to use the new validation
    const methods = controllerBaseFile.code.getMethods();
    methods.forEach(method => {
      if (method.name === `Create${entity.name}` || method.name === `Update${entity.name}`) {
        const existingBody = method.body;
        method.body = `
          if (!ValidateEntityState(${camelCase(entity.name)}))
            return BadRequest("Invalid entity state");

          ${existingBody}
        `;
      }
    });
  }

  return files;
}