using RackPeek.Domain.Helpers; using RackPeek.Domain.Persistence; using RackPeek.Domain.Resources; using RackPeek.Domain.Resources.Servers; namespace RackPeek.Domain.UseCases.Cpus; public interface IRemoveCpuUseCase : IResourceUseCase where T : Resource { public Task ExecuteAsync( string name, int index); } public class RemoveCpuUseCase(IResourceCollection repo) : IRemoveCpuUseCase where T : Resource { public async Task ExecuteAsync( string name, int index) { name = Normalize.HardwareName(name); ThrowIfInvalid.ResourceName(name); T resource = await repo.GetByNameAsync(name) ?? throw new NotFoundException($"Resource '{name}' not found."); if (resource is not ICpuResource cpuResource) return; cpuResource.Cpus ??= []; if (index < 0) throw new NotFoundException($"Please pick a CPU index >= 0 for '{name}'."); if (cpuResource.Cpus.Count == 0) throw new NotFoundException($"'{name}' has no CPUs."); if (index >= cpuResource.Cpus.Count) throw new NotFoundException($"Please pick a CPU index < {cpuResource.Cpus.Count} for '{name}'."); cpuResource.Cpus.RemoveAt(index); await repo.UpdateAsync(resource); } }