4
0

UpdateSystemUseCase.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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? ip = null,
  13. List<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 (ip != null) system.Ip = ip;
  37. if (notes != null) system.Notes = notes;
  38. if (runsOn?.Count > 0)
  39. foreach (var parent in runsOn)
  40. if (!string.IsNullOrWhiteSpace(parent))
  41. {
  42. ThrowIfInvalid.ResourceName(parent);
  43. Resource? parentHardware = await repository.GetByNameAsync(parent);
  44. if (parentHardware == null) throw new NotFoundException($"Parent '{parent}' not found.");
  45. if (parentHardware is not Hardware.Hardware and not SystemResource)
  46. throw new Exception("System cannot run on this resource.");
  47. if (!system.RunsOn.Contains(parent)) system.RunsOn.Add(parent);
  48. }
  49. await repository.UpdateAsync(system);
  50. }
  51. }