UpdateSwitchUseCase.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. namespace RackPeek.Domain.Resources.Switches;
  4. public class UpdateSwitchUseCase(IResourceCollection repository) : IUseCase {
  5. public async Task ExecuteAsync(
  6. string name,
  7. string? model = null,
  8. bool? managed = null,
  9. bool? poe = null,
  10. string? notes = null
  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 switchResource = await repository.GetByNameAsync(name) as Switch;
  17. if (switchResource == null)
  18. throw new InvalidOperationException($"Switch '{name}' not found.");
  19. if (!string.IsNullOrWhiteSpace(model))
  20. switchResource.Model = model;
  21. if (managed.HasValue)
  22. switchResource.Managed = managed.Value;
  23. if (poe.HasValue)
  24. switchResource.Poe = poe.Value;
  25. if (notes != null) switchResource.Notes = notes;
  26. await repository.UpdateAsync(switchResource);
  27. }
  28. }