AddCpuUseCase.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 IAddCpuUseCase<T> : IResourceUseCase<T>
  8. where T : Resource
  9. {
  10. public Task ExecuteAsync(
  11. string name,
  12. string? model,
  13. int? cores,
  14. int? threads);
  15. }
  16. public class AddCpuUseCase<T>(IResourceCollection repo) : IAddCpuUseCase<T> where T : Resource
  17. {
  18. public async Task ExecuteAsync(
  19. string name,
  20. string? model,
  21. int? cores,
  22. int? threads)
  23. {
  24. // ToDo pass in properties as inputs, construct the entity in the usecase
  25. // ToDo validate / normalize all inputs
  26. name = Normalize.HardwareName(name);
  27. ThrowIfInvalid.ResourceName(name);
  28. var resource = await repo.GetByNameAsync<T>(name) ??
  29. throw new NotFoundException($"Resource '{name}' not found.");
  30. if (resource is not ICpuResource cpuResource) return;
  31. cpuResource.Cpus ??= [];
  32. cpuResource.Cpus.Add(new Cpu
  33. {
  34. Model = model,
  35. Cores = cores,
  36. Threads = threads
  37. });
  38. await repo.UpdateAsync(resource);
  39. }
  40. }