UpdateDesktopGpuUseCase.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources.Models;
  4. namespace RackPeek.Domain.Resources.Hardware.Desktops.Gpus;
  5. public class UpdateDesktopGpuUseCase(IResourceCollection repository) : IUseCase
  6. {
  7. public async Task ExecuteAsync(
  8. string name,
  9. int index,
  10. string? model,
  11. int? vram)
  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 desktop = await repository.GetByNameAsync(name) as Desktop
  18. ?? throw new NotFoundException($"Desktop '{name}' not found.");
  19. if (desktop.Gpus == null || index < 0 || index >= desktop.Gpus.Count)
  20. throw new NotFoundException($"GPU index {index} not found on desktop '{name}'.");
  21. var gpu = desktop.Gpus[index];
  22. gpu.Model = model;
  23. gpu.Vram = vram;
  24. await repository.UpdateAsync(desktop);
  25. }
  26. }