Beispiel #1
0
        public JsonResult FindName(string ingredientName, double volume)
        {
            Ingredient ing = _context.Ingredients.Include(i => i.Unit).FirstOrDefault(i => i.Name == ingredientName);

            if (ing == null)
            {
                return(Json(new ErrorMessage {
                    Message = "Ингредиент не найден"
                }));
            }

            UserIngredient ingredient = new UserIngredient
            {
                Name          = ing.Name,
                UnitName      = ing.Unit.Name,
                UnitShortName = ing.Unit.ShortName,
                Quantity      = volume,
                ID            = ing.ID,
            };

            ErrorMessage message = ingredient.IsIncorrect();

            if (message)
            {
                return(Json(message));
            }

            return(Json(ingredient));
        }
        public async Task <UserIngredient> AddUserIngredient(UserIngredient ingredient, string username)
        {
            var allUsers = await _userRepo.GetAll();

            var currentUser = allUsers.FirstOrDefault(u => u.Email == username);

            ingredient.UserId = Guid.Parse(currentUser.Id.ToString());
            return(await _mapper.Map(await _ingredientRepo.Insert(await _mapper.Map(ingredient))));
        }
Beispiel #3
0
 public async Task <Data.Entities.UserIngredient> Map(UserIngredient ingredient)
 {
     return(await Task.Run(() =>
     {
         var returnItem = new Data.Entities.UserIngredient
         {
             Name = ingredient.Name
         };
         if (ingredient.Id != null)
         {
             returnItem.Id = int.Parse(ingredient.Id.ToString());
         }
         if (ingredient.UserId != null)
         {
             returnItem.UserId = Guid.Parse(ingredient.UserId.ToString());
         }
         return returnItem;
     }));
 }
Beispiel #4
0
        public ActionResult CompareMissingIngredients(string ToCompare, string UserID)
        {
            UserRecipe.SaveRecipe(ToCompare, UserID);
            pantrypartyEntities ORM = new pantrypartyEntities();
            List <Ingredient>   RecipesIngredientsList = new List <Ingredient>();
            List <Ingredient>   MyIngredients          = new List <Ingredient>();

            // Creates list of RecipeIngredient objects and initializes RecipeIngredientsList with all matching values
            List <RecipeIngredient> ChangeToRecipesIng = ORM.RecipeIngredients.Where(x => x.RecipeID == ToCompare).ToList();

            foreach (RecipeIngredient x in ChangeToRecipesIng)
            {
                if (!RecipesIngredientsList.Contains(ORM.Ingredients.Find(x.IngredientID)))
                {
                    RecipesIngredientsList.Add(ORM.Ingredients.Find(x.IngredientID));
                }
            }

            // Creates list of UserIngredient objects and initializes UserIngredientList with all matching values
            List <UserIngredient> ChangeToUserIngredients = ORM.UserIngredients.Where(x => x.UserID == UserID).ToList();

            foreach (UserIngredient x in ChangeToUserIngredients)
            {
                if (!MyIngredients.Contains(ORM.Ingredients.Find(x.IngredientID)))
                {
                    MyIngredients.Add(ORM.Ingredients.Find(x.IngredientID));
                }
            }

            // Edits recipe's ingredients list to contain only missing ingredients
            foreach (Ingredient x in MyIngredients)
            {
                if (RecipesIngredientsList.Contains(x))
                {
                    RecipesIngredientsList.Remove(x);
                }
            }

            // Creates list of Users with any/all of your missing ingredients
            List <AspNetUser> CheckNearby = UserIngredient.FindUsersWith(RecipesIngredientsList);

            // Sends list of nearby users with your missing ingredients to page

            List <AspNetUser> NearbyUsers = AspNetUser.FindNearbyUsers(CheckNearby, UserID);

            ViewBag.NearbyUsers = NearbyUsers;

            ViewBag.CurrentUserLatLong = Geocode(UserID);
            ViewBag.LatLongArray       = Geocode(NearbyUsers, UserID);

            // Sends list of your missing ingredients to page
            // ViewBag.MissingIngredients = RecipesIngredientsList;

            List <UserIngredient> Test = new List <UserIngredient>();

            foreach (Ingredient item in RecipesIngredientsList)
            {
                Test.AddRange(ORM.UserIngredients.Where(x => x.IngredientID == item.Name));
            }
            ViewBag.UserIngredients = Test.Distinct().ToList();

            string APIkey = System.Configuration.ConfigurationManager.AppSettings["Google Marker API KEY"];

            ViewData["APIkey"] = APIkey;

            return(View("NearbyUsers"));
        }
 public async Task <IActionResult> AddUserIngredient(UserIngredient ingredient)
 {
     return(Ok(await _userIngredientService.AddUserIngredient(ingredient, User.Identity.Name)));
 }
