Event Name
CreateEntityController
Event Parameters
The entity object for which the controller is being created.
The name of the resource (typically the entity name).
The directory where the API controllers are being generated.
An object containing the CRUD actions available for the entity.
afterCreateEntityController(
context: dotnetTypes.DsgContext,
eventParams: dotnet.CreateEntityControllerParams,
files: FileMap<Class>
): Promise<FileMap<Class>> {
const { entity, resourceName, apisDir } = eventParams;
const controllerPath = `${apisDir}/${entity.name}/${pascalCase(entity.name)}Controller.cs`;
const controllerFile = files.get(controllerPath);
if (controllerFile) {
// Add a custom action to the controller
controllerFile.code.addMethod(
CsharpSupport.method({
name: "ExportToCsv",
access: "public",
isAsync: true,
returnType: CsharpSupport.Types.task(CsharpSupport.Types.reference("IActionResult")),
decorators: [
CsharpSupport.decorator({
name: "HttpGet",
arguments: ["export-csv"],
}),
],
body: `
var allItems = await _service.List();
var csv = ConvertToCsv(allItems);
return File(Encoding.UTF8.GetBytes(csv), "text/csv", "${entity.name}Export.csv");
`,
})
);
// Add necessary imports
controllerFile.code.addImport("System.Text");
controllerFile.code.addImport("Microsoft.AspNetCore.Mvc");
}
return files;
}