//Private method that records the average of a user's price reviews from all their reviews in the database //This creates the price average variable to guide the user towards their own usual preference private void CalcAverage() { //Takes all the reviews in the database and puts them into a list var reviews = _context.Review.ToList(); double priceAverage = 0; int counter = 0; var id = User.FindFirst(ClaimTypes.NameIdentifier).Value; foreach (var review in reviews) { if (review.UserId == id) { priceAverage = priceAverage + (double)(review.ReviewOfPrice); counter++; } } if (counter != 0) { priceAverage = priceAverage / counter; } //If the user does not have any reviews, this creates a default price average of 1 if (double.IsNaN(priceAverage)) { priceAverage = 1; } var user = _context.AspNetUsers.Where(x => x.Id == id).First(); user.PriceAverage = priceAverage; _context.Entry(user).State = EntityState.Modified; _context.SaveChanges(); }
//Creating a method to update a user's reviews, passing in all the parameters needed from a RestaurantRoot model public IActionResult ReviewChange(string change, string restaurantId, int reviewOfPrice, double reviewOfType1, double reviewOfType2, double reviewOfType3, double reviewOfRating, string restaurantName) { //Finding the current user and putting their reviews into a List string userId = User.FindFirst(ClaimTypes.NameIdentifier).Value; var reviewList = _context.Review.Where(x => x.UserId == userId).ToList(); Review review = new Review(); //Finding the review we want to adjust based on the Restaurant Id we passed in foreach (var currentReview in reviewList) { if (currentReview.RestaurantId == restaurantId) { review = currentReview; } } //If Delete button is chosen, this delete the review from the database if (change == "Delete") { _context.Review.Remove(review); } //If Update button is chosen, this updates any changes the user made else if (change == "Update") { review.RestaurantName = restaurantName; review.RestaurantId = restaurantId; review.UserId = userId; review.ReviewOfPrice = reviewOfPrice; review.ReviewOfRating = reviewOfRating; review.ReviewOfType1 = reviewOfType1; review.ReviewOfType2 = reviewOfType2; review.ReviewOfType3 = reviewOfType3; _context.Entry(review).State = Microsoft.EntityFrameworkCore.EntityState.Modified; _context.Review.Update(review); } //Saves any changes made in the database and sends the user back to the Home Page - Preference Index _context.SaveChanges(); return(RedirectToAction("PreferenceIndex", "Snack")); }