UpdateSystemUseCase.cs 1.8 KB

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