public async Task <IActionResult> DeleteUser(EditUserViewModel userId) { var user = await userManager.FindByIdAsync(userId.Id); if (user == null) { return(View("AccessDenied")); } //deletes user's shared memories var allMemories = memorieRepository.UserSharedMemories(user.Id); if (allMemories != null) { foreach (var m in allMemories.ToList()) { await memorieRepository.DeleteMemorie(m); } } //deletes user's login (Google) var userLoginInfo = await userManager.GetLoginsAsync(user); if (userLoginInfo != null) { foreach (var info in userLoginInfo) { var userByInfo = await userManager.FindByLoginAsync(info.LoginProvider, info.ProviderKey); if (userByInfo.Id == user.Id) { await userManager.RemoveLoginAsync(userByInfo, info.LoginProvider, info.ProviderKey); } } } //Remove user's claims var userClaims = await userManager.GetClaimsAsync(user); if (userClaims != null) { await userManager.RemoveClaimsAsync(user, userClaims); } //Remove user from roles await RemoveUserFromRoles(user); var result = await userManager.DeleteAsync(user); if (result.Succeeded) { return(RedirectToAction("ListUsers")); } foreach (var error in result.Errors) { ModelState.AddModelError("", error.Description); } return(View("ListUsers")); }
public async Task <IActionResult> Delete(int?id) { Game game = gameRepository.GetById(id.Value); var memories = memorieRepository.GetAllMemories(); foreach (var m in memories) { if (m.gameId == game.Id) { await memorieRepository.DeleteMemorie(m); } } await uploadImage.Delete(game.PhotoUrl); gameRepository.Delete(game.Id); return(RedirectToAction("Games", "home")); }