Ejemplo n.º 1
0
        private async Task LikeFromGlobalSearch(RegionPart regionPart)
        {
            var appOptions = _dbContext.AppOptions.FirstOrDefault();

            if (appOptions == null)
            {
                throw new DataException("app options are null");
            }
            var offset             = appOptions.Offset;
            var globalSearchResult = await _vkService.GetUsersFromGlobalSearch((int?)regionPart.SourceId, offset);

            foreach (var currentUser in globalSearchResult)
            {
                try
                {
                    var dbUser = await _dbContext.Users.SingleOrDefaultAsync(u => u.SourceId == currentUser.Id);

                    var userInfo = await _vkService.GetUser(currentUser.Id);

                    if (dbUser == null)
                    {
                        await _vkService.SetLike(GetItemId(userInfo.PhotoId), currentUser.Id);

                        var likedUser = new User
                        {
                            FirstName         = currentUser.FirstName,
                            LastName          = currentUser.LastName,
                            LikeDate          = DateTime.Now,
                            RegionPart        = regionPart,
                            IsLiked           = true,
                            IsFriendsUploaded = false,
                            SourceId          = currentUser.Id
                        };
                        await _dbContext.Users.AddAsync(likedUser);

                        await _dbContext.SaveChangesAsync();
                    }
                }
                catch (Exception e)
                {
                    _logger.Error(e, $"Exception : {e} \n Inner : {e.InnerException}");
                }
            }

            appOptions.Offset += (uint)globalSearchResult.Count();
            _dbContext.AppOptions.Update(appOptions);
            await _dbContext.SaveChangesAsync();
        }