public void UpdateSugarOption(SugarOption sugarOption)
        {
            var oSugarOption = _appDbContext.SugarOptions.FirstOrDefault(so => so.Id == sugarOption.Id);

            if (oSugarOption == null)
            {
                _appDbContext.SugarOptions.Add(sugarOption);
            }
            else
            {
                _appDbContext.Entry(oSugarOption).State = EntityState.Detached;
                _appDbContext.Entry(sugarOption).State  = EntityState.Modified;
                _appDbContext.Update(sugarOption);
            };

            if (sugarOption.IsPrimary)
            {
                var sugars = _appDbContext.SugarOptions.Where(s => s.Id != sugarOption.Id && s.DrinkId == sugarOption.DrinkId);
                foreach (var sugar in sugars)
                {
                    sugar.IsPrimary = false;
                }
            }

            _appDbContext.SaveChanges();
        }
        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));
        }
        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 async Task <IActionResult> UpdateSugarOption(SugarOption sugarOption)
        {
            try
            {
                _drinkRepository.UpdateSugarOption(sugarOption);
            }
            catch (DbUpdateConcurrencyException)
            {
                return(Json(new { success = false }));
            }

            var drink = _drinkRepository.GetDrinkById(sugarOption.DrinkId);
            var table = await this.RenderViewAsync("_ListSugarOption", drink.SugarOptions, true);

            return(Json(new { success = true, html = table }));
        }
        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"));
        }
        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);
        }
        public IActionResult UpdateSugarOption(int?id, int drinkId)
        {
            var opt = new SugarOption();

            if (id == null)
            {
                opt.DrinkId  = drinkId;
                opt.IsActive = true;
            }
            else
            {
                opt = _drinkRepository.GetSugarById(id.Value);
                if (opt == null)
                {
                    return(NotFound());
                }
            }

            ViewBag.Units = _drinkRepository.DrinkUnits;
            return(PartialView(opt));
        }
示例#8
0
        public void ProcessSeed(LittleCofeeShopDbContext context)
        {
            var notDefUnit = new Unit {
                Name = "Not defined", ShortName = "N/A"
            };
            Unit litreUnit = new Unit {
                Name = "Litre", ShortName = "L"
            };
            Unit teaspoonUnit = new Unit {
                Name = "Teaspoon", ShortName = "t"
            };
            Unit pieceUnit = new Unit {
                Name = "Piece", ShortName = "pcs"
            };

            context.Units.AddOrUpdate(notDefUnit);
            context.Units.AddOrUpdate(litreUnit);
            context.Units.AddOrUpdate(teaspoonUnit);
            context.Units.AddOrUpdate(pieceUnit);
            //context.Units.AddRange(units);

            IList <VolumeOption> defaultVolumeOptions = new List <VolumeOption>();

            defaultVolumeOptions.Add(new VolumeOption {
                Unit = litreUnit, Size = 0.133M, Name = "Small", IsCupCap = true
            });
            defaultVolumeOptions.Add(new VolumeOption {
                Unit = litreUnit, Size = 0.250M, Name = "Middle", IsCupCap = true
            });
            IList <VolumeOption> latteVolumeOptions = new List <VolumeOption>(defaultVolumeOptions);

            latteVolumeOptions.Add(new VolumeOption {
                Unit = litreUnit, Size = 0.500M, Name = "Large", IsCupCap = true
            });
            context.VolumeOptionRecords.AddRange(latteVolumeOptions);

            IList <MilkOption> milkOptions = new List <MilkOption>();

            milkOptions.Add(new MilkOption {
                Unit = notDefUnit, Price = 0.1M, Size = 1.0M
            });
            context.MilkOptionRecords.AddRange(milkOptions);


            SugarOption sugarOption = new SugarOption {
                Unit = teaspoonUnit, Price = 0.1M, Size = 1.0M
            };

            context.SugarOptionRecords.AddOrUpdate(sugarOption);

            IList <SugarOption> sugarOptions = new List <SugarOption>();

            sugarOptions.Add(sugarOption);

            Cofee espressoCofee = new Cofee
            {
                Name          = "Espresso",
                Description   = @"Espresso is coffee of Italian origin, brewed by expressing or forcing a small amount of nearly boiling water under
                        pressure through finely ground coffee beans.Espresso is generally thicker than coffee brewed by other methods,
                        has a higher concentration of suspended and dissolved solids, and has crema on top(a foam with a creamy consistency).",
                VolumeOptions = new List <VolumeOption>(defaultVolumeOptions),
                SugarOptions  = new List <SugarOption>(sugarOptions),
                PriceCoeff    = 3.0M,
                ImagePath     = "~/Static/Espresso.jpg"
            };

            context.CofeeRecords.AddOrUpdate(espressoCofee);

            Cofee latteCofee = new Cofee
            {
                Name          = "Latte",
                Description   = @"A latte is a coffee drink made with espresso and steamed milk. A cafe latte consists of 2 fluid ounces of espresso,
                                3 ounces of steamed milk, and typically a thin layer of foam on top.It can sometimes be referred to as a “Wet Cappuccino”.",
                MilkOptions   = new List <MilkOption>(milkOptions),
                VolumeOptions = new List <VolumeOption>(latteVolumeOptions),
                SugarOptions  = new List <SugarOption>(sugarOptions),
                PriceCoeff    = 4.0M,
                ImagePath     = "~/Static/Latte.jpg"
            };

            context.CofeeRecords.AddOrUpdate(latteCofee);

            Cofee americanoCofee = new Cofee
            {
                Name          = "Americano",
                Description   = @"Caffè Americano, or Americano (Italian: American coffee) is a style of coffee prepared by adding hot water to espresso,
                                giving a similar strength but different flavor from regular drip coffee. The strength of an Americano varies with the number
                                of shots of espresso and the amount of water added.",
                VolumeOptions = new List <VolumeOption>(defaultVolumeOptions),
                SugarOptions  = new List <SugarOption>(sugarOptions),
                PriceCoeff    = 3.0M,
                ImagePath     = "~/Static/Americano.jpg"
            };

            context.CofeeRecords.AddOrUpdate(americanoCofee);

            base.Seed(context);
        }