public JsonResult GetMatchingIngredients(List<int> listOfIds) { if (listOfIds == null || listOfIds.Count == 0) { using (BlondsCookingContext context = new BlondsCookingContext()) { var result = context.Ingredients.ToList(); return Json(result); } } else { using (BlondsCookingContext context = new BlondsCookingContext()) { List<string> ingredientsList = context.Ingredients.Where(ingrdient => listOfIds.Contains(ingrdient.Id)) .Select(ingredient => ingredient.Name) .ToList(); IngredientsPairingHelper pairingHelper = new IngredientsPairingHelper(); var pairedIngredients = pairingHelper.CalculatePercentagePairingForIngredient(ingredientsList); var pairedIngredientsSorted = from pair in pairedIngredients orderby pair.Value descending select pair; List<IngredientMatchViewModel> result = new List<IngredientMatchViewModel>(); foreach (var pairedIngredient in pairedIngredientsSorted) { result.Add(new IngredientMatchViewModel(pairedIngredient.Key, pairedIngredient.Value)); } return Json(result); } } }
public ActionResult Recipe(int id = 1) { string userId = User.Identity.GetUserId(); context = new BlondsCookingContext(); Recipe recipeModel = context.Recipes.FirstOrDefault(recipe => recipe.Id == id); UserRating rate = context.UserRatings.Where( rating => rating.RecipeId == id && rating.UserId.CompareTo(userId) == 0) .FirstOrDefault(); if (recipeModel == null) { return View("Error"); } RecipeRateViewModel recipeRateViewModel = new RecipeRateViewModel() { Id = recipeModel.Id, Name = recipeModel.Name, Description = recipeModel.Description, Image = recipeModel.Image, Ingredients = recipeModel.Ingredients, Temperature = recipeModel.Temperature, Time = recipeModel.Time, Rate = (rate == null) ? null : (int?)rate.Rate }; DishesCompairingHelper helper = new DishesCompairingHelper(); List<Recipe> similarRecipes = new List<Recipe>(helper.GetTopThreeMostSimilarDishes(recipeModel)); model = new RecipeViewModel(recipeRateViewModel, similarRecipes); return View(model); }
public List<Recipe> GetTopThreeMostSimilarDishes(Recipe selectedRecipe) { List<Recipe> threeMostSimilarDishes = new List<Recipe>(); Dictionary<int, double> recipeAndSimilarityValue = new Dictionary<int, double>(); using (BlondsCookingContext context = new BlondsCookingContext()) { List<Recipe> othersRecipes = context.Recipes.Where(recipe => recipe.Id != selectedRecipe.Id).ToList(); foreach (var otherRecipe in othersRecipes) { var spicyDistance = Math.Pow((otherRecipe.SpicyValue - selectedRecipe.SpicyValue), 2); var saltyDistance = Math.Pow((otherRecipe.SaltyValue - selectedRecipe.SaltyValue), 2); var bitterDistance = Math.Pow((otherRecipe.BitterValue - selectedRecipe.BitterValue), 2); var sweetDistance = Math.Pow((otherRecipe.SweetValue - selectedRecipe.SweetValue), 2); var meatDistance = Math.Pow((otherRecipe.MeatValue - selectedRecipe.MeatValue), 2); var sourDistance = Math.Pow((otherRecipe.SourValue - selectedRecipe.SourValue), 2); var wholeDistance = Math.Sqrt(spicyDistance + saltyDistance + bitterDistance + sweetDistance + meatDistance + sourDistance); recipeAndSimilarityValue.Add(otherRecipe.Id, wholeDistance); } var sortedRecipes = recipeAndSimilarityValue.OrderBy(x => x.Value); var selectedRecipes = sortedRecipes.Take(3).Select(x => x.Key); threeMostSimilarDishes = new List<Recipe>(context.Recipes.Where(recipe => selectedRecipes.Contains(recipe.Id)).ToList()); } return threeMostSimilarDishes; }
public JsonResult Rate(int id, int rateValue) { RecommendationHelper helper = new RecommendationHelper(); if (id != 0) { using (BlondsCookingContext context = new BlondsCookingContext()) { context.UserRatings.Add(new UserRating() { RecipeId = id, Rate = rateValue, UserId = User.Identity.GetUserId() }); context.SaveChanges(); } } if (helper.CanRecalculateUserParameters(User.Identity.GetUserId())) { RecommendationHelper recommendationHelper = new RecommendationHelper(); var ratesAndParametersOfDishesRatedByUser = recommendationHelper.GetRatesAndParametersOfDishesRatedByUser(User.Identity.GetUserId()); UserParametersHelper userParametersHelper = new UserParametersHelper(); userParametersHelper.CalculateParametersForUser(ratesAndParametersOfDishesRatedByUser); } return Json(id); }
public Dictionary<string, double> CalculatePercentagePairingForIngredient(List<string> selectedIngredients) { Dictionary<string, double> percentagePairing = new Dictionary<string, double>(); using (BlondsCookingContext context = new BlondsCookingContext()) { List<Ingredient> ingredientsExceptSelected = context.Ingredients.Where(ingredient => !selectedIngredients.Contains(ingredient.Name)).ToList(); List<Recipe> recipesWithSelectedIngredients = context.Recipes.Where( recipe => selectedIngredients.All(ingredient => recipe.IngredientsVector.Contains(ingredient))) .ToList(); foreach (var ingredient in ingredientsExceptSelected) { double percentagePairingValue; var recipesWithAllIngredients = recipesWithSelectedIngredients.Where( recipe => recipe.IngredientsVector.Contains(ingredient.Name)).ToList(); if (recipesWithAllIngredients.Count == 0 || recipesWithSelectedIngredients.Count == 0) { percentagePairingValue = 0; } else { percentagePairingValue = (double)recipesWithAllIngredients.Count / recipesWithSelectedIngredients.Count; } percentagePairing.Add(ingredient.Name, percentagePairingValue); } } return percentagePairing; }
public List<Rating> GetRatesAndParametersOfDishesRatedByUser(string userId) { List<Rating> ratesAndParametersOfDishesRatedByUser = new List<Rating>(); using (BlondsCookingContext context = new BlondsCookingContext()) { List<int> dishesId = context.UserRatings.Where(rating => rating.UserId.CompareTo(userId) == 0).Select(rating => rating.RecipeId).ToList(); foreach (var dishId in dishesId) { Rating newRating = new Rating(); newRating.DishParameters = new double[] { context.Recipes.Where(dish => dish.Id == dishId).Select(dish => dish.SpicyValue).FirstOrDefault(), context.Recipes.Where(dish => dish.Id == dishId).Select(dish => dish.SaltyValue).FirstOrDefault(), context.Recipes.Where(dish => dish.Id == dishId).Select(dish => dish.BitterValue).FirstOrDefault(), context.Recipes.Where(dish => dish.Id == dishId).Select(dish => dish.SweetValue).FirstOrDefault(), context.Recipes.Where(dish => dish.Id == dishId).Select(dish => dish.MeatValue).FirstOrDefault(), context.Recipes.Where(dish => dish.Id == dishId).Select(dish => dish.SourValue).FirstOrDefault() }; newRating.DishId = dishId; newRating.Rate = context.UserRatings.Where( rating => rating.RecipeId == dishId && rating.UserId.CompareTo(userId) == 0) .Select(rating => rating.Rate) .FirstOrDefault(); newRating.UserId = userId; ratesAndParametersOfDishesRatedByUser.Add(newRating); } } return ratesAndParametersOfDishesRatedByUser; }
public JsonResult GetIngredients(int id = 0) { using (BlondsCookingContext context = new BlondsCookingContext()) { var result = context.Ingredients.ToList(); return Json(result, JsonRequestBehavior.AllowGet); } }
public bool CanRecalculateUserParameters(string userId) { using (BlondsCookingContext context = new BlondsCookingContext()) { var countOfRates = context.UserRatings.Count(rating => rating.UserId.CompareTo(userId) == 0); if (countOfRates >= MinimumNumberOfRates) { return true; } return false; } return false; }
protected void Application_Start() { //Test test = new Test(); //test.TestMethod(); using (BlondsCookingContext context = new BlondsCookingContext()) { var categories = context.Categories.Count(); var recipes = context.Recipes.Count(); } AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); }
public List<string> GetUniqueIngredients(BlondsCookingContext context) { List<string> uniqueIngredients = new List<string>(); foreach (var recipe in context.Recipes) { var ingredientsVector = recipe.IngredientsVector; List<string> ingredients = ingredientsVector.Split(',').ToList(); foreach (var ingredient in ingredients) { if (!uniqueIngredients.Contains(ingredient)) { uniqueIngredients.Add(ingredient); } } } return uniqueIngredients; }
public IngredientMatchViewModel(string name, double matchValue) { Name = name; MatchValue = matchValue; using (BlondsCookingContext context = new BlondsCookingContext()) { Id = context.Ingredients.Where(ingredient => ingredient.Name.CompareTo(Name) == 0) .Select(ingredient => ingredient.Id) .FirstOrDefault(); } if (MatchValue >= 0.3) { MatchingIconUrl = "../../Images/Layout/best-match.png"; } else if (MatchValue < 0.3 && MatchValue >= 0.1) { MatchingIconUrl = "../../Images/Layout/weak-match.png"; } else { MatchingIconUrl = "../../Images/Layout/weakest-match.png"; } }
public List<Recipe> GetRecommendedRecipesForUser(string userId) { List<Recipe> recommendedRecipesForUser = new List<Recipe>(); RecommendedDishesHelper helper = new RecommendedDishesHelper(); var ratesAndParametersOfDishesRatedByUser = GetRatesAndParametersOfDishesRatedByUser(userId); var dishesUnratedByUser = GetDishesUnratedByUser(userId); var recommendedDishes = helper.CalculatePredictedRateForUnratedDishes(ratesAndParametersOfDishesRatedByUser, dishesUnratedByUser); using (BlondsCookingContext context = new BlondsCookingContext()) { foreach (var recommendedDish in recommendedDishes) { var recipeBd = context.Recipes.Where(recipe => recipe.Id == recommendedDish.DishId).FirstOrDefault(); recommendedRecipesForUser.Add(recipeBd); } } return recommendedRecipesForUser; }
private List<Dish> GetDishesUnratedByUser(string userId) { List<Dish> dishesUnratedByUser = new List<Dish>(); using (BlondsCookingContext context = new BlondsCookingContext()) { var ratedDishesId = context.UserRatings.Where(rating => rating.UserId.CompareTo(userId) == 0).Select(rating => rating.RecipeId).ToList(); var unratedDishes = context.Recipes.Where(recipe => !ratedDishesId.Contains(recipe.Id)).ToList(); foreach (var unratedDish in unratedDishes) { dishesUnratedByUser.Add(new Dish() { DishId = unratedDish.Id, DishParameters = new double[] { unratedDish.SpicyValue, unratedDish.SaltyValue, unratedDish.BitterValue, unratedDish.SweetValue, unratedDish.MeatValue, unratedDish.SourValue } }); } } return dishesUnratedByUser; }
public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { //await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link using (BlondsCookingContext context = new BlondsCookingContext()) { context.Users.Add(new User() { IdSecure = user.Id, Email = user.Email }); context.SaveChanges(); } string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); ViewBag.Message = "Check your email and confirm your account, you must be confirmed " + "before you can log in."; return View("Info"); //return RedirectToAction("Index", "Home"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); }