UpdateCpuUseCase.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.Servers;
  5. using RackPeek.Domain.Resources.SubResources;
  6. namespace RackPeek.Domain.UseCases.Cpus;
  7. public interface IUpdateCpuUseCase<T> : IResourceUseCase<T>
  8. where T : Resource
  9. {
  10. public Task ExecuteAsync(
  11. string name,
  12. int index,
  13. string? model,
  14. int? cores,
  15. int? threads);
  16. }
  17. public class UpdateCpuUseCase<T>(IResourceCollection repo) : IUpdateCpuUseCase<T> where T : Resource
  18. {
  19. public async Task ExecuteAsync(
  20. string name,
  21. int index,
  22. string? model,
  23. int? cores,
  24. int? threads)
  25. {
  26. // ToDo pass in properties as inputs, construct the entity in the usecase
  27. // ToDo validate / normalize all inputs
  28. name = Normalize.HardwareName(name);
  29. ThrowIfInvalid.ResourceName(name);
  30. T resource = await repo.GetByNameAsync<T>(name) ??
  31. throw new NotFoundException($"Resource '{name}' not found.");
  32. if (resource is not ICpuResource cpuResource) return;
  33. cpuResource.Cpus ??= [];
  34. if (index < 0)
  35. throw new NotFoundException($"Please pick a CPU index >= 0 for '{name}'.");
  36. if (cpuResource.Cpus.Count == 0)
  37. throw new NotFoundException($"'{name}' has no CPUs.");
  38. if (index >= cpuResource.Cpus.Count)
  39. throw new NotFoundException($"Please pick a CPU index < {cpuResource.Cpus.Count} for '{name}'.");
  40. Cpu cpu = cpuResource.Cpus[index];
  41. cpu.Model = model;
  42. cpu.Cores = cores;
  43. cpu.Threads = threads;
  44. await repo.UpdateAsync(resource);
  45. }
  46. }