UpdateDesktopGpuUseCase.cs 1.0 KB

12345678910111213141516171819202122232425262728293031
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Laptops.Gpus;
  4. public class UpdateLaptopGpuUseCase(IHardwareRepository repository) : IUseCase
  5. {
  6. public async Task ExecuteAsync(
  7. string name,
  8. int index,
  9. string? model,
  10. int? vram)
  11. {
  12. // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable
  13. // ToDo validate / normalize all inputs
  14. name = Normalize.HardwareName(name);
  15. ThrowIfInvalid.ResourceName(name);
  16. var laptop = await repository.GetByNameAsync(name) as Laptop
  17. ?? throw new NotFoundException($"Laptop '{name}' not found.");
  18. if (laptop.Gpus == null || index < 0 || index >= laptop.Gpus.Count)
  19. throw new NotFoundException($"GPU index {index} not found on Laptop '{name}'.");
  20. var gpu = laptop.Gpus[index];
  21. gpu.Model = model;
  22. gpu.Vram = vram;
  23. await repository.UpdateAsync(laptop);
  24. }
  25. }