private async Task DownloadItems(AuctionHouseResponse ahResponse) { var counter = 1; var itemIds = ahResponse.Auctions .Select(x => x.AuctionedItem.Id) .Distinct() .OrderBy(x => x) .ToList(); var localItems = await _itemRepository.GetAllItemIdsAsync(); var missingItems = itemIds.Except(localItems); foreach (var itemId in missingItems) { await Task.Delay(1); // so we don't throttle Blizzard API var url = BlizzardApi.Item(_baseUrl, itemId); try { var item = JsonConvert.DeserializeObject <Item>(await _httpClient.GetStringAsync(url)); await _itemRepository.InsertOneAsync(item); Console.WriteLine($"[{counter++}] ID: {item.ItemId}, Name: {item.Name}"); } catch (Exception e) { _logger.LogWarning($"Exception occurred while parsing items from Blizzard API: {e.Message}"); } } }
private async Task UpdateItemPrices(AuctionHouseResponse ahResponse) { var timer = new Stopwatch(); timer.Start(); var itemAuctions = ahResponse.Auctions .Select(x => new { x.AuctionedItem.Id, x.Bid, x.Buyout, x.UnitPrice }) .Distinct() .OrderBy(x => x.Id) .GroupBy(x => x.Id, x => new { x.Bid, x.Buyout, x.UnitPrice }) .Select(x => new { x.Key, Values = x.ToList() }); var localItems = (await _itemRepository.GetAllItemIdsAsync()).ToList(); var allPrices = (await _pricesRepository.GetAllPricesAsync()).ToList(); foreach (var auction in itemAuctions) { var itemId = auction.Key; if (!localItems.Contains(itemId)) { _logger.LogInformation($"Item not existing in local DB, id: {itemId}"); continue; } var minBid = auction.Values.Select(x => x.Bid).Where(x => x != 0).OrderBy(x => x).FirstOrDefault(); var minBuyout = auction.Values.Select(x => x.Buyout).Where(x => x != 0).OrderBy(x => x) .FirstOrDefault(); var minUnitPrice = auction.Values.Select(x => x.UnitPrice).Where(x => x != 0).OrderBy(x => x) .FirstOrDefault(); var dateUpdated = DateTime.UtcNow; var newUnitPrice = new Price { DateTime = dateUpdated, Value = minUnitPrice }; var newBuyout = new Price { DateTime = dateUpdated, Value = minBuyout }; var newBid = new Price { DateTime = dateUpdated, Value = minBid }; var localPrices = allPrices.SingleOrDefault(x => x.ItemId == itemId); if (localPrices is null) { _logger.LogInformation( $"New item with ID: {itemId} has prices: UnitPrice: {newUnitPrice.Value}," + $" Buyout: {newBuyout.Value}, Bid: {newBid.Value}"); await _pricesRepository.InsertOneAsync(new Prices { ItemId = itemId, UnitPrice = new List <Price> { newUnitPrice }, Bids = new List <Price> { newBid }, Buyouts = new List <Price> { newBuyout } }); continue; } _logger.LogInformation($"Item with ID: {itemId} has new prices: UnitPrice: {newUnitPrice.Value}," + $" Buyout: {newBuyout.Value}, Bid: {newBid.Value}"); await _pricesRepository.UpdateOneAsync(itemId, newUnitPrice, newBid, newBuyout); } timer.Stop(); _logger.LogInformation($"Finished updating items in: {timer.Elapsed.TotalSeconds} seconds"); }
private async Task UpdatePrices(AuctionHouseResponse ahResponse) { var timer = Stopwatch.StartNew(); var itemAuctions = ahResponse.Auctions .Select(x => new { x.AuctionedItem.Id, x.Bid, x.Buyout, x.UnitPrice }) .Distinct() .GroupBy(x => x.Id, x => new { x.Bid, x.Buyout, x.UnitPrice }) .Select(x => new { x.Key, Values = x.ToList() }); var allPrices = (await _pricesRepository.GetAllPricesAsync()).ToList(); foreach (var auction in itemAuctions) { var localPrices = allPrices.SingleOrDefault(x => x.ItemId == auction.Key); var dateUpdated = DateTime.UtcNow; var minBid = auction.Values .Select(x => x.Bid) .FirstOrDefault(x => x != 0); var minBuyout = auction.Values .Select(x => x.Buyout) .FirstOrDefault(x => x != 0); var minUnitPrice = auction.Values .Select(x => x.UnitPrice) .FirstOrDefault(x => x != 0); var newUnitPrice = new Price { DateTime = dateUpdated, Value = minUnitPrice }; var newBuyout = new Price { DateTime = dateUpdated, Value = minBuyout }; var newBid = new Price { DateTime = dateUpdated, Value = minBid }; if (localPrices is null) { allPrices.Add(new Prices { ItemId = auction.Key, UnitPrice = new List <Price> { newUnitPrice }, Bids = new List <Price> { newBid }, Buyouts = new List <Price> { newBuyout } }); continue; } localPrices.UnitPrice.Add(newUnitPrice); localPrices.Buyouts.Add(newBuyout); localPrices.Bids.Add(newBid); } await _pricesRepository.RemoveAllAsync(); await _pricesRepository.InsertManyAsync(allPrices); timer.Stop(); _logger.LogInformation($"Done updating item prices in: {timer.Elapsed.Seconds} seconds."); }