Пример #1
0
        private void OnAddCommandExecute()
        {
            IsDialogOpen = false;
            newProduct   = new Product
            {
                Id              = Guid.NewGuid(),
                Name            = NewProductName,
                Description     = NewProductDescription,
                ProductCategory = NewProductCategory
            };

            Products.Add(newProduct);
            _productsRepository.AddProduct(newProduct);
            _productsRepository.Save();
            RecipeProduct recipeProduct = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = SelectedRecipe.Id,
                ProductId = newProduct.Id
            };

            SelectedRecipe.RecipeProducts.Add(recipeProduct);
            SelectedRecipeProductLookups.Add(new RecipeProductLookup
            {
                ProductId       = newProduct.Id,
                RecipeProductId = recipeProduct.Id,
                ProductName     = newProduct.Name
            });
        }
Пример #2
0
        public async Task <IActionResult> Edit(int id, [Bind("RecipeProductID,RecipeID,ProductID")] RecipeProduct recipeProduct)
        {
            if (id != recipeProduct.RecipeProductID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(recipeProduct);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RecipeProductExists(recipeProduct.RecipeProductID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Index"));
            }
            ViewData["ProductID"] = new SelectList(_context.Product, "ProductId", "ProductId", recipeProduct.ProductID);
            ViewData["RecipeID"]  = new SelectList(_context.Recipe, "RecipeID", "RecipeID", recipeProduct.RecipeID);
            return(View(recipeProduct));
        }
Пример #3
0
 private void OnAddRecipeProduct()
 {
     if (SelectedRecipe != null && SelectedSearchBoxProduct != null &&
         !SelectedRecipeProductLookups.Where(srpl => srpl.ProductId == SelectedSearchBoxProduct.Id).Any())
     {
         if (SelectedSearchBoxProduct.Id.Equals(Guid.Parse("00000000-0000-0000-0000-000000000000")))
         {
             return;
         }
         RecipeProduct recipeProduct = new RecipeProduct
         {
             Id        = Guid.NewGuid(),
             RecipeId  = SelectedRecipe.Id,
             ProductId = SelectedSearchBoxProduct.Id
         };
         SelectedRecipe.RecipeProducts.Add(recipeProduct);
         SelectedRecipeProductLookups.Add(new RecipeProductLookup
         {
             ProductId       = SelectedSearchBoxProduct.Id,
             ProductName     = SelectedSearchBoxProduct.Name,
             RecipeProductId = recipeProduct.Id
         });
         OnPropertyChanged();
     }
     ClearSelectedProductUserInput();
 }
Пример #4
0
        public async Task <IActionResult> Create([Bind("RecipeProductID,RecipeID,ProductID")] RecipeProduct recipeProduct)
        {
            if (ModelState.IsValid)
            {
                _context.Add(recipeProduct);
                await _context.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            ViewData["ProductID"] = new SelectList(_context.Product, "ProductId", "ProductId", recipeProduct.ProductID);
            ViewData["RecipeID"]  = new SelectList(_context.Recipe, "RecipeID", "RecipeID", recipeProduct.RecipeID);
            return(View(recipeProduct));
        }
Пример #5
0
        /// <summary>
        /// Metoda importująca produkt
        /// </summary>
        /// <param name="importProducts">Obiekty trzymające informacje o importowanych produktach</param>
        /// <returns>Produkty domenowe</returns>
        public async Task <ICollection <RecipeProduct> > ImportProductsAsync(List <RecipeImport.Product> importProducts)
        {
            List <RecipeProduct> importedProducts = new List <RecipeProduct>();

            foreach (var importProduct in importProducts)
            {
                var product = await GetOrCreateProductByNameAsync(importProduct.Name);

                var unit = await _unitsService.GetOrCreateUnitByNameAsync(importProduct.Unit);

                RecipeProduct recipeProduct = new RecipeProduct();
                recipeProduct.NumberOfUnit = importProduct.Amount;
                recipeProduct.ProductId    = product.Id;
                recipeProduct.UnitId       = unit.Id;
                importedProducts.Add(recipeProduct);
            }

            return(importedProducts);
        }
Пример #6
0
        public async Task <IActionResult> Edit(KitchenVM viewmodel)
        {
            if (viewmodel.Recipe == null)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    Recipe recipe = viewmodel.Recipe;
                    foreach (int element in viewmodel.SelectedProducts)
                    {
                        var recipeProduct = new RecipeProduct
                        {
                            RecipeId  = recipe.Id,
                            ProductId = Convert.ToInt32(element)
                        };
                        if (_context.RecipeProducts.Find(recipeProduct.RecipeId, recipeProduct.ProductId) == null)
                        {
                            _context.Add(recipeProduct);
                        }
                    }
                    _context.Update(recipe);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RecipeExists(viewmodel.Recipe.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(viewmodel));
        }
Пример #7
0
 private void OnAddRecipeProduct()
 {
     if (SelectedSearchBoxProduct != null &&
         !SelectedRecipeProductLookups.Where(srpl => srpl.ProductId == SelectedSearchBoxProduct.Id).Any())
     {
         RecipeProduct recipeProduct = new RecipeProduct
         {
             Id        = Guid.NewGuid(),
             RecipeId  = SelectedRecipe.Id,
             ProductId = SelectedSearchBoxProduct.Id
         };
         SelectedRecipe.RecipeProducts.Add(recipeProduct);
         SelectedRecipeProductLookups.Add(new RecipeProductLookup
         {
             ProductId       = SelectedSearchBoxProduct.Id,
             ProductName     = SelectedSearchBoxProduct.Name,
             RecipeProductId = recipeProduct.Id
         });
         OnPropertyChanged();
     }
 }
Пример #8
0
        private FoodNetDbContext GetContextWithData()
        {
            var options = new DbContextOptionsBuilder <FoodNetDbContext>()
                          .UseInMemoryDatabase(Guid.NewGuid().ToString())
                          .Options;
            var ctx = new FoodNetDbContext(options);

            user1 = new User
            {
                Id    = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD"),
                Name  = "TestUser1",
                Email = "*****@*****.**"
            };
            fridge1 = new Fridge
            {
                Id     = Guid.NewGuid(),
                UserId = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD")
            };

            user2 = new User
            {
                Id    = secondUserGuid,
                Name  = "TestUser2",
                Email = "*****@*****.**"
            };
            fridge2 = new Fridge
            {
                Id     = Guid.NewGuid(),
                UserId = Guid.Parse("B71DBC28-AAAA-47B1-8A2A-C6A5BFCD78AD")
            };

            productCategory1 = new ProductCategory
            {
                Id   = Guid.Parse("A5ADA8E6-04E1-49CA-A701-1265E216D69A"),
                Name = "Product Category 1"
            };
            productCategory2 = new ProductCategory
            {
                Id   = Guid.Parse("94CAE204-3337-43FA-8C0E-24C927BACAC4"),
                Name = "Product Category 2"
            };
            productCategory3 = new ProductCategory
            {
                Id   = Guid.NewGuid(),
                Name = "Product Category 3"
            };

            product1 = new BasicProduct
            {
                Id                = Guid.Parse("9C750539-3CA6-4239-941F-805B81C38CD4"),
                Name              = "Salmon",
                Description       = "",
                ProductCategoryId = productCategory1.Id,
                UserId            = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD")
            };
            product2 = new BasicProduct
            {
                Id                = Guid.Parse("9D8FAC6F-C194-44E5-A6D5-B6F6DBDEBBD0"),
                Name              = "Mustard",
                Description       = "",
                ProductCategoryId = productCategory1.Id,
                UserId            = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD")
            };
            product3 = new BasicProduct
            {
                Id                = Guid.Parse("F1F5610D-F065-46D8-9208-D7D1A0CB8C27"),
                Name              = "Brown Sugar",
                Description       = "",
                ProductCategoryId = productCategory1.Id,
                UserId            = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD")
            };

            product4 = new BasicProduct
            {
                Id                = Guid.Parse("06BBED64-A505-492C-889B-472F7DAB0FAA"),
                Name              = "Butter",
                Description       = "",
                ProductCategoryId = productCategory2.Id,
                UserId            = secondUserGuid
            };
            product5 = new BasicProduct
            {
                Id                = Guid.Parse("270B1180-45C6-4EE1-BC06-4819743859DC"),
                Name              = "Cream",
                Description       = "",
                ProductCategoryId = productCategory2.Id,
                UserId            = secondUserGuid
            };
            product6 = new BasicProduct
            {
                Id                = Guid.Parse("002D754C-176B-4324-A42E-2DC32369074D"),
                Name              = "Eggs",
                Description       = "",
                ProductCategoryId = productCategory2.Id,
                UserId            = secondUserGuid
            };

            newProduct1 = new NewProduct
            {
                Id                = Guid.NewGuid(),
                Name              = "New product 1",
                Description       = "",
                ProductCategoryId = productCategory1.Id,
                UserId            = firstUserGuid
            };
            newProduct2 = new NewProduct
            {
                Id                = Guid.NewGuid(),
                Name              = "New product 2",
                Description       = "",
                ProductCategoryId = productCategory2.Id,
                UserId            = secondUserGuid
            };
            newProduct3 = new NewProduct
            {
                Id                = Guid.NewGuid(),
                Name              = "New product 3",
                Description       = "",
                ProductCategoryId = productCategory2.Id,
                UserId            = secondUserGuid
            };

            recipe1 = new Recipe
            {
                Id          = Guid.Parse("83BD2A25-83EA-47D0-9B7B-0E4D528CF8C2"),
                Description = "",
                Title       = "Salmon recipe",
                UserId      = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD")
            };
            recipe2 = new Recipe
            {
                Id          = Guid.Parse("F795B317-DB3B-469F-9891-62C5CCC9DF5D"),
                Description = "",
                Title       = "Scrambled eggs",
                UserId      = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD")
            };

            recipe1Product1 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = recipe1.Id,
                ProductId = product1.Id
            };
            recipe1Product2 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = recipe1.Id,
                ProductId = product2.Id
            };
            recipe1Product3 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = recipe1.Id,
                ProductId = product3.Id
            };
            recipe2Product4 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = recipe2.Id,
                ProductId = product4.Id
            };
            recipe2Product5 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = recipe2.Id,
                ProductId = product5.Id
            };
            recipe2Product6 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = recipe2.Id,
                ProductId = product6.Id
            };

            fridge1Product1 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge1.Id,
                ProductId = product1.Id
            };
            fridge1Product2 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge1.Id,
                ProductId = product2.Id
            };
            fridge1Product3 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge1.Id,
                ProductId = product3.Id
            };
            fridge1Product4 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge1.Id,
                ProductId = product4.Id
            };
            fridge1Product5 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge1.Id,
                ProductId = product5.Id
            };
            fridge1Product6 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge1.Id,
                ProductId = product6.Id
            };

            ctx.ProductCategories.Add(productCategory1);
            ctx.ProductCategories.Add(productCategory2);
            ctx.ProductCategories.Add(productCategory3);

            ctx.Products.Add(product1);
            ctx.Products.Add(product2);
            ctx.Products.Add(product3);
            ctx.Products.Add(product4);
            ctx.Products.Add(product5);
            ctx.Products.Add(product6);

            ctx.Products.Add(newProduct1);
            ctx.Products.Add(newProduct2);
            ctx.Products.Add(newProduct3);

            ctx.Recipes.Add(recipe1);
            ctx.Recipes.Add(recipe2);

            ctx.RecipeProducts.Add(recipe1Product1);
            ctx.RecipeProducts.Add(recipe1Product2);
            ctx.RecipeProducts.Add(recipe1Product3);
            ctx.RecipeProducts.Add(recipe2Product4);
            ctx.RecipeProducts.Add(recipe2Product5);
            ctx.RecipeProducts.Add(recipe2Product6);

            ctx.Users.Add(user1);
            ctx.Users.Add(user2);

            ctx.Fridges.Add(fridge1);
            ctx.Fridges.Add(fridge2);

            ctx.SaveChanges();

            return(ctx);
        }
        /// <summary>
        /// Aktualizuje przepis
        /// </summary>
        /// <param name="command">Obiekt edycji przepisu</param>
        /// <returns>Zakutalizowany przepis</returns>
        public async Task <Recipe> UpdateRecipeAsync(UpdateCommand command)
        {
            var current = await _dbset
                          .Include(x => x.Images)
                          .Include(x => x.Products)
                          .Include("Products.Product")
                          .Include(x => x.Tags)
                          .FirstOrDefaultAsync(x => x.Id == command.Id);

            if (current == null)
            {
                throw new NullReferenceException("Przepis nie istnieje!");
            }

            // produkty
            foreach (var updatedProduct in command.Products)
            {
                var dbRecipeProduct = await _db.RecipeProducts.FirstOrDefaultAsync(x => x.Product.Name == updatedProduct.Name);

                if (dbRecipeProduct != null)
                {
                    int productId = dbRecipeProduct.ProductId;
                    var dbProduct = await _db.Products.FindAsync(dbRecipeProduct.ProductId);

                    if (dbProduct.Name != updatedProduct.Name)
                    {
                        var properProduct = await _productsService.GetOrCreateProductByNameAsync(updatedProduct.Name);

                        productId = properProduct.Id;
                    }

                    _db.Entry(dbRecipeProduct).CurrentValues.SetValues(new RecipeProduct
                    {
                        Id           = dbRecipeProduct.Id,
                        NumberOfUnit = updatedProduct.Amount,
                        ProductId    = productId,
                        RecipeId     = dbRecipeProduct.RecipeId,
                        UnitId       = updatedProduct.Unit
                    });
                }
                else
                {
                    var properProduct = await _productsService.GetOrCreateProductByNameAsync(updatedProduct.Name);

                    RecipeProduct recipeProduct = new RecipeProduct
                    {
                        NumberOfUnit = updatedProduct.Amount,
                        ProductId    = properProduct.Id,
                        UnitId       = updatedProduct.Unit,
                        Product      = new Models.Product()
                        {
                            Name = updatedProduct.Name
                        }
                    };
                    current.Products.Add(recipeProduct);
                }
            }

            foreach (var product in current.Products.ToList())
            {
                var detachedProduct = command.Products.FirstOrDefault(x => x.Name == product.Product.Name);
                if (detachedProduct == null && product.Id > 0)
                {
                    current.Products.Remove(product);
                    _db.RecipeProducts.Remove(product);
                }
            }


            // obrazki
            foreach (var updatedImage in command.Images)
            {
                var dbRecipeImage = await _db.RecipeImages.FindAsync(updatedImage.Id);

                if (dbRecipeImage != null)
                {
                    continue;
                }

                current.Images.Add(new RecipeImage
                {
                    Path     = updatedImage.RelativeUrl,
                    RecipeId = command.Id
                });
            }


            foreach (var image in current.Images.ToList())
            {
                var detachedImage = command.Images.Find(x => x.Id == image.Id);
                if (detachedImage == null && image.Id > 0)
                {
                    current.Images.Remove(image);
                    _db.RecipeImages.Remove(image);
                }
            }

            //tagi
            // todo: tagi jak produkty

            var tagsToRemove = new List <RecipeTag>();

            foreach (var tag in current.Tags)
            {
                if (command.Tags.All(x => x.ToLower() != tag.Name.ToLower()))
                {
                    tagsToRemove.Add(tag);
                }
            }

            foreach (var tag in tagsToRemove)
            {
                current.Tags.Remove(tag);
            }


            foreach (var tag in command.Tags)
            {
                bool exists = _db.RecipeTags.Any(x => x.Name.ToLower() == tag.ToLower());
                if (!exists || current.Tags.All(x => x.Name.ToLower() != tag.ToLower()))
                {
                    //var properTag = await _tagsService.GetOrCreateTagAsync(tag);

                    var properTag = await _db.RecipeTags
                                    .FirstOrDefaultAsync(x => x.Name.ToLower() == tag.ToLower());

                    if (properTag == null)
                    {
                        properTag = await _tagsService.CreateAsync(new RecipeTag()
                        {
                            Name = tag
                        });
                    }
                    current.Tags.Add(properTag);
                }
            }


            //current.Tags = command.Tags.Select(x => new RecipeTag
            //{
            //    Name = x
            //}).ToList();


            current.Name          = command.Title;
            current.Description   = command.Description;
            current.Difficulty    = command.Difficulty;
            current.TimeToPrepare = command.TimeToPrepare;
            current.EstimatedCost = command.EstimatedCost;
            current.PortionCount  = command.PortionCount;
            current.CategoryId    = command.Category;
            //current.Images = command.Images.Select(x => new RecipeImage()
            //{
            //    Path = x.Path
            //}).ToList();



            var updatedRecipe = await UpdateAsync(current);

            return(updatedRecipe);


            //List<RecipeProduct> recipeProducts = await _db.RecipeProducts.Where(x => x.RecipeId == command.Id).ToListAsync();

            //foreach (var product in command.Products)
            //{
            //    var properProduct = await _productsService.GetOrCreateProductByNameAsync(product.Name);

            //    RecipeProduct recipeProduct = new RecipeProduct();
            //    recipeProduct.NumberOfUnit = product.Amount;
            //    recipeProduct.ProductId = properProduct.Id;
            //    recipeProduct.UnitId = product.Unit;

            //    //recipeProducts.Add(recipeProduct);
            //}

            //current.Products = new List<RecipeProduct>();



            //foreach (var product in command.Products)
            //{
            //    if (product.Id != null)
            //    {
            //        var productInDb = await _db.RecipeProducts.FirstOrDefaultAsync(x => x.Id == product.Id);

            //        if (productInDb == null)
            //            continue;

            //        current.Products.Add(new RecipeProduct
            //        {
            //            Id = (int)product.Id,
            //            NumberOfUnit = product.Amount,
            //            ProductId = productInDb.ProductId,
            //            RecipeId = productInDb.RecipeId,
            //            UnitId = product.Unit
            //        });
            //    }
            //    else
            //    {
            //        var properProduct = await _productsService.GetOrCreateProductByNameAsync(product.Name);

            //        RecipeProduct recipeProduct = new RecipeProduct
            //        {
            //            NumberOfUnit = product.Amount,
            //            ProductId = properProduct.Id,
            //            UnitId = product.Unit
            //        };

            //        current.Products.Add(recipeProduct);
            //    }
            //}



            //return null;
        }
        /// <summary>
        /// Tworzy przepis
        /// </summary>
        /// <param name="command">Obiekt dodawania przepisu</param>
        /// <returns>Dodany przepis</returns>
        public async Task <Recipe> CreateRecipeAsync(CreateCommand command)
        {
            //var recipeProducts = command.products.Select(x => new RecipeProduct()
            //{
            //    UnitId = x.unit
            //});

            List <RecipeProduct> recipeProducts = new List <RecipeProduct>();

            foreach (var product in command.Products)
            {
                var properProduct = await _productsService.GetOrCreateProductByNameAsync(product.Name);

                RecipeProduct recipeProduct = new RecipeProduct();
                recipeProduct.NumberOfUnit = product.Amount;
                recipeProduct.ProductId    = properProduct.Id;
                recipeProduct.UnitId       = product.Unit;

                recipeProducts.Add(recipeProduct);
            }

            //List<RecipeTag> tags = new List<RecipeTag>();

            //foreach (var tag in command.Tags)
            //{
            //    var properTag = await _tagsService.GetOrCreateTagAsync(tag);
            //    tags.Add(properTag);
            //}



            string userId = ClaimsPrincipal.Current.Claims.FirstOrDefault(c => c.Type == "id")?.Value;

            if (String.IsNullOrEmpty(userId))
            {
                throw new ServiceException("Nieuatoryzowana próba dodania przepisu!");
            }

            var recipe = new Recipe()
            {
                Name          = command.Title,
                Description   = command.Description,
                Difficulty    = command.Difficulty,
                TimeToPrepare = command.TimeToPrepare,
                EstimatedCost = command.EstimatedCost,
                PortionCount  = command.PortionCount,
                Images        = command.Images.Select(x => new RecipeImage
                {
                    Path = x
                }).ToList(),
                Products    = recipeProducts,
                CreatedDate = DateTime.Now,
                AuthorId    = userId,
                CategoryId  = command.Category,
                Tags        = command.Tags.Select(x => new RecipeTag
                {
                    Name = x
                }).ToList()
            };



            return(await CreateAsync(recipe));
        }
