UpdateServerUseCase.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.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. string? notes = null
  12. )
  13. {
  14. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  15. // ToDo validate / normalize all inputs
  16. name = Normalize.HardwareName(name);
  17. ThrowIfInvalid.ResourceName(name);
  18. var server = await repository.GetByNameAsync(name) as Server;
  19. if (server == null)
  20. throw new NotFoundException($"Server '{name}' not found.");
  21. // ---- RAM ----
  22. if (ramGb.HasValue)
  23. {
  24. ThrowIfInvalid.RamGb(ramGb);
  25. server.Ram ??= new Ram();
  26. server.Ram.Size = ramGb.Value;
  27. }
  28. if (ramMts.HasValue)
  29. {
  30. server.Ram ??= new Ram();
  31. server.Ram.Mts = ramMts.Value;
  32. }
  33. // ---- IPMI ----
  34. if (ipmi.HasValue) server.Ipmi = ipmi.Value;
  35. if (notes != null)
  36. {
  37. server.Notes = notes;
  38. }
  39. await repository.UpdateAsync(server);
  40. }
  41. }