RemoveGpuUseCase.cs 1.1 KB

123456789101112131415161718192021222324252627282930
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Persistence;
  3. using RackPeek.Domain.Resources;
  4. using RackPeek.Domain.Resources.Servers;
  5. namespace RackPeek.Domain.UseCases.Gpus;
  6. public interface IRemoveGpuUseCase<T> : IResourceUseCase<T>
  7. where T : Resource {
  8. public Task ExecuteAsync(string name, int index);
  9. }
  10. public class RemoveGpuUseCase<T>(IResourceCollection repository) : IRemoveGpuUseCase<T> where T : Resource {
  11. public async Task ExecuteAsync(string name, int index) {
  12. name = Normalize.HardwareName(name);
  13. ThrowIfInvalid.ResourceName(name);
  14. T resource = await repository.GetByNameAsync<T>(name) ??
  15. throw new NotFoundException($"Resource '{name}' not found.");
  16. if (resource is not IGpuResource gr) throw new NotFoundException($"Resource '{name}' not found.");
  17. if (gr.Gpus == null || index < 0 || index >= gr.Gpus.Count)
  18. throw new NotFoundException($"GPU index {index} not found on '{name}'.");
  19. gr.Gpus.RemoveAt(index);
  20. await repository.UpdateAsync(resource);
  21. }
  22. }