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 Many : Command, ISubCommandOf { private readonly ProjectContext _context; private static readonly Argument LeftArgument = new(name: "entity", description: "The left side of the relation"); private static readonly Option ToOneOption = new(name: "--to-one", description: "The right side of the relation"); private static readonly Option ToManyOption = new(name: "--to-many", description: "The right side of the relation"); public Many(ProjectContext context) : base("many", "Define a n:m 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).ManyToOne(); } private async Task ToMany(string left, string toManyOption) { await new EntityLinker(_context, left, toManyOption).ManyToMany(); } } } } }