4
0

UpdateDesktopCpuUseCase.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources.Models;
  4. namespace RackPeek.Domain.Resources.Hardware.Desktops.Cpus;
  5. public class UpdateDesktopCpuUseCase(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 desktop = await repository.GetByNameAsync(name) as Desktop
  19. ?? throw new NotFoundException($"Desktop '{name}' not found.");
  20. if (desktop.Cpus == null || index < 0 || index >= desktop.Cpus.Count)
  21. throw new NotFoundException($"CPU index {index} not found on desktop '{name}'.");
  22. var cpu = desktop.Cpus[index];
  23. cpu.Model = model;
  24. cpu.Cores = cores;
  25. cpu.Threads = threads;
  26. await repository.UpdateAsync(desktop);
  27. }
  28. }