예제 #1
0
        public ProductOption()
        {
            var volumeOption = new VolumeOption();

            volumeOption.PropertyChanged += (o, e) => OnPropertyChanged(() => Value);
            value = volumeOption;
        }
        public RedirectToRouteResult RemoveFromCart(Cart cart, int cofeeId, int volumeOptionId, bool isSugarOption, bool isMilkOption = false)
        {
            Cofee cofee = repository.Products
                          .FirstOrDefault(p => p.CofeeId == cofeeId);
            VolumeOption volumeOption = cofee?.VolumeOptions
                                        .FirstOrDefault(vo => vo.VolumeOptionId == volumeOptionId);

            if ((cofee != null) && (volumeOption != null))
            {
                int?sugarOptionId = isSugarOption ? cofee.SugarOptions.FirstOrDefault()?.SugarOptionId : null;

                int?         milkoptionId = isMilkOption ? cofee.MilkOptions.FirstOrDefault()?.MilkOptionId : null;
                CofeeOptions cofeeOptions = new CofeeOptions {
                    MilkOptionId = milkoptionId, SugarOptionId = sugarOptionId
                };
                Recipe recipe = new Recipe
                {
                    CofeeId    = cofee.CofeeId,
                    VolumeSize = volumeOption.Size,
                    IsCupCap   = volumeOption.IsCupCap,
                    Options    = cofeeOptions
                };

                cart.RemoveLine(recipe);
            }

            return(RedirectToAction("Index"));
        }
예제 #3
0
        public void Can_Delete_Valid_Products()
        {
            // Arrange - create a Product
            Cofee cofee = new Cofee {
                CofeeId = 2, Name = "Test"
            };
            // Arrange - create the mock repository
            // Arrange - create the mock repository
            Mock <ICofeeRepository> mock = new Mock <ICofeeRepository>();
            var volumeOptions            = new VolumeOption[] { new VolumeOption {
                                                                    VolumeOptionId = 1,
                                                                    Size           = 0.133M,
                                                                    Unit           = new Unit {
                                                                        Name = "Litre"
                                                                    }
                                                                } };
            var sugarOptions = new SugarOption[] { new SugarOption {
                                                       SugarOptionId = 1, Size = 1, Price = 0, Unit = new Unit {
                                                           Name = "Teaspoon"
                                                       }
                                                   } };
            var espresso = new Cofee
            {
                CofeeId       = 1,
                Name          = "Espresso",
                PriceCoeff    = 2.0M,
                VolumeOptions = volumeOptions,
                SugarOptions  = sugarOptions
            };

            var americano = new Cofee
            {
                CofeeId       = 2,
                Name          = "Americano",
                PriceCoeff    = 3.0M,
                VolumeOptions = volumeOptions,
                SugarOptions  = sugarOptions
            };

            mock.Setup(m => m.Products).Returns(new Cofee[] {
                espresso, americano
            }.AsQueryable());
            // Arrange - create the controller
            AdminController target = new AdminController(mock.Object);

            // Act - delete the product
            target.Delete(cofee.CofeeId);
            // Assert - ensure that the repository delete method was
            // called with the correct Product
            mock.Verify(m => m.DeleteProduct(cofee.CofeeId));
        }
