RemoveNicUseCase.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132
  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.Nics;
  6. public interface IRemoveNicUseCase<T> : IResourceUseCase<T>
  7. where T : Resource
  8. {
  9. public Task ExecuteAsync(string name, int index);
  10. }
  11. public class RemoveNicUseCase<T>(IResourceCollection repository) : IRemoveNicUseCase<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<T>(name) ??
  18. throw new NotFoundException($"Resource '{name}' not found.");
  19. if (resource is not INicResource nr) throw new NotFoundException($"Resource '{name}' not found.");
  20. if (nr.Nics == null || index < 0 || index >= nr.Nics.Count)
  21. throw new NotFoundException($"NIC index {index} not found on desktop '{name}'.");
  22. nr.Nics.RemoveAt(index);
  23. await repository.UpdateAsync(resource);
  24. }
  25. }