public async Task UpdateInfoOfItemAsync(Item itemUpdated) { using var _trackingContext = new TrackingContext(_options); var item = await _trackingContext.Items.Where(i => i.ItemId == itemUpdated.ItemId).FirstOrDefaultAsync() ?? throw new Exception($"{Exceptions.ITEM_NOT_FOUND_EXCEPTION}"); _updateInfoHelper.GetUpdatedItem(ref item, itemUpdated); await _trackingContext.SaveChangesAsync(); }
private async void OnTimedEvent(Object source, ElapsedEventArgs e) { if (!itemsQueue.Any()) { return; } var item = itemsQueue.Dequeue(); try { var newInfo = item.Source switch { PullAndBear.SHOP_NAME => await _pullAndBearClient.GetItemInfoAsync(item.Url), Bershka.SHOP_NAME => await _bershkaClient.GetItemInfoAsync(item.Url), _ => throw new Exception(Exceptions.UNKNOWN_SHOP_EXCEPTION), }; if (item.Status != newInfo.Status || item.Price != newInfo.Price) { await _botService.SendMessageMarkdownV2Async( item.ChatId, _textConverter.ToString(item, newInfo)); } _updateInfoHelper.GetUpdatedItem(ref item, newInfo); await _trackingRepository.UpdateInfoOfItemAsync(item); } catch (Exception ex) { // it's ok; lets update item information next time; } if ((DateTime.Now - item.LastUpdateDate).TotalHours > maxTimeWithoutUpdateIntervalHours) { await _trackingRepository.RemoveItemAsync(item.Url); await _botService.SendMessageAsync( item.ChatId, WANNA_REMOVE_ITEM_EXCEPTION); } else { itemsQueue.Enqueue(item); if ((DateTime.Now - item.LastUpdateDate).TotalHours > maxUpdateTimeIntervalHours) { await _botService.SendMessageAsync( item.ChatId, CANT_UPDATE_EXCEPTION); } } }