public async Task <IActionResult> Index(string?code, string?error, [FromQuery(Name = "error_description")] string?errorDescription) { if (error is not null || errorDescription is not null) { _logger.LogWarning($"Got error '{error}' with description '{errorDescription}'"); var model = new VkErrorModel { ExceptionType = error !, Message = errorDescription ! }; return(View(model)); } if (code is null) { _logger.LogError("Authentication code is null"); var model = new VkErrorModel { ExceptionType = "Bad query", Message = "Authentication code is null" }; return(View(model)); } _logger.LogInformation("Got auth code"); try { AccessTokenData accessToken = await _vkAuthService.Auth(code); HttpContext.Session.Set("AccessToken", accessToken); User user = await _vkApiService.GetUser(accessToken.UserId, accessToken.AccessToken); HttpContext.Session.Set("User", user); } catch (Exception e) { _logger.LogError(e, "Unable to authorize in VK"); var errorModel = new VkErrorModel { ExceptionType = e.GetType().Name, Message = e.Message }; return(View(errorModel)); } return(Redirect("/")); } }
public Task UpdateFriendList(FriendsUpdateDto dto, ApplicationContext context = null) { Console.WriteLine($"{GetType()}.{nameof(UpdateFriendList)} started"); if (context == null) { context = new ApplicationContext(_contextService.Options); } if (dto.UserId == null) { dto.UserId = _vkApiService.GetCurrentId(); } //Находим текущего пользователя и обновляем его связи var currentUserEntity = context.VkUsers.FirstOrDefault(u => u.ExternalId == dto.UserId); if (currentUserEntity == null) { User vkUser; try { vkUser = _vkApiService.GetUser(dto.UserId); } catch { return(Task.CompletedTask); } currentUserEntity = new VkUser { ExternalId = vkUser.Id, IsDeactivated = false, PhotoUrl = vkUser.PhotoMaxOrig.ToString(), FullName = $"{vkUser.FirstName} {vkUser.LastName}" }; context.Add(currentUserEntity); context.SaveChanges(); } List <User> friends; try { friends = _vkApiService.GetFriends(currentUserEntity.ExternalId).ToList(); } catch { return(Task.CompletedTask); } context.RemoveRange(_friendsService.GetRemovedUsers(context, currentUserEntity.Id, friends)); var newFriends = _friendsService.GetNewUsers(context, currentUserEntity.Id, friends); foreach (var newFriend in newFriends.Where(f => context.VkUsers.All(u => u.ExternalId != f.Id))) { context.VkUsers.Add(new VkUser { ExternalId = newFriend.Id, FullName = $"{newFriend.FirstName} {newFriend.LastName}", IsDeactivated = newFriend.IsDeactivated, LastCheck = DateTime.Now, PhotoUrl = newFriend.PhotoMaxOrig?.ToString() }); } context.SaveChanges(); foreach (var friend in friends) { if (dto.Recursive) { UpdateFriendList(new FriendsUpdateDto { Recursive = false, UserId = friend.Id }, context); } var friendEntity = context.VkUsers.First(f => f.ExternalId == friend.Id); if (context.FriendsUserToUsers.Any(c => c.RightUserId == friendEntity.Id && c.LeftUserId == currentUserEntity.Id)) { continue; } context.FriendsUserToUsers.Add(new FriendsUserToUser { LeftUserId = currentUserEntity.Id, RightUserId = friendEntity.Id }); } context.SaveChanges(); Console.WriteLine($"{GetType()}.{nameof(UpdateFriendList)} completed"); return(Task.CompletedTask); }