|
| 1 | +// routes-generator — emits ASP.NET Core Minimal API CRUD endpoints per entity. |
| 2 | +// |
| 3 | +// One static class per entity with a Map<Entity>Routes(this IEndpointRouteBuilder, |
| 4 | +// string prefix) extension. Single-PK entities get full CRUD (list/get/post/put/ |
| 5 | +// delete) over the generated AppDbContext; composite-/no-PK entities get the |
| 6 | +// collection GET only (item addressing needs a single key) with a warning. |
| 7 | + |
| 8 | +using System.Text; |
| 9 | +using MetaObjects.Meta; |
| 10 | + |
| 11 | +namespace MetaObjects.Codegen.Generators; |
| 12 | + |
| 13 | +/// <summary>Generates Minimal API CRUD route registration per <c>object.entity</c>.</summary> |
| 14 | +public sealed class RoutesGenerator : PerEntityGenerator |
| 15 | +{ |
| 16 | + public override string Name => "routes-generator"; |
| 17 | + |
| 18 | + protected override bool Filter(MetaObject entity) => entity.IsEntity(); |
| 19 | + |
| 20 | + protected override EmittedFile GenerateOne(MetaObject entity, GenContext ctx) |
| 21 | + { |
| 22 | + var cls = CSharpNaming.Pascal(entity.Name); |
| 23 | + var dbSet = CSharpNaming.Pluralize(cls); |
| 24 | + var route = CSharpNaming.Pluralize(entity.Name).ToLowerInvariant(); |
| 25 | + |
| 26 | + var pkFields = entity.PrimaryIdentity()?.Fields ?? []; |
| 27 | + string? pkType = null; |
| 28 | + if (pkFields.Count == 1) |
| 29 | + { |
| 30 | + var pkField = entity.Fields().FirstOrDefault(f => f.Name == pkFields[0]); |
| 31 | + if (pkField is not null) pkType = CSharpNaming.ScalarFor(pkField.SubType); |
| 32 | + } |
| 33 | + bool fullCrud = pkType is not null; |
| 34 | + if (!fullCrud) |
| 35 | + ctx.Warn($"{Name}: entity \"{entity.Name}\" has no single-column primary key — emitting collection GET only."); |
| 36 | + |
| 37 | + var sb = new StringBuilder(); |
| 38 | + sb.AppendLine("// <auto-generated/>"); |
| 39 | + sb.AppendLine("// Generated by MetaObjects routes-generator. Do not edit by hand."); |
| 40 | + sb.AppendLine("#nullable enable"); |
| 41 | + sb.AppendLine("using Microsoft.AspNetCore.Builder;"); |
| 42 | + sb.AppendLine("using Microsoft.AspNetCore.Http;"); |
| 43 | + sb.AppendLine("using Microsoft.AspNetCore.Routing;"); |
| 44 | + sb.AppendLine("using Microsoft.EntityFrameworkCore;"); |
| 45 | + sb.AppendLine(); |
| 46 | + sb.AppendLine($"namespace {ctx.Config.Namespace};"); |
| 47 | + sb.AppendLine(); |
| 48 | + sb.AppendLine($"public static class {cls}Routes"); |
| 49 | + sb.AppendLine("{"); |
| 50 | + sb.AppendLine($" public static IEndpointRouteBuilder Map{cls}Routes(this IEndpointRouteBuilder app, string prefix = \"/api\")"); |
| 51 | + sb.AppendLine(" {"); |
| 52 | + |
| 53 | + // List |
| 54 | + sb.AppendLine(" app.MapGet(prefix + \"/" + route + "\", async (AppDbContext db) =>"); |
| 55 | + sb.AppendLine(" await db." + dbSet + ".ToListAsync());"); |
| 56 | + |
| 57 | + if (fullCrud) |
| 58 | + { |
| 59 | + sb.AppendLine(); |
| 60 | + sb.AppendLine(" app.MapGet(prefix + \"/" + route + "/{id}\", async (" + pkType + " id, AppDbContext db) =>"); |
| 61 | + sb.AppendLine(" await db." + dbSet + ".FindAsync(id) is { } found ? Results.Ok(found) : Results.NotFound());"); |
| 62 | + sb.AppendLine(); |
| 63 | + sb.AppendLine(" app.MapPost(prefix + \"/" + route + "\", async (" + cls + " input, AppDbContext db) =>"); |
| 64 | + sb.AppendLine(" {"); |
| 65 | + sb.AppendLine(" db." + dbSet + ".Add(input);"); |
| 66 | + sb.AppendLine(" await db.SaveChangesAsync();"); |
| 67 | + sb.AppendLine(" return Results.Created(prefix + \"/" + route + "\", input);"); |
| 68 | + sb.AppendLine(" });"); |
| 69 | + sb.AppendLine(); |
| 70 | + sb.AppendLine(" app.MapPut(prefix + \"/" + route + "/{id}\", async (" + pkType + " id, " + cls + " input, AppDbContext db) =>"); |
| 71 | + sb.AppendLine(" {"); |
| 72 | + sb.AppendLine(" var existing = await db." + dbSet + ".FindAsync(id);"); |
| 73 | + sb.AppendLine(" if (existing is null) return Results.NotFound();"); |
| 74 | + sb.AppendLine(" db.Entry(existing).CurrentValues.SetValues(input);"); |
| 75 | + sb.AppendLine(" await db.SaveChangesAsync();"); |
| 76 | + sb.AppendLine(" return Results.NoContent();"); |
| 77 | + sb.AppendLine(" });"); |
| 78 | + sb.AppendLine(); |
| 79 | + sb.AppendLine(" app.MapDelete(prefix + \"/" + route + "/{id}\", async (" + pkType + " id, AppDbContext db) =>"); |
| 80 | + sb.AppendLine(" {"); |
| 81 | + sb.AppendLine(" var existing = await db." + dbSet + ".FindAsync(id);"); |
| 82 | + sb.AppendLine(" if (existing is null) return Results.NotFound();"); |
| 83 | + sb.AppendLine(" db." + dbSet + ".Remove(existing);"); |
| 84 | + sb.AppendLine(" await db.SaveChangesAsync();"); |
| 85 | + sb.AppendLine(" return Results.NoContent();"); |
| 86 | + sb.AppendLine(" });"); |
| 87 | + } |
| 88 | + |
| 89 | + sb.AppendLine(); |
| 90 | + sb.AppendLine(" return app;"); |
| 91 | + sb.AppendLine(" }"); |
| 92 | + sb.AppendLine("}"); |
| 93 | + |
| 94 | + return new EmittedFile($"{cls}Routes.g.cs", sb.ToString()); |
| 95 | + } |
| 96 | +} |
0 commit comments