63 lines
2.3 KiB
C#
63 lines
2.3 KiB
C#
using System.CommandLine;
|
|
using MycroForge.CLI.CodeGen;
|
|
using MycroForge.Core;
|
|
using MycroForge.Core.Contract;
|
|
|
|
namespace MycroForge.CLI.Commands;
|
|
|
|
public partial class MycroForge
|
|
{
|
|
public partial class Db
|
|
{
|
|
public partial class Link
|
|
{
|
|
public class One : Command, ISubCommandOf<Link>
|
|
{
|
|
private readonly ProjectContext _context;
|
|
|
|
private static readonly Argument<string> LeftArgument =
|
|
new(name: "entity", description: "The left side of the relation");
|
|
|
|
private static readonly Option<string> ToOneOption =
|
|
new(name: "--to-one", description: "The right side of the relation");
|
|
|
|
private static readonly Option<string> ToManyOption =
|
|
new(name: "--to-many", description: "The right side of the relation");
|
|
|
|
|
|
public One(ProjectContext context) : base("one", "Define a 1:n relation")
|
|
{
|
|
_context = context;
|
|
AddArgument(LeftArgument);
|
|
AddOption(ToOneOption);
|
|
AddOption(ToManyOption);
|
|
this.SetHandler(ExecuteAsync, LeftArgument, ToOneOption, ToManyOption);
|
|
}
|
|
|
|
private async Task ExecuteAsync(string left, string? toOneOption, string? toManyOption)
|
|
{
|
|
if (toOneOption is not null && toManyOption is not null)
|
|
throw new Exception("Cannot set both --to-one and --to-many option.");
|
|
|
|
if (toOneOption is not null)
|
|
await ToOne(left, toOneOption);
|
|
|
|
else if (toManyOption is not null)
|
|
await ToMany(left, toManyOption);
|
|
|
|
else throw new Exception("Set --to-one or --to-many option.");
|
|
}
|
|
|
|
private async Task ToOne(string left, string toOneOption)
|
|
{
|
|
await new EntityLinker(_context, left, toOneOption).OneToOne();
|
|
}
|
|
|
|
private async Task ToMany(string left, string toManyOption)
|
|
{
|
|
await new EntityLinker(_context, left, toManyOption).OneToMany();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |