RemovePortUseCase.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.Hardware.Firewalls;
  5. using RackPeek.Domain.Resources.Hardware.Servers;
  6. namespace RackPeek.Domain.UseCases.Ports;
  7. public interface IRemovePortUseCase<T> : IResourceUseCase<T>
  8. where T : Resource
  9. {
  10. public Task ExecuteAsync(string name, int index);
  11. }
  12. public class RemovePortUseCase<T>(IResourceCollection repository) : IRemovePortUseCase<T> where T : Resource
  13. {
  14. public async Task ExecuteAsync(string name, int index)
  15. {
  16. name = Normalize.HardwareName(name);
  17. ThrowIfInvalid.ResourceName(name);
  18. var resource = await repository.GetByNameAsync<T>(name)
  19. ?? throw new NotFoundException($"Resource '{name}' not found.");
  20. if (resource is not IPortResource pr)
  21. {
  22. throw new NotFoundException($"Resource '{name}' not found.");
  23. }
  24. if (pr.Ports == null || index < 0 || index >= pr.Ports.Count)
  25. throw new NotFoundException($"Port index {index} not found on '{name}'.");
  26. pr.Ports.RemoveAt(index);
  27. await repository.UpdateAsync(resource);
  28. }
  29. }