LaptopLabelAddCommand.cs 1.1 KB

1234567891011121314151617181920212223
  1. using Microsoft.Extensions.DependencyInjection;
  2. using RackPeek.Domain.Resources.Laptops;
  3. using RackPeek.Domain.UseCases.Labels;
  4. using Spectre.Console;
  5. using Spectre.Console.Cli;
  6. namespace Shared.Rcl.Commands.Laptops.Labels;
  7. public class LaptopLabelAddSettings : LaptopNameSettings {
  8. [CommandOption("--key <KEY>")] public string Key { get; set; } = default!;
  9. [CommandOption("--value <VALUE>")] public string Value { get; set; } = default!;
  10. }
  11. public class LaptopLabelAddCommand(IServiceProvider serviceProvider) : AsyncCommand<LaptopLabelAddSettings> {
  12. public override async Task<int> ExecuteAsync(CommandContext context, LaptopLabelAddSettings settings,
  13. CancellationToken cancellationToken) {
  14. using IServiceScope scope = serviceProvider.CreateScope();
  15. IAddLabelUseCase<Laptop> useCase = scope.ServiceProvider.GetRequiredService<IAddLabelUseCase<Laptop>>();
  16. await useCase.ExecuteAsync(settings.Name, settings.Key, settings.Value);
  17. AnsiConsole.MarkupLine($"[green]Label '{settings.Key}' added to '{settings.Name}'.[/]");
  18. return 0;
  19. }
  20. }