AddCpuUseCase.cs 1.3 KB

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