예제 #1
0
        public static void AddDrink(this ApplicationContext context, Drink drink, int quantity, string userId)
        {
            ShoppingBasket basket   = new ShoppingBasket();
            PurchasedGood  purchase = new PurchasedGood();

            if (context.ShoppingBaskets.Count() != 0)
            {
                List <ShoppingBasket> basketList = context.ShoppingBaskets.ToList();
                basket   = basketList.Where(x => x.UserId == userId).FirstOrDefault();
                purchase = basket != null?context.PurchasedGoods.ToList().
                           Where(x => x.ShoppingBasketViewModelId == basket.Id & x.DrinkId == drink.Id).FirstOrDefault() : null;
            }

            if (basket == null)
            {
                basket = new ShoppingBasket()
                {
                    UserId = userId
                };
            }

            if (purchase != null)
            {
                purchase.Quantity += quantity;
            }

            if (purchase == null)
            {
                purchase = new PurchasedGood {
                    DrinkId = drink.Id, Quantity = quantity, ShoppingBasketViewModel = basket, ShoppingBasketViewModelId = basket.Id
                };
            }

            basket.Goods.Add(purchase);
            context.SaveChangesAsync();
        }
예제 #2
0
 public void Delete(PurchasedGood element)
 {
     DbContext.PurchasedGoods.Remove(element);
 }
예제 #3
0
 public void Create(PurchasedGood element)
 {
     DbContext.PurchasedGoods.Add(element);
 }