AddSwitchPortUseCase.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Hardware.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Switches.Ports;
  4. public class AddSwitchPortUseCase(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 Switch
  19. ?? throw new NotFoundException($"Switch '{name}' not found.");
  20. desktop.Ports ??= new List<Port>();
  21. desktop.Ports.Add(new Port
  22. {
  23. Type = nicType,
  24. Speed = speed,
  25. Count = ports
  26. });
  27. await repository.UpdateAsync(desktop);
  28. }
  29. }