AddNicUseCase.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Hardware.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Servers.Nics;
  4. public class AddNicUseCase(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. if (speed.HasValue) ThrowIfInvalid.NicSpeed(speed.Value);
  17. if (ports.HasValue) ThrowIfInvalid.NicPorts(ports.Value);
  18. var nicType = Normalize.NicType(type);
  19. ThrowIfInvalid.NicType(nicType);
  20. var hardware = await repository.GetByNameAsync(name);
  21. if (hardware is not Server server)
  22. throw new NotFoundException($"Server: '{name}' not found.");
  23. server.Nics ??= [];
  24. server.Nics.Add(new Nic
  25. {
  26. Type = nicType,
  27. Speed = speed,
  28. Ports = ports
  29. });
  30. await repository.UpdateAsync(server);
  31. }
  32. }