using RackPeek.Domain.Helpers; using RackPeek.Domain.Persistence; using RackPeek.Domain.Resources; using RackPeek.Domain.Resources.Servers; using RackPeek.Domain.Resources.SubResources; namespace RackPeek.Domain.UseCases.Nics; public interface IAddNicUseCase : IResourceUseCase where T : Resource { public Task ExecuteAsync( string name, string? type, double? speed, int? ports); } public class AddNicUseCase(IResourceCollection repository) : IAddNicUseCase where T : Resource { public async Task ExecuteAsync( string name, string? type, double? speed, int? ports) { // ToDo pass in properties as inputs, construct the entity in the usecase, ensure optional inputs are nullable // ToDo validate / normalize all inputs name = Normalize.HardwareName(name); ThrowIfInvalid.ResourceName(name); var nicType = Normalize.NicType(type); ThrowIfInvalid.NicType(nicType); var resource = await repository.GetByNameAsync(name) ?? throw new NotFoundException($"Resource '{name}' not found."); if (resource is not INicResource nr) throw new NotFoundException($"Resource '{name}' not found."); nr.Nics ??= new List(); nr.Nics.Add(new Nic { Type = nicType, Speed = speed, Ports = ports }); await repository.UpdateAsync(resource); } }