public static List <AspNetUser> FindUsersWith(List <Ingredient> MissingIngredients) { pantrypartyEntities ORM = new pantrypartyEntities(); List <UserIngredient> UIList = new List <UserIngredient>(); List <AspNetUser> ToReturn = new List <AspNetUser>(); foreach (Ingredient ing in MissingIngredients) { UIList = ORM.UserIngredients.Where(x => x.IngredientID == ing.Name).ToList(); if (UIList == null) { continue; } else { foreach (UserIngredient item in UIList) { if (!ToReturn.Exists(x => x.ID == item.UserID)) { ToReturn.Add(ORM.AspNetUsers.Find(item.UserID)); } } UIList.Clear(); } } ToReturn = ToReturn.Distinct <AspNetUser>().ToList(); return(ToReturn); }
public static void SaveIngredients(Ingredient ing, string UserID) { pantrypartyEntities ORM = new pantrypartyEntities(); UserIngredient NewUserIngredient = new UserIngredient(); NewUserIngredient.UserID = UserID; NewUserIngredient.IngredientID = ing.Name; // check for distinct values before arbitrarily saving to DB if (ORM.Ingredients.Find(ing.Name) == null) { ORM.Ingredients.Add(ing); ORM.SaveChanges(); } // check for distinct values before arbitrarily saving to DB List <UserIngredient> CheckList = ORM.AspNetUsers.Find(UserID).UserIngredients.Where(x => x.IngredientID == ing.Name).ToList(); if (CheckList.Count == 0) { ORM.AspNetUsers.Find(UserID).UserIngredients.Add(NewUserIngredient); ORM.SaveChanges(); } }
// Checks if the ingredients for each returned recipe are in the DB // If not, adds them and the Recipe-Ingredient relationship public static void SaveNewRecipeIngredient(JObject IngArray, Recipe ThisRecipe) { pantrypartyEntities ORM = new pantrypartyEntities(); if (ORM.Recipes.Find(ThisRecipe.ID) == null) { ORM.Recipes.Add(ThisRecipe); ORM.SaveChanges(); } for (int i = 0; i < IngArray["extendedIngredients"].Count(); i++) { if (ORM.Ingredients.Find(IngArray["extendedIngredients"][i]["name"].ToString()) == null) { Ingredient newIngredient = new Ingredient { Name = IngArray["extendedIngredients"][i]["name"].ToString() }; ORM.Ingredients.Add(newIngredient); ORM.SaveChanges(); } RecipeIngredient ObjToCheck = new RecipeIngredient(); ObjToCheck.RecipeID = ThisRecipe.ID; ObjToCheck.IngredientID = IngArray["extendedIngredients"][i]["name"].ToString(); if (ORM.RecipeIngredients.Where(x => x.RecipeID == ObjToCheck.RecipeID).ToList().Count == 0) { ORM.Recipes.Find(ThisRecipe.ID).RecipeIngredients.Add(ObjToCheck); ORM.SaveChanges(); } } }
public static List <AspNetUser> FindNearbyUsers(List <AspNetUser> Users, string UserId) { pantrypartyEntities ORM = new pantrypartyEntities(); AspNetUser CurrentUser = ORM.AspNetUsers.Find(UserId); string city = CurrentUser.City; if (city.Contains(" ")) { city = city.Replace(" ", "+"); } List <AspNetUser> NearbyUsers = new List <AspNetUser>(); // Checks distance between you and all users with your missing ingredients foreach (AspNetUser User in Users) { if (!NearbyUsers.Contains(User)) { if (User.City.Contains(" ")) { User.City = User.City.Replace(" ", "+"); } string APIkey = System.Configuration.ConfigurationManager.AppSettings["Google Distance Matrix API KEY"]; HttpWebRequest request = WebRequest.CreateHttp("https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=" + city + ",MI&destinations=" + User.City + ",MI&key=" + APIkey); request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { StreamReader rd = new StreamReader(response.GetResponseStream()); string output = rd.ReadToEnd(); rd.Close(); JObject JParser = JObject.Parse(output); // Gets the distance between your city and another user's city and converts to a floating-point string DistanceAsString = JParser["rows"][0]["elements"][0]["distance"]["text"].ToString(); // also removes " mi" from end of DistanceAsString float DistanceAsFloat = float.Parse(DistanceAsString.Remove(DistanceAsString.Length - 3)); if (DistanceAsFloat <= 25) { // populates list with users within 20 miles of your city NearbyUsers.Add(User); } else { continue;//no one in your area yet } // end method } else // something is wrong { //return View("../Shared/Error"); } } } // end of foreach return(NearbyUsers); }
// Checks if Recipe exists in DB, if not, adds to DB, and adds User->Recipe relationship public static void SaveRecipes(Recipe ThisRecipe, string UserID) { pantrypartyEntities ORM = new pantrypartyEntities(); AspNetUser CurrentUser = ORM.AspNetUsers.Find(UserID); UserRecipe ToAdd = new UserRecipe(); ToAdd.UserID = UserID; ToAdd.RecipeID = ThisRecipe.ID; if (ORM.Recipes.Find(ThisRecipe.ID) == null) { ORM.Recipes.Add(ThisRecipe); ORM.SaveChanges(); } if (!ORM.UserRecipes.Where(x => x.UserID == UserID).Distinct().ToList().Contains(ToAdd)) { ORM.UserRecipes.Add(ToAdd); ORM.SaveChanges(); } }
public static void SaveRecipe(string RecipeID, string UserID) { pantrypartyEntities ORM = new pantrypartyEntities(); // some if statement }