public async Task <List <ItemPrice> > FetchAllItemPricesFromApi(CancellationToken cancellationToken, int[] filter) { var ids = await _gw2ApiClient.FetchAllItemIdsAsync(cancellationToken); var chunkedItemIds = ids.Where(x => !filter.Contains(x)).Batch(150); var tasks = chunkedItemIds .Select(chunk => _gw2ApiClient.FetchItemPricesForIdsAsync(cancellationToken, chunk)) .ToList(); return((await Task.WhenAll(tasks)).SelectMany(x => x).ToList()); }
public async Task UpdatePricesAsync() { var items = _context.Items.ToList(); var itemPriceDictionary = (await Task.WhenAll(items.Batch(150).Select(batch => _gw2ApiClient.FetchItemPricesForIdsAsync(CancellationToken.None, batch.Select(x => x.ItemId))))) .SelectMany(x => x).ToDictionary(x => x.ItemPriceId, x => x); items.ForEach(x => { x.Buy = itemPriceDictionary[x.ItemId].BuyData.BuyPrice; x.Sell = itemPriceDictionary[x.ItemId].SellData.SellPrice; }); //TODO: Unfuck this. needs more thinking. Build hierarchy? Iterate until all updated? await _context.SaveChangesAsync(); }