RemoveDriveUseCase.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  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.Drives;
  6. public interface IRemoveDriveUseCase<T> : IResourceUseCase<T>
  7. where T : Resource
  8. {
  9. public Task ExecuteAsync(string name, int index);
  10. }
  11. public class RemoveDriveUseCase<T>(IResourceCollection repository) : IRemoveDriveUseCase<T> where T : Resource
  12. {
  13. public async Task ExecuteAsync(string name, int index)
  14. {
  15. name = Normalize.HardwareName(name);
  16. ThrowIfInvalid.ResourceName(name);
  17. var resource = await repository.GetByNameAsync(name) ?? throw new NotFoundException($"Resource '{name}' not found.");
  18. if (resource is not IDriveResource dr)
  19. {
  20. throw new NotFoundException($"Resource '{name}' not found.");
  21. }
  22. if (dr.Drives == null || index < 0 || index >= dr.Drives.Count)
  23. throw new NotFoundException($"Drive index {index} not found on '{name}'.");
  24. dr.Drives.RemoveAt(index);
  25. await repository.UpdateAsync(resource);
  26. }
  27. }