AddDesktopGpuUseCase.cs 967 B

123456789101112131415161718192021222324252627282930
  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 AddLaptopGpuUseCase(IResourceCollection repository) : IUseCase
  6. {
  7. public async Task ExecuteAsync(
  8. string name,
  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 InvalidOperationException($"Laptop '{name}' not found.");
  18. laptop.Gpus ??= new List<Gpu>();
  19. laptop.Gpus.Add(new Gpu
  20. {
  21. Model = model,
  22. Vram = vram
  23. });
  24. await repository.UpdateAsync(laptop);
  25. }
  26. }