예제 #1
0
        public void Seed()
        {
            _ctx.Database.EnsureCreated();

            if (!_ctx.Beverages.Any())
            {
                // Beverage categories
                BeverageCategory coffee = new BeverageCategory()
                {
                    Name = "Coffee"
                };
                _ctx.BeverageCategories.Add(coffee);

                // Beverage categories
                BeverageCategory tea = new BeverageCategory()
                {
                    Name = "Tea"
                };
                _ctx.BeverageCategories.Add(tea);

                // Beverages
                Beverage houseBlend = new Beverage()
                {
                    Description         = "House Blend",
                    DetailedDescription = "Aroma, body and flavor all in balance—with tastes of nuts and cocoa brought out by the roast.",
                    Price    = 45m,
                    Category = coffee
                };
                _ctx.Beverages.Add(houseBlend);

                Beverage darkRoast = new Beverage()
                {
                    Description         = "Dark Roast",
                    DetailedDescription = "Roasted to emphasize the smoky, toasty flavors of the roast itself.",
                    Price    = 40m,
                    Category = coffee
                };
                _ctx.Beverages.Add(darkRoast);

                Beverage decaf = new Beverage()
                {
                    Description         = "Decaf",
                    DetailedDescription = "For the ones who want to cut down on the caffeine but not the enjoyment of coffee.",
                    Price    = 35m,
                    Category = coffee
                };
                _ctx.Beverages.Add(decaf);

                Beverage expresso = new Beverage()
                {
                    Description         = "Expresso",
                    DetailedDescription = "This dark-roasted blend produces a smoky aroma and tastes great.",
                    Price    = 40m,
                    Category = coffee
                };
                _ctx.Beverages.Add(expresso);

                Beverage chai = new Beverage()
                {
                    Description         = "Tiger Spice Chai",
                    DetailedDescription = "David Rio's signature and award-winning Chai recipe based on black tea.",
                    Price    = 40m,
                    Category = tea
                };
                _ctx.Beverages.Add(chai);

                Beverage earlGrey = new Beverage()
                {
                    Description         = "Earl Grey",
                    DetailedDescription = "Classic Earl Grey. A delicate tea with a delicious twist of citrusy bergamot.",
                    Price    = 25m,
                    Category = tea
                };
                _ctx.Beverages.Add(earlGrey);


                // Condiments
                Condiment milk = new Condiment()
                {
                    Description         = "Milk",
                    DetailedDescription = "Steamed",
                    Price = 5m
                };
                _ctx.Condiments.Add(milk);

                Condiment soy = new Condiment()
                {
                    Description         = "Soy",
                    DetailedDescription = "Steamed",
                    Price = 7m
                };
                _ctx.Condiments.Add(soy);

                Condiment mocha = new Condiment()
                {
                    Description         = "Mocha",
                    DetailedDescription = "Bittersweet mocha sauce",
                    Price = 15m
                };
                _ctx.Condiments.Add(mocha);

                Condiment whip = new Condiment()
                {
                    Description         = "Whip",
                    DetailedDescription = "Organic cream",
                    Price = 5m
                };
                _ctx.Condiments.Add(whip);

                Condiment vanilla = new Condiment()
                {
                    Description         = "Vanilla syrup",
                    DetailedDescription = "Sweet touch of vanilla flavor",
                    Price = 5m
                };
                _ctx.Condiments.Add(vanilla);

                Condiment caramel = new Condiment()
                {
                    Description         = "Caramel syrup",
                    DetailedDescription = "Sweet touch of caramel flavor",
                    Price = 5m
                };
                _ctx.Condiments.Add(caramel);

                Condiment hazelnut = new Condiment()
                {
                    Description         = "Hazelnut syrup",
                    DetailedDescription = "Sweet touch of hazelnut flavor",
                    Price = 5m
                };
                _ctx.Condiments.Add(hazelnut);


                // Sales
                Sale houseSale = new Sale()
                {
                    Name        = "Sale on House Blend",
                    Description = "Try our amazing house blend with a 20 percent discount this week.",
                    Percent     = 20,
                    Type        = ESaleType.On_Beverage
                };
                _ctx.Sales.Add(houseSale);

                Sale chaiSale = new Sale()
                {
                    Name        = "Sale on Tiger Spice Chai",
                    Description = "Try David Rio's signature and award-winning Tiger Spice Chai with a 20 percent discount on the total product this week.",
                    Percent     = 20,
                    Type        = ESaleType.On_Decorated_Beverage
                };
                _ctx.Sales.Add(chaiSale);


                // Beverage sales
                BeverageSale houseBeveragesale = new BeverageSale()
                {
                    BeverageId = houseBlend.Id,
                    SaleId     = houseSale.Id,
                    DateFrom   = DateTime.Now,
                    DateTo     = DateTime.Now.AddDays(7)
                };
                _ctx.BeverageSales.Add(houseBeveragesale);

                BeverageSale chaiBeveragesale = new BeverageSale()
                {
                    BeverageId = chai.Id,
                    SaleId     = chaiSale.Id,
                    DateFrom   = DateTime.Now,
                    DateTo     = DateTime.Now.AddDays(7)
                };
                _ctx.BeverageSales.Add(chaiBeveragesale);


                // Orders
                Order coffeeOrder = new Order()
                {
                    OrderDate = DateTime.Now,
                    Completed = true
                };
                _ctx.Orders.Add(coffeeOrder);

                Order latteOrder = new Order()
                {
                    OrderDate = DateTime.Now.AddDays(-2),
                    Completed = true
                };
                _ctx.Orders.Add(latteOrder);


                // Order items
                OrderItem coffeeItem = new OrderItem()
                {
                    Beverage    = darkRoast,
                    Description = darkRoast.Description,
                    Quantity    = 2,
                    UnitPrice   = darkRoast.Price,
                    Order       = coffeeOrder
                };
                _ctx.OrderItems.Add(coffeeItem);

                OrderItem latteItem = new OrderItem()
                {
                    Beverage    = houseBlend,
                    Description = $"{darkRoast.Description} with {milk.Description}",
                    Quantity    = 1,
                    UnitPrice   = (houseBlend.Price + milk.Price),
                    Order       = latteOrder
                };
                _ctx.OrderItems.Add(latteItem);

                _ctx.SaveChanges();
            }
        }
