UpdateServerUseCase.cs 1.4 KB

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