public async Task AddIngredientCustom(Guid recipeId, string categoryName, string measureUnit, string name,
                                              double quantity, double cost, double weight)
        {
            Ingredient ingredient;

            if (!await Exists(name, measureUnit))
            {
                Guid categoryId;
                if (!await _ingredientsCategoryRepository.Exists(categoryName))
                {
                    IngredientCategory igCat = IngredientCategory.Create(categoryName);
                    await _ingredientsCategoryRepository.Add(igCat);

                    categoryId = igCat.Id;
                }
                else
                {
                    categoryId = (await _ingredientsCategoryRepository.GetByName(categoryName)).Id;
                }
                double scale = IngredientsRepository.MapMeasurement["undefined"];
                if (IngredientsRepository.MapMeasurement.ContainsKey(measureUnit))
                {
                    scale = IngredientsRepository.MapMeasurement[measureUnit];
                }

                ingredient = Ingredient.Create(categoryId, name, measureUnit, cost * scale * weight / 10000000);
                await Add(ingredient);
                await UpdateIngredientsCategory(name, await GetSpecificCategory(name));
            }
            else
            {
                if (!categoryName.Equals("other-ingredients"))
                {
                    if (!await _ingredientsCategoryRepository.Exists(categoryName))
                    {
                        IngredientCategory igCat = IngredientCategory.Create(categoryName);
                        await _ingredientsCategoryRepository.Add(igCat);
                    }
                    await UpdateIngredientsCategory(name, (await _ingredientsCategoryRepository.GetByName(categoryName)).Id);
                }
                ingredient = await GetByNameAndMeasure(name, measureUnit);
            }
            await _fridgeRepository.Add(PairItem.Create(ingredient.Id, recipeId, quantity));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddToFridge(FridgeViewModel fridge)
        {
            User user = await _signInManager.UserManager.GetUserAsync(User);

            var products = await productRepository.Products();

            products = products.OrderBy(p => p.Name);

            if (!ModelState.IsValid)
            {
                if (ModelState["Id"].Errors.Count != 0)
                {
                    ModelState.Remove("Id");
                    ModelState.SetModelValue("Id", new ValueProviderResult("Należy wybrać produkt", CultureInfo.InvariantCulture));
                    ModelState.AddModelError("Id", "Należy wybrać produkt");
                }

                fridge.Products = products;
                fridge.Fridges  = await fridgeRepository.GetUserProducts(user);

                return(View("Fridge", fridge));
            }

            Fridge productInFridge = await fridgeRepository.FindAsync(f => f.Product.Id == fridge.Id && f.User.Id == user.Id);

            if (productInFridge != null)
            {
                productInFridge.Quantity = (int)fridge.Quantity;
                fridgeRepository.Update(productInFridge);
            }
            else
            {
                Product product = await productRepository.FindAsync(p => p.Id == fridge.Id);

                fridgeRepository.Add(new Fridge {
                    Product = product, User = user, Quantity = (int)fridge.Quantity
                });
            }

            return(RedirectToAction("Fridge"));
        }