Closed wtmaxim closed 4 months ago
public static class GetTodoEndpoint
{
internal static RouteHandlerBuilder MapGetTodoEndpoint(this IEndpointRouteBuilder endpoints)
{
return endpoints.MapGet("/{id:guid}", async (Guid id, ISender mediator, ICodeGenerationService codeGenerationService) =>
{
var response = await mediator.Send(new GetTodoRequest(id));
// Utilisation du service de génération de code
var template = new CodeGenTemplate(response);
codeGenerationService.GenerateCode(template, "codegen.cs");
return Results.Ok(response);
})
.WithName(nameof(GetTodoEndpoint))
.WithSummary("gets todo item by id")
.WithDescription("gets todo item by id")
.Produces<GetTodoResponse>()
.RequirePermission("Permissions.Todos.View")
.MapToApiVersion(1);
}
}```
``` public class CodeGenTemplate : ICodeTemplate
{
private readonly GetTodoResponse _response;
public CodeGenTemplate(GetTodoResponse response)
{
_response = response;
}
public void Generate(CodegenTextWriter writer)
{
writer.WriteLine("public class GetTodoResponse");
writer.WriteLine("{");
writer.WriteLine($"public Guid Id {{ get; set; }} = Guid.NewGuid();");
writer.WriteLine($"public string Title {{ get; set; }} = \"{_response.Title}\";");
writer.WriteLine($"public string Description {{ get; set; }} = \"{_response.Notes}\";");
writer.WriteLine("}");
}
}
use https://github.com/Drizin/CodegenCS