RemoveDesktopDriveUseCase.cs 793 B

12345678910111213141516171819202122
  1. using RackPeek.Domain.Helpers;
  2. using RackPeek.Domain.Resources.Models;
  3. namespace RackPeek.Domain.Resources.Hardware.Laptops.Drives;
  4. public class RemoveLaptopDriveUseCase(IHardwareRepository repository) : IUseCase
  5. {
  6. public async Task ExecuteAsync(string name, int index)
  7. {
  8. name = Normalize.HardwareName(name);
  9. ThrowIfInvalid.ResourceName(name);
  10. var laptop = await repository.GetByNameAsync(name) as Laptop
  11. ?? throw new NotFoundException($"Laptop '{name}' not found.");
  12. if (laptop.Drives == null || index < 0 || index >= laptop.Drives.Count)
  13. throw new NotFoundException($"Drive index {index} not found on Laptop '{name}'.");
  14. laptop.Drives.RemoveAt(index);
  15. await repository.UpdateAsync(laptop);
  16. }
  17. }