public async Task <AssetPairContract> Update(string assetPairId, [FromBody] AssetPairUpdateRequest assetPairUpdateRequest) { await ValidatePairUpdate(assetPairUpdateRequest); ValidateId(assetPairId, assetPairUpdateRequest.Id); if (assetPairUpdateRequest.IsFrozen == false) { assetPairUpdateRequest.FreezeInfo = new FreezeInfoContract(); } var updated = await _assetPairsRepository.UpdateAsync(assetPairUpdateRequest); if (updated == null) { throw new ArgumentException("Update failed", nameof(assetPairUpdateRequest)); } var updatedContract = _convertService.Convert <IAssetPair, AssetPairContract>(updated); await _eventSender.SendSettingsChangedEvent($"{Request.Path}", SettingsChangedSourceType.AssetPair, updated.Id); await _cqrsMessageSender.SendAssetPairChangedEvent(new AssetPairChangedEvent { OperationId = Guid.NewGuid().ToString("N"), AssetPair = updatedContract, }); return(updatedContract); }
public async Task <IAssetPair> UpdateAsync(AssetPairUpdateRequest assetPairUpdateRequest) { var current = await TableStorage.GetDataAsync(AssetPairEntity.Pk, assetPairUpdateRequest.Id); if (current == null) { throw new ArgumentException("Asset pair does not exist", nameof(assetPairUpdateRequest)); } return(await TableStorage.ReplaceAsync(AssetPairEntity.Pk, assetPairUpdateRequest.Id, prev => UpdateHelper.GetAzureReplaceObject(prev, assetPairUpdateRequest))); }
public async Task <IAssetPair> UpdateAsync(AssetPairUpdateRequest assetPairUpdateRequest) { using (var conn = new SqlConnection(_connectionString)) { var updateObj = UpdateHelper.GetSqlUpdateObject(assetPairUpdateRequest); await conn.ExecuteAsync( $"update {TableName} set {GetUpdateClause(updateObj.ParameterNames)} where Id=@Id", updateObj); return(await conn.QuerySingleOrDefaultAsync <AssetPairEntity>( $"SELECT * FROM {TableName} WHERE Id=@id", new { assetPairUpdateRequest.Id })); } }
private async Task ValidatePairUpdate(AssetPairUpdateRequest assetPair) { if (assetPair == null) { throw new ArgumentNullException(nameof(assetPair), "Model is incorrect"); } if (string.IsNullOrWhiteSpace(assetPair.Id)) { throw new ArgumentNullException(nameof(assetPair.Id), "AssetPair Id must be set"); } if (assetPair.MatchingEngineMode != null && !Enum.IsDefined(typeof(MatchingEngineModeContract), assetPair.MatchingEngineMode)) { throw new ArgumentNullException(nameof(assetPair.MatchingEngineMode), "AssetPair MatchingEngineMode must be set"); } if (assetPair.BaseAssetId != null && await _assetsRepository.GetAsync(assetPair.BaseAssetId) == null) { throw new InvalidOperationException($"Base Asset {assetPair.BaseAssetId} does not exist"); } if (assetPair.QuoteAssetId != null && await _assetsRepository.GetAsync(assetPair.QuoteAssetId) == null) { throw new InvalidOperationException($"Quote Asset {assetPair.QuoteAssetId} does not exist"); } if (!string.IsNullOrEmpty(assetPair.MarketId) && await _marketRepository.GetAsync(assetPair.MarketId) == null) { throw new InvalidOperationException($"Market {assetPair.MarketId} does not exist"); } if (assetPair.StpMultiplierMarkupAsk != null && assetPair.StpMultiplierMarkupAsk <= 0) { throw new InvalidOperationException($"StpMultiplierMarkupAsk must be greater then zero"); } if (assetPair.StpMultiplierMarkupBid != null && assetPair.StpMultiplierMarkupBid <= 0) { throw new InvalidOperationException($"StpMultiplierMarkupBid must be greater then zero"); } var current = await _assetPairsRepository.GetAsync(assetPair.Id); if (current == null) { throw new InvalidOperationException($"Asset pair with id {assetPair.Id} does not exist"); } if ((current.BaseAssetId != assetPair.BaseAssetId || current.QuoteAssetId != assetPair.QuoteAssetId || current.LegalEntity != assetPair.LegalEntity) && await _assetPairsRepository.GetByBaseQuoteAndLegalEntityAsync(assetPair.BaseAssetId, assetPair.QuoteAssetId, assetPair.LegalEntity) != null) { throw new InvalidOperationException( $"Asset pair with base: {assetPair.BaseAssetId}, quote: {assetPair.QuoteAssetId}, legalEntity: {assetPair.LegalEntity} already exists"); } if (assetPair.IsFrozen == false && assetPair.FreezeInfo != null) { throw new InvalidOperationException($"FreezeInfo can be specified only for frozen asset pair"); } //base pair check <-- the last one if (assetPair.BasePairId == null) { return; } if (await _assetPairsRepository.GetAsync(assetPair.BasePairId) == null) { throw new InvalidOperationException($"BasePair with Id {assetPair.BasePairId} does not exist"); } if (await _assetPairsRepository.GetByBaseAssetPairAsync(assetPair.BasePairId) != null) { throw new InvalidOperationException($"BasePairId {assetPair.BasePairId} does not exist"); } if (await _assetPairsRepository.GetByBaseAssetPairAndNotByIdAsync(assetPair.Id, assetPair.BasePairId) != null) { throw new InvalidOperationException($"BasePairId {assetPair.BasePairId} cannot be added twice"); } }