UpdateCpuUseCase.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. 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. public async Task ExecuteAsync(
  18. string name,
  19. int index,
  20. string? model,
  21. int? cores,
  22. int? threads) {
  23. // ToDo pass in properties as inputs, construct the entity in the usecase
  24. // ToDo validate / normalize all inputs
  25. name = Normalize.HardwareName(name);
  26. ThrowIfInvalid.ResourceName(name);
  27. T resource = await repo.GetByNameAsync<T>(name) ??
  28. throw new NotFoundException($"Resource '{name}' not found.");
  29. if (resource is not ICpuResource cpuResource) return;
  30. cpuResource.Cpus ??= [];
  31. if (index < 0)
  32. throw new NotFoundException($"Please pick a CPU index >= 0 for '{name}'.");
  33. if (cpuResource.Cpus.Count == 0)
  34. throw new NotFoundException($"'{name}' has no CPUs.");
  35. if (index >= cpuResource.Cpus.Count)
  36. throw new NotFoundException($"Please pick a CPU index < {cpuResource.Cpus.Count} for '{name}'.");
  37. Cpu cpu = cpuResource.Cpus[index];
  38. cpu.Model = model;
  39. cpu.Cores = cores;
  40. cpu.Threads = threads;
  41. await repo.UpdateAsync(resource);
  42. }
  43. }