UpdateNicUseCase.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System.ComponentModel.DataAnnotations;
  2. using RackPeek.Domain.Helpers;
  3. using RackPeek.Domain.Resources.Models;
  4. namespace RackPeek.Domain.Resources.Hardware.Servers.Nics;
  5. public class UpdateNicUseCase(IHardwareRepository 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. if (speed.HasValue) ThrowIfInvalid.NicSpeed(speed.Value);
  19. if (ports.HasValue) ThrowIfInvalid.NicPorts(ports.Value);
  20. var nicType = Normalize.NicType(type);
  21. ThrowIfInvalid.NicType(nicType);
  22. var hardware = await repository.GetByNameAsync(name);
  23. if (hardware is not Server server)
  24. throw new NotFoundException($"Server: '{name}' not found.");
  25. server.Nics ??= [];
  26. if (index < 0 || index >= server.Nics.Count)
  27. throw new ValidationException("NIC index out of range.");
  28. var nic = server.Nics[index];
  29. nic.Type = nicType;
  30. nic.Speed = speed;
  31. nic.Ports = ports;
  32. await repository.UpdateAsync(server);
  33. }
  34. }