Пример #11
0
        public async Task Create(ArborioModel arborioModel)
        {
            var recipe = new Recipe()
            {
                Name        = arborioModel.Name,
                ImageHref   = arborioModel.ImageHref,
                Description = arborioModel.Description,
            };
            var product = new Product();

            RecipeProduct recipeProduct;
            Product       productFind = null;
            var           result      = Database.RecipeManager.GetRecipeByName(arborioModel.Name);

            if (result.Count == 0)
            {
                try
                {
                    recipe = Database.RecipeManager.Create(recipe);
                }
                catch (DbEntityValidationException e)
                {
                    foreach (var eve in e.EntityValidationErrors)
                    {
                        Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                        eve.Entry.Entity.GetType().Name, eve.Entry.State);
                        foreach (var ve in eve.ValidationErrors)
                        {
                            Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                            ve.PropertyName, ve.ErrorMessage);
                        }
                    }
                    throw;
                }

                for (int i = 0; i < arborioModel.Products.Count; i++)
                {
                    productFind = Database.ProductManager.FindProductByName(arborioModel.Products[i]);
                    if (productFind != null)
                    {
                        recipeProduct = new RecipeProduct()
                        {
                            RecipeId  = recipe.RecipeId,
                            ProductId = productFind.ProductId,
                        };
                        var res       = Database.RecipeProductManager.GetRecipeIdByProductId(productFind.ProductId);
                        var recipeRes = res.Where(x => x.RecipeId == recipe.RecipeId);
                        if (recipeRes.Count() == 0)
                        {
                            recipeProduct.Number = arborioModel.Number[i];
                            Database.RecipeProductManager.Create(recipeProduct);
                        }
                        else
                        {
                            var ds = 5;
                        }
                    }
                    else
                    {
                        product.Name = arborioModel.Products[i];
                        Database.ProductManager.Create(product);

                        recipeProduct = new RecipeProduct()
                        {
                            RecipeId  = recipe.RecipeId,
                            ProductId = product.ProductId,
                        };
                        recipeProduct.Number = arborioModel.Number[i];
                        Database.RecipeProductManager.Create(recipeProduct);
                    }
                    await Database.SaveAsync();
                }
            }
        }
