public IActionResult makeOrder(string id, string address) { decimal totalPrice = 0; //make list of id's of his products and total price List <int> ids = new List <int>(); List <penddingOrder> penddings = context.penddingOrders.Select(x => x).ToList(); List <product> p = context.Products.Select(x => x).ToList(); foreach (var item in penddings) { if (item.user_id == id) { ids.Add(item.prod_id); foreach (var item2 in p) { if (item.prod_id == item2.productId) { totalPrice += item2.productPrice; } } } } //make order with user id order newOrder = new order { userId = id, orederDate = DateTime.Now, orderAddress = address, orderTotalPrice = totalPrice, orderStatus = "Open" }; context.orders.Add(newOrder); context.SaveChanges(); //id of order just created int o_id = newOrder.orderId; //make order items for order foreach (var item in ids) { product pr = context.Products.SingleOrDefault(x => x.productId == item); orderItem orderitem = new orderItem { orderId = o_id, productId = item, quantity = 1, subTotal = pr.productPrice }; context.orderItems.Add(orderitem); context.SaveChanges(); } //delete order from penndingOrder Because it aleady goes to order table foreach (var item in ids) { foreach (var item1 in penddings) { if (item == item1.prod_id) { penddingOrder pend = context.penddingOrders.Find(item1.pendingId); context.Remove(pend); context.SaveChanges(); } } } return(RedirectToAction("shoppingcart")); }
public IActionResult deleteFcart(int id) { penddingOrder pendding = context.penddingOrders.SingleOrDefault(x => x.prod_id == id); context.Remove(pendding); context.SaveChanges(); return(RedirectToAction("shoppingcart")); }
//[Route("Hemalaya")] public IActionResult Index(int ProductId) { //Get Id Of the currently logged in user var loggedInUser = userManager.GetUserId(User); bool idFound = false; List <penddingOrder> allpendding = context.penddingOrders.Select(x => x).ToList(); foreach (var item in allpendding) { if (item.prod_id == ProductId) { idFound = true; } } if (idFound != true) { penddingOrder pendding = new penddingOrder { prod_id = ProductId, user_id = loggedInUser }; context.penddingOrders.Add(pendding); context.SaveChanges(); } return(RedirectToAction("Index")); }