Event Name
CreateEntityControllerBase
Event Parameters
The entity object for which the base controller is being created.
The name of the resource (typically the entity name).
The directory where the API controllers are being generated.
An array of module actions available for the 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;
}