Skip to content
This repository was archived by the owner on Feb 6, 2025. It is now read-only.

Commit c1347a6

Browse files
committed
(docs): Updating the examples for the .NET Reference functions based on the current real .net plugins and feedback.
1 parent dda9d43 commit c1347a6

19 files changed

+427
-276
lines changed

docs/plugins/dotnet-plugin-events/create-dtos.md

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,24 @@ export interface CreateDTOsParams extends EventParams {
2525
}
2626
```
2727

28-
Example:
28+
### Example
2929

3030
```ts
31-
async afterCreateDTOs(
32-
context: DsgContext,
33-
eventParams: CreateDTOsParams,
34-
files: FileMap<F>
35-
) {
36-
const { entity, dtoName, dtoBasePath } = eventParams;
37-
const dtoPath = join(dtoBasePath, `${dtoName}.cs`);
38-
const dtoFile = files.get(dtoPath);
39-
31+
afterCreateDTOs(
32+
context: dotnetTypes.DsgContext,
33+
eventParams: dotnet.CreateDTOsParams,
34+
files: FileMap<Class>
35+
): Promise<FileMap<Class>> {
36+
const { entity, dtoName } = eventParams;
37+
const dtoFile = files.get(`DTOs/${dtoName}.cs`);
4038
if (dtoFile) {
41-
const updatedCode = dtoFile.code + `
42-
public class ${entity.name}SummaryDTO
43-
{
44-
public int Id { get; set; }
45-
public string Name { get; set; }
46-
}
47-
`;
48-
49-
files.set({
50-
path: dtoPath,
51-
code: updatedCode
52-
});
39+
dtoFile.code.addProperty(
40+
CsharpSupport.property({
41+
name: "LastModified",
42+
type: CsharpSupport.Types.dateTime(),
43+
})
44+
);
5345
}
54-
5546
return files;
5647
}
5748
```

docs/plugins/dotnet-plugin-events/create-entity-controller-base.md

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,27 +35,53 @@ An array of module actions available for the entity.
3535

3636
An array of all entities in the application.
3737

38-
Example:
38+
### Example
3939

4040
```ts
41-
async afterCreateEntityControllerBase(
42-
context: DsgContext,
43-
eventParams: CreateEntityControllerBaseParams,
44-
files: FileMap<F>
45-
) {
46-
const { resourceName, apisDir } = eventParams;
47-
const controllerBasePath = join(apisDir, `${resourceName}ControllerBase.cs`);
41+
afterCreateEntityControllerBase(
42+
context: dotnetTypes.DsgContext,
43+
eventParams: dotnet.CreateEntityControllerBaseParams,
44+
files: FileMap<Class>
45+
): Promise<FileMap<Class>> {
46+
const { entity, resourceName, apisDir } = eventParams;
47+
const controllerBasePath = `${apisDir}/${entity.name}/Base/${pascalCase(entity.name)}ControllerBase.cs`;
4848
const controllerBaseFile = files.get(controllerBasePath);
4949

5050
if (controllerBaseFile) {
51-
const updatedCode = controllerBaseFile.code.replace(
52-
"public abstract class",
53-
"[ApiController]\n[Route(\"api/[controller]\")]\npublic abstract class"
51+
// Add a protected method to the base controller
52+
controllerBaseFile.code.addMethod(
53+
CsharpSupport.method({
54+
name: "ValidateEntityState",
55+
access: "protected",
56+
returnType: CsharpSupport.Types.boolean(),
57+
parameters: [
58+
CsharpSupport.parameter({
59+
name: "entity",
60+
type: CsharpSupport.Types.reference(entity.name),
61+
}),
62+
],
63+
body: `
64+
if (entity == null)
65+
return false;
66+
67+
// Add custom validation logic here
68+
return true;
69+
`,
70+
})
5471
);
5572

56-
files.set({
57-
path: controllerBasePath,
58-
code: updatedCode
73+
// Modify existing methods to use the new validation
74+
const methods = controllerBaseFile.code.getMethods();
75+
methods.forEach(method => {
76+
if (method.name === `Create${entity.name}` || method.name === `Update${entity.name}`) {
77+
const existingBody = method.body;
78+
method.body = `
79+
if (!ValidateEntityState(${camelCase(entity.name)}))
80+
return BadRequest("Invalid entity state");
81+
82+
${existingBody}
83+
`;
84+
}
5985
});
6086
}
6187

docs/plugins/dotnet-plugin-events/create-entity-controller.md

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,43 @@ The directory where the API controllers are being generated.
4242

4343
An object containing the CRUD actions available for the entity.
4444

45-
Example:
45+
### Example
4646

4747
```ts
48-
async afterCreateEntityController(
49-
context: DsgContext,
50-
eventParams: CreateEntityControllerParams,
51-
files: FileMap<F>
52-
) {
48+
afterCreateEntityController(
49+
context: dotnetTypes.DsgContext,
50+
eventParams: dotnet.CreateEntityControllerParams,
51+
files: FileMap<Class>
52+
): Promise<FileMap<Class>> {
5353
const { entity, resourceName, apisDir } = eventParams;
54-
const controllerPath = join(apisDir, `${resourceName}Controller.cs`);
54+
const controllerPath = `${apisDir}/${entity.name}/${pascalCase(entity.name)}Controller.cs`;
5555
const controllerFile = files.get(controllerPath);
5656

5757
if (controllerFile) {
58-
const updatedCode = controllerFile.code.replace(
59-
"public class",
60-
"[ApiVersion(\"1.0\")]\npublic class"
58+
// Add a custom action to the controller
59+
controllerFile.code.addMethod(
60+
CsharpSupport.method({
61+
name: "ExportToCsv",
62+
access: "public",
63+
isAsync: true,
64+
returnType: CsharpSupport.Types.task(CsharpSupport.Types.reference("IActionResult")),
65+
decorators: [
66+
CsharpSupport.decorator({
67+
name: "HttpGet",
68+
arguments: ["export-csv"],
69+
}),
70+
],
71+
body: `
72+
var allItems = await _service.List();
73+
var csv = ConvertToCsv(allItems);
74+
return File(Encoding.UTF8.GetBytes(csv), "text/csv", "${entity.name}Export.csv");
75+
`,
76+
})
6177
);
6278

63-
files.set({
64-
path: controllerPath,
65-
code: updatedCode
66-
});
79+
// Add necessary imports
80+
controllerFile.code.addImport("System.Text");
81+
controllerFile.code.addImport("Microsoft.AspNetCore.Mvc");
6782
}
6883

6984
return files;

docs/plugins/dotnet-plugin-events/create-entity-extensions.md

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,61 @@ export interface CreateEntityExtensionsParams extends EventParams {
2525
}
2626
```
2727

28-
Example:
28+
### Example
2929

3030
```ts
31-
async afterCreateEntityExtensions(
32-
context: DsgContext,
33-
eventParams: CreateEntityExtensionsParams,
34-
files: FileMap<F>
35-
) {
36-
const { entity, apisDir } = eventParams;
37-
const extensionsPath = join(apisDir, "Extensions", `${entity.name}Extensions.cs`);
31+
afterCreateEntityExtensions(
32+
context: dotnetTypes.DsgContext,
33+
eventParams: dotnet.CreateEntityExtensionsParams,
34+
files: FileMap<Class>
35+
): Promise<FileMap<Class>> {
36+
const { entity, resourceName, apisDir } = eventParams;
37+
const extensionsPath = `${apisDir}/${entity.name}/${pascalCase(entity.name)}Extensions.cs`;
3838
const extensionsFile = files.get(extensionsPath);
3939

4040
if (extensionsFile) {
41-
const updatedCode = extensionsFile.code + `
42-
public static string GetDisplayName(this ${entity.name} entity)
43-
{
44-
return $"{entity.FirstName} {entity.LastName}";
45-
}
46-
`;
47-
48-
files.set({
49-
path: extensionsPath,
50-
code: updatedCode
51-
});
41+
// Add a custom extension method
42+
extensionsFile.code.addMethod(
43+
CsharpSupport.method({
44+
name: "ToAuditString",
45+
isStatic: true,
46+
returnType: CsharpSupport.Types.string(),
47+
parameters: [
48+
CsharpSupport.parameter({
49+
name: "this",
50+
type: CsharpSupport.Types.reference(entity.name),
51+
isThis: true,
52+
}),
53+
],
54+
body: `
55+
return $"{entity.Id}|{entity.CreatedAt}|{entity.UpdatedAt}";
56+
`,
57+
})
58+
);
59+
60+
// Add a custom mapper extension
61+
extensionsFile.code.addMethod(
62+
CsharpSupport.method({
63+
name: "ToDto",
64+
isStatic: true,
65+
returnType: CsharpSupport.Types.reference(`${entity.name}Dto`),
66+
parameters: [
67+
CsharpSupport.parameter({
68+
name: "this",
69+
type: CsharpSupport.Types.reference(entity.name),
70+
isThis: true,
71+
}),
72+
],
73+
body: `
74+
return new ${entity.name}Dto
75+
{
76+
Id = entity.Id,
77+
// Map other properties here
78+
AuditString = entity.ToAuditString()
79+
};
80+
`,
81+
})
82+
);
5283
}
5384

5485
return files;

docs/plugins/dotnet-plugin-events/create-entity-interface.md

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,29 +28,24 @@ export interface CreateEntityInterfaceParams extends EventParams {
2828
}
2929
```
3030

31-
Example:
31+
### Example
3232

3333
```ts
34-
async afterCreateEntityInterface(
35-
context: DsgContext,
36-
eventParams: CreateEntityInterfaceParams,
37-
files: FileMap<F>
38-
) {
39-
const { entity, apisDir } = eventParams;
40-
const interfacePath = join(apisDir, "Interfaces", `I${entity.name}.cs`);
41-
const interfaceFile = files.get(interfacePath);
42-
34+
afterCreateEntityInterface(
35+
context: dotnetTypes.DsgContext,
36+
eventParams: dotnet.CreateEntityInterfaceParams,
37+
files: FileMap<Interface>
38+
): Promise<FileMap<Interface>> {
39+
const { entity } = eventParams;
40+
const interfaceFile = files.get(`Interfaces/I${entity.name}.cs`);
4341
if (interfaceFile) {
44-
const updatedCode = interfaceFile.code + `
45-
Task<bool> IsUnique(string name);
46-
`;
47-
48-
files.set({
49-
path: interfacePath,
50-
code: updatedCode
51-
});
42+
interfaceFile.code.addMethod(
43+
CsharpSupport.method({
44+
name: "Validate",
45+
returnType: CsharpSupport.Types.boolean(),
46+
})
47+
);
5248
}
53-
5449
return files;
5550
}
5651
```

docs/plugins/dotnet-plugin-events/create-entity-model.md

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,24 @@ export interface CreateEntityModelParams extends EventParams {
2626
}
2727
```
2828

29-
Example:
29+
### Example
3030

3131
```ts
32-
async afterCreateEntityModel(
33-
context: DsgContext,
34-
eventParams: CreateEntityModelParams,
35-
files: FileMap<F>
36-
) {
37-
const { entity, resourceName, apisDir } = eventParams;
38-
const modelPath = join(apisDir, "Models", `${resourceName}.cs`);
39-
const modelFile = files.get(modelPath);
40-
32+
afterCreateEntityModel(
33+
context: dotnetTypes.DsgContext,
34+
eventParams: dotnet.CreateEntityModelParams,
35+
files: FileMap<Class>
36+
): Promise<FileMap<Class>> {
37+
const { entity, resourceName } = eventParams;
38+
const modelFile = files.get(`${resourceName}/Models/${entity.name}.cs`);
4139
if (modelFile) {
42-
const updatedCode = modelFile.code.replace(
43-
"public class",
44-
"[Table(\"" + entity.name + "\")]\npublic class"
40+
modelFile.code.addAttribute(
41+
CsharpSupport.attribute({
42+
name: "Table",
43+
arguments: [`"${entity.name}s"`],
44+
})
4545
);
46-
47-
files.set({
48-
path: modelPath,
49-
code: updatedCode
50-
});
5146
}
52-
5347
return files;
5448
}
5549
```

0 commit comments

Comments
 (0)