public async Task <List <TradingInstrumentContract> > AssignCollection(string tradingConditionId, [FromBody] string[] instruments) { var currentInstruments = await _tradingInstrumentsRepository.GetByTradingConditionAsync(tradingConditionId); if (currentInstruments.Any()) { var toRemove = currentInstruments.Where(x => !instruments.Contains(x.Instrument)).ToArray(); var existingOrderGroups = await _tradingService.CheckActiveByTradingCondition(tradingConditionId); if (existingOrderGroups.Any()) { var errorMessage = existingOrderGroups.Aggregate( "Unable to remove following instruments as they have active orders: ", (current, @group) => current + $"{@group} orders) "); throw new InvalidOperationException(errorMessage); } foreach (var pair in toRemove) { await _tradingInstrumentsRepository.DeleteAsync(pair.Instrument, pair.TradingConditionId); } } var pairsToAdd = instruments.Where(x => currentInstruments.All(y => y.Instrument != x)); var addedPairs = await _tradingInstrumentsRepository.CreateDefaultTradingInstruments(tradingConditionId, pairsToAdd, _defaultTradingInstrumentSettings); await _eventSender.SendSettingsChangedEvent($"{Request.Path}", SettingsChangedSourceType.TradingInstrument); return(addedPairs.Select(x => _convertService.Convert <ITradingInstrument, TradingInstrumentContract>(x)) .ToList()); }