UpdateSystemUseCase.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. List<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 (runsOn?.Count > 0)
  37. {
  38. foreach(string parent in runsOn) {
  39. if (!string.IsNullOrWhiteSpace(parent)) {
  40. ThrowIfInvalid.ResourceName(parent);
  41. var parentHardware = await repository.GetByNameAsync(parent) as Hardware.Hardware;
  42. if (parentHardware == null) throw new NotFoundException($"Parent hardware '{parent}' not found.");
  43. if (!system.RunsOn.Contains(parent)) system.RunsOn.Add(parent);
  44. }
  45. }
  46. }
  47. await repository.UpdateAsync(system);
  48. }
  49. }