UpdateDesktopUseCase.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources.Models;
  4. namespace RackPeek.Domain.Resources.Hardware.Desktops;
  5. public class UpdateDesktopUseCase(IResourceCollection repository) : IUseCase
  6. {
  7. public async Task ExecuteAsync(
  8. string name,
  9. string? model = null,
  10. int? ramGb = null,
  11. int? ramMts = null,
  12. string? notes = null
  13. )
  14. {
  15. // ToDo validate / normalize all inputs
  16. name = Normalize.HardwareName(name);
  17. ThrowIfInvalid.ResourceName(name);
  18. var desktop = await repository.GetByNameAsync(name) as Desktop;
  19. if (desktop == null)
  20. throw new NotFoundException($"Desktop '{name}' not found.");
  21. if (!string.IsNullOrWhiteSpace(model))
  22. desktop.Model = model;
  23. // ---- RAM ----
  24. if (ramGb.HasValue)
  25. {
  26. ThrowIfInvalid.RamGb(ramGb);
  27. desktop.Ram ??= new Ram();
  28. desktop.Ram.Size = ramGb.Value;
  29. }
  30. if (ramMts.HasValue)
  31. {
  32. desktop.Ram ??= new Ram();
  33. desktop.Ram.Mts = ramMts.Value;
  34. }
  35. if (notes != null)
  36. {
  37. desktop.Notes = notes;
  38. }
  39. await repository.UpdateAsync(desktop);
  40. }
  41. }