LaptopCpuRemoveCommand.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using System.ComponentModel;
  2. using Microsoft.Extensions.DependencyInjection;
  3. using RackPeek.Domain.Resources.Laptops;
  4. using RackPeek.Domain.UseCases.Cpus;
  5. using Spectre.Console;
  6. using Spectre.Console.Cli;
  7. namespace Shared.Rcl.Commands.Laptops.Cpus;
  8. public class LaptopCpuRemoveSettings : CommandSettings
  9. {
  10. [CommandArgument(0, "<Laptop>")]
  11. [Description("The name of the Laptop.")]
  12. public string LaptopName { get; set; } = default!;
  13. [CommandArgument(1, "<index>")]
  14. [Description("The index of the Laptop cpu to remove.")]
  15. public int Index { get; set; }
  16. }
  17. public class LaptopCpuRemoveCommand(IServiceProvider provider)
  18. : AsyncCommand<LaptopCpuRemoveSettings>
  19. {
  20. public override async Task<int> ExecuteAsync(
  21. CommandContext context,
  22. LaptopCpuRemoveSettings settings,
  23. CancellationToken cancellationToken)
  24. {
  25. using var scope = provider.CreateScope();
  26. var useCase = scope.ServiceProvider.GetRequiredService<IRemoveCpuUseCase<Laptop>>();
  27. await useCase.ExecuteAsync(settings.LaptopName, settings.Index);
  28. AnsiConsole.MarkupLine($"[green]CPU #{settings.Index} removed from Laptop '{settings.LaptopName}'.[/]");
  29. return 0;
  30. }
  31. }