예제 #4
0
        public void Can_Edit_Product()
        {
            // Arrange - create the mock repository
            Mock <ICofeeRepository> mock = new Mock <ICofeeRepository>();
            var volumeOptions            = new VolumeOption[] { new VolumeOption {
                                                                    VolumeOptionId = 1,
                                                                    Size           = 0.133M,
                                                                    Unit           = new Unit {
                                                                        Name = "Litre"
                                                                    }
                                                                } };
            var sugarOptions = new SugarOption[] { new SugarOption {
                                                       SugarOptionId = 1, Size = 1, Price = 0, Unit = new Unit {
                                                           Name = "Teaspoon"
                                                       }
                                                   } };
            var espresso = new Cofee
            {
                CofeeId       = 1,
                Name          = "Espresso",
                PriceCoeff    = 2.0M,
                VolumeOptions = volumeOptions,
                SugarOptions  = sugarOptions
            };

            var americano = new Cofee
            {
                CofeeId       = 2,
                Name          = "Americano",
                PriceCoeff    = 3.0M,
                VolumeOptions = volumeOptions,
                SugarOptions  = sugarOptions
            };

            mock.Setup(m => m.Products).Returns(new Cofee[] {
                espresso, americano
            }.AsQueryable());

            // Arrange - create the controller
            AdminController target = new AdminController(mock.Object);
            // Act
            Cofee c1 = target.Edit(1).ViewData.Model as Cofee;
            Cofee c2 = target.Edit(2).ViewData.Model as Cofee;

            // Assert
            Assert.AreEqual(1, c1.CofeeId);
            Assert.AreEqual(2, c2.CofeeId);
        }
        public RedirectToRouteResult AddToCart(Cart cart, int quantity, int cofeeId, int volumeOptionId, bool isSugarOption, bool isMilkOption = false)
        {
            Cofee cofee = repository.Products
                          .FirstOrDefault(c => c.CofeeId == cofeeId);
            VolumeOption volumeOption = cofee?.VolumeOptions
                                        .FirstOrDefault(vo => vo.VolumeOptionId == volumeOptionId);

            if ((cofee != null) && (volumeOption != null))
            {
                Recipe recipe = new Recipe();
                recipe.CofeeId        = cofee.CofeeId;
                recipe.CofeeName      = cofee.Name;
                recipe.CofeePriceCoef = cofee.PriceCoeff;

                recipe.VolumeOptionId = volumeOption.VolumeOptionId;
                recipe.VolumeSize     = volumeOption.Size;
                recipe.IsCupCap       = volumeOption.IsCupCap;

                recipe.Options = new CofeeOptions();

                if (isSugarOption)
                {
                    SugarOption sugarOption = cofee.SugarOptions.FirstOrDefault();
                    if (sugarOption != null)
                    {
                        recipe.Options.SugarOptionId = sugarOption.SugarOptionId;
                        recipe.Options.SugarPrice    = sugarOption.Price;
                        recipe.Options.SugarSize     = sugarOption.Size;
                        recipe.Options.SugarUnitName = sugarOption.Unit.Name;
                    }
                }

                if (isMilkOption)
                {
                    MilkOption milkOption = cofee.MilkOptions.FirstOrDefault();
                    if (milkOption != null)
                    {
                        recipe.Options.MilkOptionId = milkOption.MilkOptionId;
                        recipe.Options.MilkPrice    = milkOption.Price;
                        recipe.Options.MilkSize     = milkOption.Size;
                        recipe.Options.MilkUnitName = milkOption.Unit.Name;
                    }
                }
                cart.AddItem(recipe, quantity);
            }
            return(RedirectToAction("Index"));
        }
예제 #6
0
        public void Cannot_Edit_Nonexistent_Product()
        {
            // Arrange - create the mock repository
            Mock <ICofeeRepository> mock = new Mock <ICofeeRepository>();
            var volumeOptions            = new VolumeOption[] { new VolumeOption {
                                                                    VolumeOptionId = 1,
                                                                    Size           = 0.133M,
                                                                    Unit           = new Unit {
                                                                        Name = "Litre"
                                                                    }
                                                                } };
            var sugarOptions = new SugarOption[] { new SugarOption {
                                                       SugarOptionId = 1, Size = 1, Price = 0, Unit = new Unit {
                                                           Name = "Teaspoon"
                                                       }
                                                   } };
            var espresso = new Cofee
            {
                CofeeId       = 1,
                Name          = "Espresso",
                PriceCoeff    = 2.0M,
                VolumeOptions = volumeOptions,
                SugarOptions  = sugarOptions
            };

            var americano = new Cofee
            {
                CofeeId       = 2,
                Name          = "Americano",
                PriceCoeff    = 3.0M,
                VolumeOptions = volumeOptions,
                SugarOptions  = sugarOptions
            };

            mock.Setup(m => m.Products).Returns(new Cofee[] {
                espresso, americano
            }.AsQueryable());
            // Arrange - create the controller
            AdminController target = new AdminController(mock.Object);
            // Act
            Cofee result = (Cofee)target.Edit(4).ViewData.Model;

            // Assert
            Assert.IsNull(result);
        }