RemoveCpuUseCase.cs 1018 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.Models;
  5. namespace RackPeek.Domain.UseCases.Cpus;
  6. public interface IRemoveCpuUseCase<T> : IResourceUseCase<T>
  7. where T : Resource
  8. {
  9. public Task ExecuteAsync(
  10. string name,
  11. int index);
  12. }
  13. public class RemoveCpuUseCase<T>(IResourceCollection repo) : IRemoveCpuUseCase<T> where T : Resource
  14. {
  15. public async Task ExecuteAsync(
  16. string name,
  17. int index)
  18. {
  19. name = Normalize.HardwareName(name);
  20. ThrowIfInvalid.ResourceName(name);
  21. var resource = await repo.GetByNameAsync(name);
  22. if (resource is not ICpuResource cpuResource) return;
  23. cpuResource.Cpus ??= [];
  24. if (index < 0 || index >= cpuResource.Cpus.Count)
  25. throw new ArgumentOutOfRangeException(nameof(index), "CPU index out of range.");
  26. cpuResource.Cpus.RemoveAt(index);
  27. await repo.UpdateAsync(resource);
  28. }
  29. }