4
0

UpdateSystemUseCase.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. 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);
  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)
  36. {
  37. system.Notes = notes;
  38. }
  39. if (!string.IsNullOrWhiteSpace(runsOn))
  40. {
  41. ThrowIfInvalid.ResourceName(runsOn);
  42. var parentHardware = await hardwareRepo.GetByNameAsync(runsOn);
  43. if (parentHardware == null) throw new NotFoundException($"Parent hardware '{runsOn}' not found.");
  44. system.RunsOn = runsOn;
  45. }
  46. await repository.UpdateAsync(system);
  47. }
  48. }