UpdateSystemUseCase.cs 1.8 KB

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