UpdateFirewallPortUseCase.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Hardware.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Firewalls.Ports;
  4. public class UpdateFirewallPortUseCase(IHardwareRepository repository) : IUseCase
  5. {
  6. public async Task ExecuteAsync(
  7. string name,
  8. int index,
  9. string? type,
  10. double? speed,
  11. int? ports)
  12. {
  13. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  14. // ToDo validate / normalize all inputs
  15. name = Normalize.HardwareName(name);
  16. ThrowIfInvalid.ResourceName(name);
  17. var nicType = Normalize.NicType(type);
  18. ThrowIfInvalid.NicType(nicType);
  19. var firewall = await repository.GetByNameAsync(name) as Firewall
  20. ?? throw new NotFoundException($"Firewall '{name}' not found.");
  21. if (firewall.Ports == null || index < 0 || index >= firewall.Ports.Count)
  22. throw new NotFoundException($"Port index {index} not found on firewall '{name}'.");
  23. var nic = firewall.Ports[index];
  24. nic.Type = nicType;
  25. nic.Speed = speed;
  26. nic.Count = ports;
  27. await repository.UpdateAsync(firewall);
  28. }
  29. }