RemoveFirewallPortUseCase.cs 845 B

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