UpdateCpuUseCase.cs 1.6 KB

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