示例#1
0
        private static async Task CheckItemPrice(Item item)
        {
            if (item.SubscribersEmails.Length == 0)
            {
                await DeleteUnusedItem(item);

                return;
            }

            decimal currentPrice = 0;

            try
            {
                currentPrice = await PriceParser.Parse(item, cache);
            }
            catch (Exception ex)
            {
                log.LogError($"Can't parse url {item.Url}: {ex.Message}.");

                Errors += $"Can't parse url {item.Url}: {ex.Message}.<br>";

                return;
            }

            log.LogInformation($"Item {item.Name} price: {currentPrice} rub.");

            Price prevPrice;

            try
            {
                prevPrice = await priceDb.GetLastItemPriceAsync(item);
            }
            catch
            {
                try
                {
                    await priceDb.CreateItemPriceAsync(item, currentPrice);
                }catch (Exception ex)
                {
                    log.LogError($"Can't write item price to db: {ex.Message}.");
                }

                return;
            }

            if (currentPrice < prevPrice.ItemPrice)
            {
                await NotifyOfPriceReduction(item, currentPrice, prevPrice);
            }

            if (currentPrice == prevPrice.ItemPrice)
            {
                return;
            }

            Price newPrice;

            try
            {
                newPrice = await priceDb.CreateItemPriceAsync(item, currentPrice);
            }
            catch (Exception ex)
            {
                log.LogError($"Can't save item {item.Name} price {currentPrice} to db: {ex.Message}.");
            }
        }