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.Laptops.Gpus;
  5. public class UpdateLaptopGpuUseCase(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 laptop = await repository.GetByNameAsync(name) as Laptop
  18. ?? throw new NotFoundException($"Laptop '{name}' not found.");
  19. if (laptop.Gpus == null || index < 0 || index >= laptop.Gpus.Count)
  20. throw new NotFoundException($"GPU index {index} not found on Laptop '{name}'.");
  21. var gpu = laptop.Gpus[index];
  22. gpu.Model = model;
  23. gpu.Vram = vram;
  24. await repository.UpdateAsync(laptop);
  25. }
  26. }