示例#1
0
        public async Task ProcessSearch()
        {
            var ads = new List <AdModel>();

            try
            {
                ads = await _searchClient.GetAds();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.Message);
                Console.ResetColor();
            }

            if (ads?.Count == 0)
            {
                return;
            }

            var newAds = ads.Where(ad =>
                                   AdsCache.FirstOrDefault(item =>
                                                           item.ProviderAdId == ad.ProviderAdId && item.AdSource == ad.AdSource) == null)
                         .ToList();

            bool append = AdsCache.Count == 0;

            if (AdsCache.Count > 0)
            {
                newAds.Reverse();
            }


            foreach (var item in newAds)
            {
                if (AdsCache.Count >= LIST_SIZE)
                {
                    AdsCache.RemoveAt(AdsCache.Count - 1);
                }

                if (append)
                {
                    AdsCache.Add(item);
                }
                else
                {
                    AdsCache.Insert(0, item);
                }


                Console.WriteLine($"=======NEW CAR: {item.AdTitle} {item.PriceInfo} {item.CarInfo} =======");
            }

            if (newAds.Count > 0)
            {
                using (var dbContext = new AppDbContext())
                {
                    foreach (var item in newAds)
                    {
                        dbContext.Ads.Add(new Ad
                        {
                            OwnerId       = _user.Id,
                            ProviderAdId  = item.ProviderAdId,
                            AdTitle       = item.AdTitle,
                            CarInfo       = item.CarInfo,
                            ImageLink     = item.ImageLink,
                            PriceInfo     = item.PriceInfo,
                            AdSource      = item.AdSource,
                            AddressInfo   = item.AddressInfo,
                            Email         = item.Email,
                            CreatedAtInfo = item.CreatedAtInfo,
                            Phone         = item.Phone,
                            AdLink        = item.AdLink,
                        });
                    }

                    await dbContext.SaveChangesAsync();
                }

                await MessagingService.SendPushNotificationWithData($"({newAds.Count()}) нових авто. {Searchitem.Title}", Searchitem.Description, new Random().Next(1, 9999999), _user.MobileAppToken);
            }
        }