AddCpuUseCase.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.Hardware.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(name);
  29. if (resource is not ICpuResource cpuResource) return;
  30. cpuResource.Cpus ??= [];
  31. cpuResource.Cpus.Add(new Cpu
  32. {
  33. Model = model,
  34. Cores = cores,
  35. Threads = threads
  36. });
  37. await repo.UpdateAsync(resource);
  38. }
  39. }