Exemple #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();
        }
Exemple #2
0
        public async Task InitDb()
        {
            //await _dbContext.Database.EnsureDeletedAsync();
            await _dbContext.Database.EnsureCreatedAsync();

            var      isInitialized = _dbContext.AppOptions.FirstOrDefault();
            VkRegion dbRegion      = null;

            if (isInitialized == null)
            {
                var tmbRegion = _vkService.GetRegions("Тамбов").FirstOrDefault();
                if (tmbRegion != null)
                {
                    dbRegion = _dbContext.Set <VkRegion>().Add(new VkRegion()
                    {
                        Title    = tmbRegion.Title,
                        SourceId = tmbRegion.Id
                    }).Entity;
                    isInitialized = new ApplicationOptions()
                    {
                        IsCitiesSynchronized = true
                    };
                    _dbContext.AppOptions.Add(isInitialized);
                    await _dbContext.SaveChangesAsync();
                }

                var regionParts = TambovCities.ResourceManager.GetString("tambov").Split(',').ToArray();
                if (tmbRegion != null)
                {
                    foreach (var part in regionParts)
                    {
                        var parts = _vkService.GetRegionPartsByString(part)
                                    .Where(c => c.Region == tmbRegion.Title && c.Id.HasValue)
                                    .DistinctBy(c => c.Title)
                                    .Select(c => new RegionPart
                        {
                            Title    = c.Title,
                            SourceId = c.Id.Value,
                            RegionId = dbRegion.Id,
                            VkRegion = dbRegion,
                        }).ToArray();
                        _dbContext.AddRange(parts);
                        await _dbContext.SaveChangesAsync();
                    }
                }
            }
        }