public IActionResult Saved() { var currentUserId = userManager.GetUserId(HttpContext.User); var saves = myContext.Saves.Where(x => x.UserId == currentUserId).OrderByDescending(x => x.SavedAt).ToList(); var savedRecipes = new List <RecentRecipes>(); foreach (var saved in saves) { var recipe = myContext.Recipes.FirstOrDefault(x => x.RecipeId == saved.RecipeId); var recipeUser = myContext.Userz.FirstOrDefault(x => x.Id == recipe.CreatedBy); var recipeToShow = new RecentRecipes() { Image = recipe.Image, RecipeId = recipe.RecipeId, Name = recipe.Name, IsSaved = true, CreatedBy = recipeUser.Name }; savedRecipes.Add(recipeToShow); } return(View(savedRecipes)); }
public IActionResult Index() { var currentUserId = userManager.GetUserId(HttpContext.User); //recent recipes var recentRecipes = new List <RecentRecipes>(); var recentAddedRecipes = myContext.Recipes.OrderByDescending(x => x.CreatedAt).Take(3).ToList(); foreach (var recipe in recentAddedRecipes) { var user = myContext.Userz.FirstOrDefault(x => x.Id == recipe.CreatedBy); var isSaved = myContext.Saves.Any(x => x.UserId == currentUserId && x.RecipeId == recipe.RecipeId); var recipeToShow = new RecentRecipes() { Image = recipe.Image, RecipeId = recipe.RecipeId, Name = recipe.Name, IsSaved = isSaved, CreatedBy = user.Name }; recentRecipes.Add(recipeToShow); } ViewBag.RecentRecipes = recentRecipes.ToList(); //main dashboard var dashboard = new List <DashboardViewModel>(); var following = myContext.Followers.Where(x => x.UserId == currentUserId).ToList(); if (following.Count() != 0) { foreach (var follower in following) { var followerRecipes = myContext.Recipes.Include(x => x.RecipeIngredients).OrderByDescending(x => x.CreatedAt).Where(x => x.CreatedBy == follower.FollowerId || x.CreatedBy == currentUserId).ToList(); foreach (var recipe in followerRecipes) { var totalRating = 0; var sumRating = 0; var ratings = myContext.Reviews.Where(x => x.RecipeId == recipe.RecipeId).ToList(); foreach (var rating in ratings) { sumRating += rating.Rating; totalRating++; } var isSaved = myContext.Saves.Any(x => x.UserId == currentUserId && x.RecipeId == recipe.RecipeId); var recipeUser = myContext.Userz.FirstOrDefault(x => x.Id == recipe.CreatedBy); var dashboardRecipe = new DashboardViewModel() { Recipe = recipe, IsSaved = isSaved, CreatedBy = recipeUser.Name, Rating = sumRating == 0 ? 0 : System.Math.Round((sumRating / (double)totalRating), 2) }; dashboard.Add(dashboardRecipe); } } } else { var myRecipes = myContext.Recipes.Include(x => x.RecipeIngredients).OrderByDescending(x => x.CreatedAt).Where(x => x.CreatedBy == currentUserId).ToList(); foreach (var recipe in myRecipes) { var isSaved = myContext.Saves.Any(x => x.UserId == currentUserId && x.RecipeId == recipe.RecipeId); var recipeUser = myContext.Userz.FirstOrDefault(x => x.Id == recipe.CreatedBy); var dashboardRecipe = new DashboardViewModel() { Recipe = recipe, IsSaved = isSaved, CreatedBy = recipeUser.Name }; dashboard.Add(dashboardRecipe); } } ViewBag.DashboardRecipes = dashboard.ToList(); return(View()); }