UpdatePortUseCase.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.Ports;
  6. public interface IUpdatePortUseCase<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 UpdatePortUseCase<T>(IResourceCollection repository) : IUpdatePortUseCase<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 IPortResource pr) throw new NotFoundException($"Resource '{name}' not found.");
  34. if (pr.Ports == null || index < 0 || index >= pr.Ports.Count)
  35. throw new NotFoundException($"Port index {index} not found on '{name}'.");
  36. var nic = pr.Ports[index];
  37. nic.Type = nicType;
  38. nic.Speed = speed;
  39. nic.Count = ports;
  40. await repository.UpdateAsync(resource);
  41. }
  42. }