public async Task <int> ReloadTradeHistoryAsync() { while (!StartWork()) { await Task.Delay(1000); } try { using (var context = CreateContext()) { var offset = 0; var data = await _b2C2RestClient.GetTradeHistoryAsync(offset, 100); var query = $"TRUNCATE TABLE {Constants.Schema}.{Constants.TradesTable}"; await context.Database.ExecuteSqlCommandAsync(query); while (data.Any()) { var items = data.Select(e => new TradeEntity(e)).ToList(); context.Trades.AddRange(items); await context.SaveChangesAsync(); offset += data.Count; data = await _b2C2RestClient.GetTradeHistoryAsync(offset, 100); } return(offset); } } finally { StopWork(); } }
public async Task <int> ReloadTradeHistoryAsync() { while (!StartWork()) { await Task.Delay(1000); } try { using (var context = CreateContext()) { var tradeRequest = new TradesHistoryRequest { Limit = 100 }; string tradeId = null; if (_enableAutoUpdate) { var last = context.Trades.OrderByDescending(x => x.Created).FirstOrDefault(); if (last != null) { tradeRequest.CreatedAfter = last.Created.AddHours(-1); tradeId = last.TradeId; } } var data = await _b2C2RestClient.GetTradeHistoryAsync(tradeRequest); _log.Debug($"Current cursor = null; get data after tradeId = {(tradeId ?? "null")}; load more {data.Data.Count}"); int totalCount = 0; bool finish = false; while (!finish || data.Data.Count > 0) { var items = new List <TradeEntity>(); foreach (var item in data.Data) { foreach (var assetMapping in _assetMappings) { item.AssetPair = item.AssetPair.Replace(assetMapping.Key, assetMapping.Value); } items.Add(new TradeEntity(item)); } foreach (var item in items) { if (!string.IsNullOrEmpty(tradeId) && item.TradeId == tradeId) { finish = true; break; } totalCount++; context.Trades.Add(item); } await context.SaveChangesAsync(); if (finish) { _log.Debug($"Finish loading to tradeId = {tradeId}. Loaded {totalCount} records"); break; } tradeRequest.Cursor = data.Next; data = await _b2C2RestClient.GetTradeHistoryAsync(tradeRequest); if (string.IsNullOrEmpty(data.Next)) { finish = true; } _log.Debug($"Current cursor = {tradeRequest.Cursor}; next cursor: {tradeRequest.Cursor}; load more {data.Data.Count}"); } return(totalCount); } } finally { StopWork(); } }