AddNicUseCase.cs 1.5 KB

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