AddSwitchPortUseCase.cs 1.1 KB

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