public Int32 GetBasket(IEnumerable <BasketAPI> Basket) { var BasketForDB = new Basket() { BasketDate = DateTime.Now }; var Product = new Product(); foreach (var Element in Basket) { Product = _context.Product.SingleOrDefault(p => p.Id == Element.ProductId); if (Product != null) { var BasketElement = new BasketElement(); BasketElement.ProductId = Product.Id; BasketElement.ProductName = Product.Name; BasketElement.ProductPhoto = Product.Photo; BasketElement.Price = Product.Price; BasketElement.Quant = (short)Element.Quantity; BasketForDB.Content.Add(BasketElement); } } if (BasketForDB.Content.Count() == 0) { return(0); } else { _context.Basket.Add(BasketForDB); _context.SaveChanges(); return(BasketForDB.Id); } }
public ActionResult Index(int Quantity1, Item itm) { BasketElement item = new BasketElement { Item = itm, Quantity = Quantity1 }; if (GlobalVariables.counter == 0) { List <BasketElement> basketElements = new List <BasketElement>(); basketElements.Add(item); Basket basket = new Basket { BasketIElements = basketElements }; GlobalVariables.MyBasket = basket; } else { var basketElements = GlobalVariables.MyBasket.BasketIElements; basketElements.Add(item); GlobalVariables.MyBasket.BasketIElements = basketElements; } GlobalVariables.counter = GlobalVariables.counter + 1; return(RedirectToAction("Index", "Home", new { id = GlobalVariables.LoginUser.Id })); }
public ActionResult PlaceOrder(Order OrderIn) { if (ModelState.IsValid) { var newOrder = new Order(); newOrder.OrderTime = DateTime.Now; newOrder.DelivaryToTime = DateTime.Now; newOrder.CustomerId = OrderIn.CustomerId; newOrder.ActualName = OrderIn.ActualName; newOrder.ActualSurName = OrderIn.ActualSurName; newOrder.ActualPhone = OrderIn.ActualPhone; newOrder.ActualEmail = OrderIn.ActualEmail; newOrder.ActualStreet = OrderIn.ActualStreet; newOrder.ActualHomeNr = OrderIn.ActualHomeNr; newOrder.ActualFlatNr = OrderIn.ActualFlatNr; if (OrderIn.DeliveryASAP) { newOrder.DeliveryASAP = true; } else { newOrder.DelivaryToTime = OrderIn.DelivaryToTime; } if (OrderIn.PayByCash) { newOrder.PayByCash = true; } else { newOrder.PayByCard = true; } newOrder.OrderDetail = OrderIn.OrderDetail; newOrder.Done = false; float total = 0; foreach (var BasketEl in OrderIn.OrderContent.ToList()) { if ((BasketEl != null) && (BasketEl.Quant != 0) && (BasketEl.ProductId != 0)) { var Product = _DBdata.Product.SingleOrDefault(p => p.Id == BasketEl.ProductId); if (Product != null) { var newBasketElement = new BasketElement(); newBasketElement.ProductId = Product.Id; newBasketElement.ProductName = Product.Name; newBasketElement.Price = Product.Price; newBasketElement.ProductPhoto = Product.Photo; newBasketElement.Quant = BasketEl.Quant; newOrder.OrderContent.Add(newBasketElement); total = total + Product.Price * BasketEl.Quant; } } } newOrder.Total = total; _DBdata.Order.Add(newOrder); var BasketId = OrderIn.BasketId; _DBdata.Basket.SingleOrDefault(b => b.Id == BasketId).CustomerId = OrderIn.CustomerId; _DBdata.SaveChanges(); return(RedirectToAction("Index", "Home", new { order = true })); } else { return(View("OrderList", "Customer", new { id = OrderIn.BasketId })); } }