UpdateDesktopCpuUseCase.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Laptops.Cpus;
  4. public class UpdateLaptopCpuUseCase(IHardwareRepository repository) : IUseCase
  5. {
  6. public async Task ExecuteAsync(
  7. string name,
  8. int index,
  9. string? model,
  10. int? cores,
  11. int? threads)
  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.Cpus == null || index < 0 || index >= laptop.Cpus.Count)
  20. throw new NotFoundException($"CPU index {index} not found on Laptop '{name}'.");
  21. var cpu = laptop.Cpus[index];
  22. cpu.Model = model;
  23. cpu.Cores = cores;
  24. cpu.Threads = threads;
  25. await repository.UpdateAsync(laptop);
  26. }
  27. }