public async Task <ActionResult> UpdateRepositories() { var user = await _userManager.GetUserAsync(User); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } var githubToken = (await _userManager.GetClaimsAsync(user)).FirstOrDefault(x => x.Type == "GithubAccessToken") ?.Value; var allRepositories = (await _githubService.GetAllRepositories(githubToken)).Select(x => x.ToGithubRepository()); var userRepositories = _githubService.GetUserRepositories(user.Id); var model = new UpdateGithubRepositoriesViewModel { RepositoryViewModels = allRepositories.Select(x => { var userRepo = userRepositories.FirstOrDefault(y => y.FullName == x.FullName); return(new GithubRepositoryViewModel { Name = x.Name, Owner = x.Owner, IsSelected = userRepo != null, Id = userRepo?.Id }); }).ToList() }; return(View(model)); }
public async Task <ActionResult> UpdateRepositories(UpdateGithubRepositoriesViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var user = await _userManager.GetUserAsync(User); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } var result = _githubService.UpdateUserRepositories(user.Id, model.RepositoryViewModels.Where(x => x.IsSelected).Select(x => x.ToGithubRepository())); if (result) { return(RedirectToAction("Index")); } return(View(model)); }