UpdateDesktopCpuUseCase.cs 1.1 KB

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