Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.amplication.com/llms.txt

Use this file to discover all available pages before exploring further.

Event Name

CreateEntityService

Event Parameters

entity
Entity
The entity object for which the service is being created.
resourceName
string
The name of the resource (typically the entity name).
apisDir
string
The directory where the API services are being generated.
entityActions
object
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;
}