UpdateUpsUseCase.cs 935 B

1234567891011121314151617181920212223242526272829303132
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.UpsUnits;
  4. public class UpdateUpsUseCase(IHardwareRepository repository) : IUseCase
  5. {
  6. public async Task ExecuteAsync(
  7. string name,
  8. string? model = null,
  9. int? va = null
  10. )
  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 ups = await repository.GetByNameAsync(name) as Ups;
  17. if (ups == null)
  18. throw new InvalidOperationException($"UPS '{name}' not found.");
  19. if (!string.IsNullOrWhiteSpace(model))
  20. ups.Model = model;
  21. if (va.HasValue)
  22. ups.Va = va.Value;
  23. await repository.UpdateAsync(ups);
  24. }
  25. }