UpdateDesktopCpuUseCase.cs 1.1 KB

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