예제 #2
0
        public async Task <int> SaveBeverageAsync(List <Rms.Classes.MenuItem> MenuItems)
        {
            using (var transaction = Context.Database.BeginTransaction())
            {
                try
                {
                    var catlist = await Context.BeverageCategories.ToListAsync();

                    var items = await Context.Beverages.ToListAsync();

                    foreach (var bevcat in MenuItems.GroupBy(a => a.MenuCategoryName))
                    {
                        var cat = bevcat.Key;

                        BeverageCategory flag = catlist.Where(a => a.BeverageCategoryName == cat).FirstOrDefault();

                        if (flag == null)
                        {
                            flag = new BeverageCategory();
                            flag.BeverageCategoryName = cat;
                            Context.BeverageCategories.Add(flag);
                        }
                        else
                        {
                            Context.Entry(flag).State = EntityState.Modified;
                        }
                        await Context.SaveChangesAsync();

                        foreach (var i in bevcat)
                        {
                            var      beverage = i.beverage;
                            var      price    = i.Price;
                            Beverage flag1    = items.Where(a => a.Name.Trim().ToUpper() == beverage.Trim().ToUpper()).FirstOrDefault();
                            if (flag1 == null)
                            {
                                flag1             = new Beverage();
                                flag1.Name        = beverage;
                                flag1.Price       = price;
                                flag1.IsPopular   = i.IsPopular;
                                flag1.ShowInChart = i.ShowInChart;
                                int volume = 0;
                                int.TryParse(i.Volume, out volume);
                                flag1.Volume             = volume;
                                flag1.Image              = i.Image;
                                flag1.DrinkType          = Drinktype.Whisky;
                                flag1.BeverageCategoryId = flag.BeverageCategoryId;
                                Context.Beverages.Add(flag1);
                            }
                            else
                            {
                                Context.Entry(flag1).State = EntityState.Modified;
                            }
                            await Context.SaveChangesAsync();
                        }
                    }
                    transaction.Commit();
                    return(0);
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw ex;
                }
            }
        }