public VirtualNumberAssociation Generate(IVirtualNumberRequest virtualNumberRequest) { var availableNumbers = _virtualNumberRepository.Get() .Where(number => number.Purpose.Id.Equals(virtualNumberRequest.PurposeId)) .Include(number => number.VirtualPhoneNumber); var virtualNumbersUsedBySeeker = _virtualNumberAssociationRepository.Get() .Where( association => association.Caller.Id == virtualNumberRequest.Caller.Id && association.VirtualNumber.Purpose.Id == virtualNumberRequest.PurposeId); var numbersUsedBySeeker = virtualNumbersUsedBySeeker.Select( number => number.VirtualNumber).Include(number => number.VirtualPhoneNumber).ToList(); var availableNumber = availableNumbers.ToList() .Except((numbersUsedBySeeker), new VirtualNumberComparer()).FirstOrDefault(); if (availableNumber == null) { throw new ApplicationException("No more numbers available!"); } var newAssociation = new VirtualNumberAssociation { Caller = virtualNumberRequest.Caller, Callee = virtualNumberRequest.Callee, State = new InUse(), VirtualNumber = availableNumber, BabajobJobId = virtualNumberRequest.BabajobJobId }; this.Save(newAssociation); return(newAssociation); }
public void Save(VirtualNumberAssociation virtualNumberAssociation) { var caller = _userRepository.Get() .FirstOrDefault( user => user.Id == virtualNumberAssociation.Caller.Id); if (caller == null) { _userRepository.Save(virtualNumberAssociation.Caller); } var callee = _userRepository.Get() .FirstOrDefault(user => user.Id == virtualNumberAssociation.Callee.Id); if (callee == null) { _userRepository.Save(virtualNumberAssociation.Callee); } var newAssociation = new VirtualNumberAssociation { CallerId = virtualNumberAssociation.Caller.Id, CalleeId = virtualNumberAssociation.Callee.Id, StateId = virtualNumberAssociation.State.Id, VirtualNumberId = virtualNumberAssociation.VirtualNumber.Id, BabajobJobId = virtualNumberAssociation.BabajobJobId }; _virtualNumberAssociationRepository.Save(newAssociation); }