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(); }
public void Delete(PurchasedGood element) { DbContext.PurchasedGoods.Remove(element); }
public void Create(PurchasedGood element) { DbContext.PurchasedGoods.Add(element); }