UpdateDesktopNicUseCase.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Hardware.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Desktops.Nics;
  4. public class UpdateDesktopNicUseCase(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 desktop = await repository.GetByNameAsync(name) as Desktop
  20. ?? throw new NotFoundException($"Desktop '{name}' not found.");
  21. if (desktop.Nics == null || index < 0 || index >= desktop.Nics.Count)
  22. throw new NotFoundException($"NIC index {index} not found on desktop '{name}'.");
  23. var nic = desktop.Nics[index];
  24. nic.Type = nicType;
  25. nic.Speed = speed;
  26. nic.Ports = ports;
  27. await repository.UpdateAsync(desktop);
  28. }
  29. }