mycroforge/MycroForge.CLI/Commands/MycroForge.Api.Generate.Crud.cs

45 lines
1.7 KiB
C#

using System.CommandLine;
using MycroForge.CLI.CodeGen;
using MycroForge.CLI.Commands.Interfaces;
namespace MycroForge.CLI.Commands;
public partial class MycroForge
{
public partial class Api
{
public partial class Generate
{
public class Crud : Command, ISubCommandOf<Generate>
{
private static readonly Argument<string> EntityArgument =
new(name: "entity", description: "The entity to target");
private readonly ProjectContext _context;
public Crud(ProjectContext context)
: base("crud", "Generated CRUD functionality for an entity")
{
_context = context;
AddArgument(EntityArgument);
this.SetHandler(ExecuteAsync, EntityArgument);
}
private async Task ExecuteAsync(string entity)
{
var path = string.Empty;
if (entity.Split(':').Select(s => s.Trim()).ToArray() is { Length: 2 } fullName)
{
path = fullName[0];
entity = fullName[1];
}
await new CrudServiceGenerator(_context).Generate(path, entity);
await new RequestClassGenerator(_context).Generate(path, entity, RequestClassGenerator.Type.Create);
await new RequestClassGenerator(_context).Generate(path, entity, RequestClassGenerator.Type.Update);
await new CrudRouterGenerator(_context).Generate(path, entity);
}
}
}
}
}