Пример #12
0
        private void InitFields()
        {
            user1 = new User
            {
                Id    = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD"),
                Name  = "TestUser1",
                Email = "*****@*****.**"
            };
            fridge1 = new Fridge
            {
                Id     = Guid.NewGuid(),
                UserId = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD")
            };

            user2 = new User
            {
                Id    = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD"),
                Name  = "TestUser2",
                Email = "*****@*****.**"
            };
            fridge2 = new Fridge
            {
                Id     = Guid.NewGuid(),
                UserId = Guid.Parse("B71DBC28-AAAA-47B1-8A2A-C6A5BFCD78AD")
            };

            productCategory1 = new ProductCategory
            {
                Id   = Guid.Parse("A5ADA8E6-04E1-49CA-A701-1265E216D69A"),
                Name = "Product Category 1"
            };
            productCategory2 = new ProductCategory
            {
                Id   = Guid.Parse("94CAE204-3337-43FA-8C0E-24C927BACAC4"),
                Name = "Product Category 2"
            };
            productCategory3 = new ProductCategory
            {
                Id   = Guid.NewGuid(),
                Name = "Product Category 3"
            };

            product1 = new BasicProduct
            {
                Id                = Guid.Parse("9C750539-3CA6-4239-941F-805B81C38CD4"),
                Name              = "Salmon",
                Description       = "",
                ProductCategoryId = productCategory1.Id,
                UserId            = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD")
            };
            product2 = new BasicProduct
            {
                Id                = Guid.Parse("9D8FAC6F-C194-44E5-A6D5-B6F6DBDEBBD0"),
                Name              = "Mustard",
                Description       = "",
                ProductCategoryId = productCategory1.Id,
                UserId            = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD")
            };
            product3 = new BasicProduct
            {
                Id                = Guid.Parse("F1F5610D-F065-46D8-9208-D7D1A0CB8C27"),
                Name              = "Brown Sugar",
                Description       = "",
                ProductCategoryId = productCategory1.Id,
                UserId            = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD")
            };

            product4 = new BasicProduct
            {
                Id                = Guid.Parse("06BBED64-A505-492C-889B-472F7DAB0FAA"),
                Name              = "Butter",
                Description       = "",
                ProductCategoryId = productCategory2.Id,
                UserId            = secondUserGuid
            };
            product5 = new BasicProduct
            {
                Id                = Guid.Parse("270B1180-45C6-4EE1-BC06-4819743859DC"),
                Name              = "Cream",
                Description       = "",
                ProductCategoryId = productCategory2.Id,
                UserId            = secondUserGuid
            };
            product6 = new BasicProduct
            {
                Id                = Guid.Parse("002D754C-176B-4324-A42E-2DC32369074D"),
                Name              = "Eggs",
                Description       = "",
                ProductCategoryId = productCategory2.Id,
                UserId            = secondUserGuid
            };

            newProduct1 = new NewProduct
            {
                Id                = Guid.NewGuid(),
                Name              = "New product 1",
                Description       = "",
                ProductCategoryId = productCategory1.Id,
                UserId            = firstUserGuid
            };
            newProduct2 = new NewProduct
            {
                Id                = Guid.NewGuid(),
                Name              = "New product 2",
                Description       = "",
                ProductCategoryId = productCategory2.Id,
                UserId            = secondUserGuid
            };
            newProduct3 = new NewProduct
            {
                Id                = Guid.NewGuid(),
                Name              = "New product 3",
                Description       = "",
                ProductCategoryId = productCategory2.Id,
                UserId            = secondUserGuid
            };

            recipe1 = new Recipe
            {
                Id          = Guid.Parse("83BD2A25-83EA-47D0-9B7B-0E4D528CF8C2"),
                Description = "",
                Title       = "Salmon recipe",
                UserId      = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD")
            };
            recipe2 = new Recipe
            {
                Id          = Guid.Parse("F795B317-DB3B-469F-9891-62C5CCC9DF5D"),
                Description = "",
                Title       = "Scrambled eggs",
                UserId      = Guid.Parse("B71DBC28-6A02-47B1-8A2A-C6A5BFCD78AD")
            };

            recipe1Product1 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = recipe1.Id,
                ProductId = product1.Id
            };
            recipe1Product2 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = recipe1.Id,
                ProductId = product2.Id
            };
            recipe1Product3 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = recipe1.Id,
                ProductId = product3.Id
            };
            recipe2Product4 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = recipe2.Id,
                ProductId = product4.Id
            };
            recipe2Product5 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = recipe2.Id,
                ProductId = product5.Id
            };
            recipe2Product6 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                RecipeId  = recipe2.Id,
                ProductId = product6.Id
            };

            fridge1Product1 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge1.Id,
                ProductId = product1.Id
            };
            fridge1Product2 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge1.Id,
                ProductId = product2.Id
            };
            fridge1Product3 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge1.Id,
                ProductId = product3.Id
            };
            fridge1Product4 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge1.Id,
                ProductId = product4.Id
            };
            fridge1Product5 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge1.Id,
                ProductId = product5.Id
            };
            fridge1Product6 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge1.Id,
                ProductId = product6.Id
            };

            tag1 = new Tag
            {
                Id         = Guid.NewGuid(),
                Text       = "tag1",
                RecipeTags = new List <RecipeTag>()
            };
            tag2 = new Tag
            {
                Id         = Guid.NewGuid(),
                Text       = "tag2",
                RecipeTags = new List <RecipeTag>()
            };
            tag3 = new Tag
            {
                Id         = Guid.NewGuid(),
                Text       = "tag3",
                RecipeTags = new List <RecipeTag>()
            };

            recipe1tag1 = new RecipeTag
            {
                Id       = Guid.NewGuid(),
                TagId    = tag1.Id,
                RecipeId = recipe1.Id
            };
            recipe1tag2 = new RecipeTag
            {
                Id       = Guid.NewGuid(),
                TagId    = tag1.Id,
                RecipeId = recipe1.Id
            };
            recipe2tag3 = new RecipeTag
            {
                Id       = Guid.NewGuid(),
                TagId    = tag1.Id,
                RecipeId = recipe1.Id
            };

            recipe1.RecipeProducts = new List <RecipeProduct>
            {
                recipe1Product1,
                recipe1Product2,
                recipe1Product3
            };
            recipe1.RecipeTags = new List <RecipeTag> {
                recipe1tag1,
                recipe1tag2
            };

            recipe2.RecipeProducts = new List <RecipeProduct>();
            recipe2.RecipeTags     = new List <RecipeTag>
            {
                recipe2tag3
            };

            tempUpdatingRecipe = new Recipe
            {
                RecipeProducts = new List <RecipeProduct>(),
                RecipeTags     = new List <RecipeTag> {
                }
            };

            recipesList = new List <Recipe>
            {
                recipe1,
                recipe2
            };

            productsList = new List <Product> {
                product1, product2, product3, product4, product5, product6
            };

            poorProductsRepositoryMock = new Mock <IProductsRepository>();
            poorProductsRepositoryMock.Setup(pr => pr.GetProductById(It.IsAny <Guid>()))
            .Returns <Guid>(g => productsList.Where(p => p.Id == g).FirstOrDefault());

            tagsList = new List <Tag> {
                tag1, tag2, tag3
            };

            poorTagsRepositoryMock = new Mock <ITagsRepository>();
            poorTagsRepositoryMock.Setup(tr => tr.GetTagById(It.IsAny <Guid>()))
            .Returns <Guid>(g => tagsList.Where(t => t.Id == g).FirstOrDefault());

            recipesRepositoryMock = new Mock <IRecipesRepository>();
            recipesRepositoryMock.Setup(rr => rr.RemoveRecipeProduct(It.IsAny <RecipeProduct>()))
            .Callback <RecipeProduct>(rp =>
            {
                tempUpdatingRecipe.RecipeProducts.Remove(rp);
            });
            recipesRepositoryMock.Setup(rr => rr.AddProduct(It.IsAny <RecipeProduct>()))
            .Callback <RecipeProduct>(rp =>
            {
                tempUpdatingRecipe.RecipeProducts.Add(rp);
            });
            recipesRepositoryMock.Setup(rr => rr.RemoveProductById(It.IsAny <Guid>()))
            .Callback <Guid>(g =>
            {
                tempUpdatingRecipe.RecipeProducts.Remove(
                    tempUpdatingRecipe.RecipeProducts.Where(r => r.ProductId == g).FirstOrDefault());
            });
            recipesRepositoryMock.Setup(rr => rr.RemoveRecipeTag(It.IsAny <RecipeTag>()))
            .Callback <RecipeTag>(rt =>
            {
                tempUpdatingRecipe.RecipeTags.Remove(rt);
            });
            recipesRepositoryMock.Setup(rr => rr.RemoveTagById(It.IsAny <Guid>()))
            .Callback <Guid>(g =>
            {
                tempUpdatingRecipe.RecipeTags.Remove(
                    tempUpdatingRecipe.RecipeTags.Where(rt => rt.TagId == g).FirstOrDefault());
            });
            recipesRepositoryMock.Setup(rr => rr.AddTag(It.IsAny <RecipeTag>()))
            .Callback <RecipeTag>(rt =>
            {
                tempUpdatingRecipe.RecipeTags.Add(rt);
            });
            recipesRepositoryMock.Setup(rr => rr.GetRecipeById(It.IsAny <Guid>()))
            .Returns <Guid>(recipeId =>
            {
                return(tempUpdatingRecipe);
            });
            recipesRepositoryMock.Setup(rr => rr.ContainsProduct(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .Returns <Guid, Guid>((recipeId, productId) =>
            {
                return(tempUpdatingRecipe.RecipeProducts.Any(rp => rp.ProductId == productId));
            });
            recipesRepositoryMock.Setup(rr => rr.ContainsTag(It.IsAny <Guid>(), It.IsAny <Guid>()))
            .Returns <Guid, Guid>((recipeId, tagId) =>
            {
                return(tempUpdatingRecipe.RecipeTags.Any(rt => rt.Id == tagId));
            });
        }
Пример #13
0
 public void Create(RecipeProduct item)
 {
     Database.RecipesProducts.Add(item);
     Database.SaveChanges();
 }
Пример #14
0
        private void setUpMocks()
        {
            fridgeProductForDbSetList = new List <FridgeProduct>();
            recipeProductForDbSetList = new List <RecipeProduct>();
            recipeForDbSetList        = new List <Recipe>();


            user = new User
            {
                Id      = Guid.NewGuid(),
                Fridges = new List <Fridge>()
            };

            recipe1 = new Recipe
            {
                Id             = Guid.NewGuid(),
                Title          = "Recipe1",
                UserId         = user.Id,
                Description    = "test",
                RecipeProducts = new List <RecipeProduct>()
            };
            recipe2 = new Recipe
            {
                Id             = Guid.NewGuid(),
                Title          = "Recipe2",
                UserId         = user.Id,
                Description    = "test",
                RecipeProducts = new List <RecipeProduct>()
            };
            recipe3 = new Recipe
            {
                Id             = Guid.NewGuid(),
                Title          = "Recipe3",
                UserId         = user.Id,
                Description    = "test",
                RecipeProducts = new List <RecipeProduct>()
            };
            recipe4 = new Recipe
            {
                Id             = Guid.NewGuid(),
                Title          = "Recipe4",
                UserId         = user.Id,
                Description    = "test",
                RecipeProducts = new List <RecipeProduct>()
            };

            productCategory = new ProductCategory
            {
                Id   = Guid.NewGuid(),
                Name = "ProductCategory1"
            };

            product1 = new Product
            {
                Id   = Guid.NewGuid(),
                Name = "Product1",
                ProductCategoryId = productCategory.Id,
                Description       = "test"
            };
            product2 = new Product
            {
                Id   = Guid.NewGuid(),
                Name = "Product2",
                ProductCategoryId = productCategory.Id,
                Description       = "test"
            };
            product3 = new Product
            {
                Id   = Guid.NewGuid(),
                Name = "Product3",
                ProductCategoryId = productCategory.Id,
                Description       = "test"
            };
            product4 = new Product
            {
                Id   = Guid.NewGuid(),
                Name = "Product4",
                ProductCategoryId = productCategory.Id,
                Description       = "test"
            };

            fridge = new Fridge
            {
                Id             = Guid.NewGuid(),
                UserId         = user.Id,
                FridgeProducts = new List <FridgeProduct>()
            };

            fridgeProduct1 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge.Id,
                ProductId = product1.Id
            };
            fridgeProduct2 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge.Id,
                ProductId = product2.Id
            };
            fridgeProduct3 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge.Id,
                ProductId = product3.Id
            };
            fridgeProduct4 = new FridgeProduct
            {
                Id        = Guid.NewGuid(),
                FridgeId  = fridge.Id,
                ProductId = product4.Id
            };

            recipe1Product1 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product1.Id,
                RecipeId  = recipe1.Id,
                Recipe    = recipe1,
                Product   = product1
            };
            recipe1Product2 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product2.Id,
                RecipeId  = recipe1.Id,
                Recipe    = recipe1,
                Product   = product2
            };
            recipe1Product3 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product3.Id,
                RecipeId  = recipe1.Id,
                Recipe    = recipe1,
                Product   = product3
            };
            recipe1Product4 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product4.Id,
                RecipeId  = recipe1.Id,
                Recipe    = recipe1,
                Product   = product4
            };

            recipe2Product1 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product1.Id,
                RecipeId  = recipe2.Id,
                Recipe    = recipe2,
                Product   = product1
            };
            recipe2Product2 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product2.Id,
                RecipeId  = recipe2.Id,
                Recipe    = recipe2,
                Product   = product2
            };
            recipe2Product3 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product3.Id,
                RecipeId  = recipe2.Id,
                Recipe    = recipe2,
                Product   = product3
            };
            recipe2Product4 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product4.Id,
                RecipeId  = recipe2.Id,
                Recipe    = recipe2,
                Product   = product4
            };

            recipe3Product1 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product1.Id,
                RecipeId  = recipe3.Id,
                Recipe    = recipe3,
                Product   = product1
            };
            recipe3Product2 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product2.Id,
                RecipeId  = recipe3.Id,
                Recipe    = recipe3,
                Product   = product2
            };
            recipe3Product3 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product3.Id,
                RecipeId  = recipe3.Id,
                Recipe    = recipe3,
                Product   = product3
            };
            recipe3Product4 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product4.Id,
                RecipeId  = recipe3.Id,
                Recipe    = recipe3,
                Product   = product4
            };

            recipe4Product1 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product1.Id,
                RecipeId  = recipe4.Id,
                Recipe    = recipe4,
                Product   = product1
            };
            recipe4Product2 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product2.Id,
                RecipeId  = recipe4.Id,
                Recipe    = recipe4,
                Product   = product2
            };
            recipe4Product3 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product3.Id,
                RecipeId  = recipe4.Id,
                Recipe    = recipe4,
                Product   = product3
            };
            recipe4Product4 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product4.Id,
                RecipeId  = recipe4.Id,
                Recipe    = recipe4,
                Product   = product4
            };
        }
