AddDesktopNicUseCase.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Hardware.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Desktops.Nics;
  4. public class AddDesktopNicUseCase(IHardwareRepository repository) : IUseCase
  5. {
  6. public async Task ExecuteAsync(
  7. string name,
  8. string? type,
  9. double? speed,
  10. int? ports)
  11. {
  12. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  13. // ToDo validate / normalize all inputs
  14. name = Normalize.HardwareName(name);
  15. ThrowIfInvalid.ResourceName(name);
  16. var nicType = Normalize.NicType(type);
  17. ThrowIfInvalid.NicType(nicType);
  18. var desktop = await repository.GetByNameAsync(name) as Desktop
  19. ?? throw new NotFoundException($"Desktop '{name}' not found.");
  20. desktop.Nics ??= new List<Nic>();
  21. desktop.Nics.Add(new Nic
  22. {
  23. Type = nicType,
  24. Speed = speed,
  25. Ports = ports
  26. });
  27. await repository.UpdateAsync(desktop);
  28. }
  29. }