UpdateSwitchPortUseCase.cs 1.2 KB

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