Event Name
CreateEntityService
Event Parameters
The entity object for which the service is being created.
The name of the resource (typically the entity name).
The directory where the API services are being generated.
An object containing the CRUD actions available for the entity.
afterCreateEntityService(
context: dotnetTypes.DsgContext,
eventParams: dotnet.CreateEntityServiceParams,
files: FileMap<Class>
): Promise<FileMap<Class>> {
const { entity, resourceName, apisDir } = eventParams;
const servicePath = `${apisDir}/${entity.name}/${pascalCase(entity.name)}Service.cs`;
const serviceFile = files.get(servicePath);
if (serviceFile) {
// Add a custom method to the service
serviceFile.code.addMethod(
CsharpSupport.method({
name: "GetRecentlyModified",
access: "public",
isAsync: true,
returnType: CsharpSupport.Types.task(CsharpSupport.Types.list(CsharpSupport.Types.reference(entity.name))),
parameters: [
CsharpSupport.parameter({
name: "days",
type: CsharpSupport.Types.int(),
defaultValue: "7",
}),
],
body: `
var cutoffDate = DateTime.UtcNow.AddDays(-days);
return await _repository.GetAll()
.Where(e => e.UpdatedAt >= cutoffDate)
.OrderByDescending(e => e.UpdatedAt)
.ToListAsync();
`,
})
);
// Add necessary imports
serviceFile.code.addImport("System");
serviceFile.code.addImport("System.Linq");
serviceFile.code.addImport("Microsoft.EntityFrameworkCore");
}
return files;
}