UpdateSwitchUseCase.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Switches;
  4. public class UpdateSwitchUseCase(IHardwareRepository repository) : IUseCase
  5. {
  6. public async Task ExecuteAsync(
  7. string name,
  8. string? model = null,
  9. bool? managed = null,
  10. bool? poe = null,
  11. string? notes = null
  12. )
  13. {
  14. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  15. // ToDo validate / normalize all inputs
  16. name = Normalize.HardwareName(name);
  17. ThrowIfInvalid.ResourceName(name);
  18. var switchResource = await repository.GetByNameAsync(name) as Switch;
  19. if (switchResource == null)
  20. throw new InvalidOperationException($"Switch '{name}' not found.");
  21. if (!string.IsNullOrWhiteSpace(model))
  22. switchResource.Model = model;
  23. if (managed.HasValue)
  24. switchResource.Managed = managed.Value;
  25. if (poe.HasValue)
  26. switchResource.Poe = poe.Value;
  27. if (notes != null)
  28. {
  29. switchResource.Notes = notes;
  30. }
  31. await repository.UpdateAsync(switchResource);
  32. }
  33. }