UpdateServerUseCase.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Hardware.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Servers;
  4. public class UpdateServerUseCase(IHardwareRepository repository) : IUseCase
  5. {
  6. public async Task ExecuteAsync(
  7. string name,
  8. int? ramGb = null,
  9. int? ramMts = null,
  10. bool? ipmi = null
  11. )
  12. {
  13. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  14. // ToDo validate / normalize all inputs
  15. name = Normalize.HardwareName(name);
  16. ThrowIfInvalid.ResourceName(name);
  17. var server = await repository.GetByNameAsync(name) as Server;
  18. if (server == null)
  19. throw new NotFoundException($"Server '{name}' not found.");
  20. // ---- RAM ----
  21. if (ramGb.HasValue)
  22. {
  23. ThrowIfInvalid.RamGb(ramGb);
  24. server.Ram ??= new Ram();
  25. server.Ram.Size = ramGb.Value;
  26. }
  27. if (ramMts.HasValue)
  28. {
  29. server.Ram ??= new Ram();
  30. server.Ram.Mts = ramMts.Value;
  31. }
  32. // ---- IPMI ----
  33. if (ipmi.HasValue) server.Ipmi = ipmi.Value;
  34. await repository.UpdateAsync(server);
  35. }
  36. }