RemoveSwitchPortUseCase.cs 799 B

1234567891011121314151617181920212223
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Hardware.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Switches.Ports;
  4. public class RemoveSwitchPortUseCase(IHardwareRepository repository) : IUseCase
  5. {
  6. public async Task ExecuteAsync(string name, int index)
  7. {
  8. name = Normalize.HardwareName(name);
  9. ThrowIfInvalid.ResourceName(name);
  10. var Switch = await repository.GetByNameAsync(name) as Switch
  11. ?? throw new NotFoundException($"Switch '{name}' not found.");
  12. if (Switch.Ports == null || index < 0 || index >= Switch.Ports.Count)
  13. throw new NotFoundException($"Port index {index} not found on Switch '{name}'.");
  14. Switch.Ports.RemoveAt(index);
  15. await repository.UpdateAsync(Switch);
  16. }
  17. }