UpdateSystemUseCase.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Hardware;
  3. namespace RackPeek.Domain.Resources.SystemResources.UseCases;
  4. public class UpdateSystemUseCase(ISystemRepository repository, IHardwareRepository hardwareRepo) : IUseCase
  5. {
  6. public async Task ExecuteAsync(
  7. string name,
  8. string? type = null,
  9. string? os = null,
  10. int? cores = null,
  11. int? ram = null,
  12. string? runsOn = null
  13. )
  14. {
  15. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  16. // ToDo validate / normalize all inputs
  17. name = Normalize.SystemName(name);
  18. ThrowIfInvalid.ResourceName(name);
  19. var system = await repository.GetByNameAsync(name);
  20. if (system is null)
  21. throw new InvalidOperationException($"System '{name}' not found.");
  22. if (!string.IsNullOrWhiteSpace(type))
  23. system.Type = type;
  24. if (!string.IsNullOrWhiteSpace(os))
  25. system.Os = os;
  26. if (cores.HasValue)
  27. system.Cores = cores.Value;
  28. if (ram.HasValue)
  29. system.Ram = ram.Value;
  30. if (!string.IsNullOrWhiteSpace(runsOn))
  31. {
  32. ThrowIfInvalid.ResourceName(runsOn);
  33. var parentHardware = await hardwareRepo.GetByNameAsync(runsOn);
  34. if (parentHardware == null) throw new NotFoundException($"Parent hardware '{runsOn}' not found.");
  35. system.RunsOn = runsOn;
  36. }
  37. await repository.UpdateAsync(system);
  38. }
  39. }