UpdateNicUseCase.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.Servers;
  5. namespace RackPeek.Domain.UseCases.Nics;
  6. public interface IUpdateNicUseCase<T> : IResourceUseCase<T>
  7. where T : Resource
  8. {
  9. public Task ExecuteAsync(
  10. string name,
  11. int index,
  12. string? type,
  13. double? speed,
  14. int? ports);
  15. }
  16. public class UpdateNicUseCase<T>(IResourceCollection repository) : IUpdateNicUseCase<T> where T : Resource
  17. {
  18. public async Task ExecuteAsync(
  19. string name,
  20. int index,
  21. string? type,
  22. double? speed,
  23. int? ports)
  24. {
  25. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  26. // ToDo validate / normalize all inputs
  27. name = Normalize.HardwareName(name);
  28. ThrowIfInvalid.ResourceName(name);
  29. var nicType = Normalize.NicType(type);
  30. ThrowIfInvalid.NicType(nicType);
  31. var resource = await repository.GetByNameAsync<T>(name) ??
  32. throw new NotFoundException($"Resource '{name}' not found.");
  33. if (resource is not INicResource nr) throw new NotFoundException($"Resource '{name}' not found.");
  34. if (nr.Nics == null || index < 0 || index >= nr.Nics.Count)
  35. throw new NotFoundException($"NIC index {index} not found on desktop '{name}'.");
  36. var nic = nr.Nics[index];
  37. nic.Type = nicType;
  38. nic.Speed = speed;
  39. nic.Ports = ports;
  40. await repository.UpdateAsync(resource);
  41. }
  42. }