Пример #15
0
 public void AddProduct(RecipeProduct recipeProduct)
 {
     ctx.RecipeProducts.Add(recipeProduct);
     Save();
 }
Пример #16
0
 public void RemoveRecipeProduct(RecipeProduct recipeProduct)
 {
     ctx.RecipeProducts.Remove(recipeProduct);
     Save();
 }
Пример #17
0
        private void setUpMocks()
        {
            repoSaved = false;

            recipesRepositoryMock  = new Mock <IRecipesRepository>();
            productsRepositoryMock = new Mock <IProductsRepository>();
            usersRepositoryMock    = new Mock <IUsersRepository>();
            eventAggregatorMock    = new Mock <IEventAggregator>();

            var productAddedEventMock = new Mock <NewProductAddedEvent>();

            eventAggregatorMock.Setup(e => e.GetEvent <NewProductAddedEvent>()).
            Returns(productAddedEventMock.Object);
            var recipeAddedEventMock = new Mock <NewRecipeAddedEvent>();

            eventAggregatorMock.Setup(e => e.GetEvent <NewRecipeAddedEvent>()).
            Returns(recipeAddedEventMock.Object);
            var recipeRemovedEventMock = new Mock <RecipeRemovedEvent>();

            eventAggregatorMock.Setup(e => e.GetEvent <RecipeRemovedEvent>()).
            Returns(recipeRemovedEventMock.Object);
            var recipeUpdatedEventMock = new Mock <RecipeUpdatedEvent>();

            eventAggregatorMock.Setup(e => e.GetEvent <RecipeUpdatedEvent>()).
            Returns(recipeUpdatedEventMock.Object);

            recipeProductList = new List <RecipeProduct>();

            product1 = new Product {
                Id = Guid.NewGuid(), Name = "Product1"
            };
            product2 = new Product {
                Id = Guid.NewGuid(), Name = "Product2"
            };
            productList = new List <Product> {
                product1, product2
            };

            productsRepositoryMock.Setup(pr => pr.GetAllProducts()).
            Returns(productList);

            User user = new User {
                Id = Guid.NewGuid()
            };

            usersRepositoryMock.Setup(ur => ur.GetFirstUser())
            .Returns(user);

            recipe1 = new Recipe
            {
                Id     = Guid.NewGuid(),
                Title  = "Recipe1",
                UserId = user.Id
            };
            recipe2 = new Recipe {
                Id     = Guid.NewGuid(),
                Title  = "Recipe2",
                UserId = user.Id
            };

            recipeList = new List <Recipe> {
                recipe1, recipe2
            };

            recipesRepositoryMock.Setup(rr => rr.GetAllRecipes())
            .Returns(recipeList);

            recipesRepositoryMock.Setup(rr => rr.Save())
            .Callback(() => repoSaved = true);

            RecipeProduct recipeProduct1 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product1.Id,
                RecipeId  = recipe1.Id
            };
            RecipeProduct recipeProduct2 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product2.Id,
                RecipeId  = recipe1.Id
            };
            RecipeProduct recipeProduct3 = new RecipeProduct
            {
                Id        = Guid.NewGuid(),
                ProductId = product1.Id,
                RecipeId  = recipe2.Id
            };

            rpl1 = new RecipeProductLookup
            {
                ProductId       = product1.Id,
                RecipeProductId = recipeProduct1.Id,
                ProductName     = product1.Name
            };
            rpl2 = new RecipeProductLookup
            {
                ProductId       = product2.Id,
                RecipeProductId = recipeProduct2.Id,
                ProductName     = product2.Name
            };
            rpl3 = new RecipeProductLookup
            {
                ProductId       = product1.Id,
                RecipeProductId = recipeProduct3.Id,
                ProductName     = product1.Name
            };


            recipe1.RecipeProducts = new List <RecipeProduct> {
                recipeProduct1, recipeProduct2
            };
            recipe2.RecipeProducts = new List <RecipeProduct> {
                recipeProduct3
            };

            recipesRepositoryMock.Setup(rr => rr.AddRecipe(
                                            It.IsAny <Recipe>()))
            .Callback <Recipe>(r => recipeList.Add(r));
            recipesRepositoryMock.Setup(rr => rr.RemoveRecipe(
                                            It.IsAny <Recipe>()))
            .Callback <Recipe>(r => recipeList.Remove(r));

            recipesRepositoryMock.Setup(rr => rr.GetProductsForRecipe(
                                            It.Is <Guid>(g => g.Equals(product1.Id))))
            .Returns(new List <Product>
            {
                product1, product2
            });
            recipesRepositoryMock.Setup(rr => rr.GetRecipeProductLookupsForRecipe(
                                            It.Is <Guid>(g => g.Equals(recipe1.Id))))
            .Returns(new List <RecipeProductLookup> {
                rpl1, rpl2
            });

            recipesRepositoryMock.Setup(rr => rr.GetProductsForRecipe(
                                            It.Is <Guid>(g => g.Equals(product2.Id))))
            .Returns(new List <Product> {
                product1
            });

            recipesRepositoryMock.Setup(rr => rr.GetRecipeProductLookupsForRecipe(
                                            It.Is <Guid>(g => g.Equals(recipe2.Id))))
            .Returns(new List <RecipeProductLookup> {
                rpl3
            });

            recipesRepositoryMock.Setup(rr => rr.GetProductsForRecipe(
                                            It.Is <Guid>(g => !g.Equals(recipe1.Id) && !g.Equals(recipe2.Id))))
            .Returns(new List <Product>());
            recipesRepositoryMock.Setup(rr => rr.GetRecipeProductLookupsForRecipe(
                                            It.Is <Guid>(g => !g.Equals(recipe1.Id) && !g.Equals(recipe2.Id))))
            .Returns(new List <RecipeProductLookup>());

            recipesRepositoryMock.Setup(rr => rr.AddRecipeProduct(
                                            It.IsAny <RecipeProduct>()))
            .Callback <RecipeProduct>(rp => recipeProductList.Add(rp));



            recipesViewModel = new RecipesViewModel(
                recipesRepositoryMock.Object,
                productsRepositoryMock.Object,
                usersRepositoryMock.Object,
                eventAggregatorMock.Object);
        }