Beispiel #6
0
        public IHttpActionResult Create([FromBody] LabelIngredientRequest req)
        {
            var ingredientName = req.IngredientName;
            var assignLabel    = req.AssignLabel;

            using (FFDBContext db = new FFDBContext())
            {
                string userID = User.Identity.GetUserId();
                if (userID != null)
                {
                    int ingredientID = db.LabelledIngredients.Where(s => s.Name == ingredientName).Select(s => s.ID).FirstOrDefault();
                    if (ingredientID == 0)
                    {
                        LabelledIngredient labelIngredient = new LabelledIngredient {
                            Name = ingredientName
                        };
                        try
                        {
                            db.LabelledIngredients.Add(labelIngredient);
                            db.SaveChanges();
                            ingredientID = labelIngredient.ID;
                        }
                        catch
                        {
                            var jsonData_failed_ingredient_add = new JsonResponse
                            {
                                success  = false,
                                message  = "Ingredient not added to database",
                                redirect = false
                            };


                            return(Ok(jsonData_failed_ingredient_add));
                        }
                    }
                    if (db.UserIngredients.Where(s => s.LabelledIngredientID == ingredientID && s.userID == userID).Count() > 0)
                    {
                        var existingRecord = db.UserIngredients.FirstOrDefault(s => s.LabelledIngredientID == ingredientID && s.userID == userID);
                        existingRecord.Label = assignLabel;
                        db.SaveChanges();
                        var jsonData_edit_record = new JsonResponse
                        {
                            success  = true,
                            message  = "Label has been changed in the record",
                            redirect = false
                        };

                        return(Ok(jsonData_edit_record));
                    }
                    else
                    {
                        UserIngredient userIng = new UserIngredient(userID, assignLabel, ingredientID);
                        try
                        {
                            db.UserIngredients.Add(userIng);
                            db.SaveChanges();
                            var jsonData_success = new JsonResponse
                            {
                                success  = true,
                                message  = "Label has been saved.",
                                redirect = false
                            };

                            return(Ok(jsonData_success));
                        }
                        catch (Exception e)
                        {
                            var jsonData_fail = new JsonResponse
                            {
                                success  = false,
                                message  = $"Something went wrong: {e.InnerException.InnerException.Message}",
                                redirect = false
                            };

                            return(Ok(jsonData_fail));
                        }
                    }
                }

                var jsonData3 = new JsonResponse
                {
                    success  = false,
                    message  = "User not logged in.",
                    redirect = true
                };

                return(Ok(jsonData3));
            }
        }
Beispiel #7
0
        //[ValidateAntiForgeryToken]
        public ContentResult Create(string assignLabel, string ingredientName)
        {
            string userID = User.Identity.GetUserId();

            if (userID != null)
            {
                int ingredientID = db.LabelledIngredients.Where(s => s.Name == ingredientName).Select(s => s.ID).FirstOrDefault();
                if (ingredientID == 0)
                {
                    LabelledIngredient labelIngredient = new LabelledIngredient();
                    labelIngredient.Name = ingredientName;
                    try
                    {
                        db.LabelledIngredients.Add(labelIngredient);
                        db.SaveChanges();
                        ingredientID = labelIngredient.ID;
                    }
                    catch
                    {
                        var jsonData_failed_ingredient_add = new
                        {
                            success  = false,
                            message  = "Ingredient not added to database",
                            redirect = false
                        };

                        var result = JObject.FromObject(jsonData_failed_ingredient_add);
                        return(Content(result.ToString(), "Application/json"));
                    }
                }
                if (db.UserIngredients.Where(s => s.LabelledIngredientID == ingredientID && s.userID == userID).Count() > 0)
                {
                    var existingRecord = db.UserIngredients.FirstOrDefault(s => s.LabelledIngredientID == ingredientID && s.userID == userID);
                    existingRecord.Label = assignLabel;
                    db.SaveChanges();
                    var jsonData_edit_record = new
                    {
                        success  = true,
                        message  = "Label has been changed in the record",
                        redirect = false
                    };

                    var result = JObject.FromObject(jsonData_edit_record);
                    return(Content(result.ToString(), "Application/json"));
                }
                else
                {
                    UserIngredient userIng = new UserIngredient(userID, assignLabel, ingredientID);
                    try
                    {
                        db.UserIngredients.Add(userIng);
                        db.SaveChanges();
                        var jsonData_success = new
                        {
                            success  = true,
                            message  = "Label has been saved.",
                            redirect = false
                        };

                        var result = JObject.FromObject(jsonData_success);
                        return(Content(result.ToString(), "Application/json"));
                    }
                    catch
                    {
                        var jsonData_fail = new
                        {
                            success  = false,
                            message  = "Something went wrong",
                            redirect = false
                        };

                        var result2 = JObject.FromObject(jsonData_fail);
                        return(Content(result2.ToString(), "Application/json"));
                    }
                }
            }

            var jsonData3 = new
            {
                success  = false,
                message  = "User not logged in.",
                redirect = true
            };

            var result3 = JObject.FromObject(jsonData3);

            return(Content(result3.ToString(), "Application/json"));
        }