public async Task <IActionResult> OnPost() { Memberships = membershipData.GetMemberships(); ApplicationUser = await userManager.GetUserAsync(User); if (ModelState.IsValid) { foreach (var cartItem in HttpContext.Session.GetObjectFromJson <List <ShoppingCart> >("CartItems").ToList()) { OrderDetails.Add(new OrderDetail { Product = productData.GetProductById(cartItem.Id), Quantity = cartItem.Quantity }); // Product = productData.GetProductById(cartItem.Id); Product.Quantity -= cartItem.Quantity; // } if (Selected != null) { var membership = membershipData.GetMembershipById(Convert.ToInt32(Selected)); ApplicationUser.Membership = membership; Order.OrderTotal = cartBL.MemberOnCheckout(Convert.ToDouble(HttpContext.Session.GetString("TotalPrice")), ApplicationUser); } else { Order.OrderTotal = Convert.ToDouble(HttpContext.Session.GetString("TotalPrice")); } Order.OrderDetails = OrderDetails; Order.UserId = ApplicationUser.Id; // Order.DateOrdered = DateTime.Now; // orderData.CreateOrder(Order); orderData.Commit(); HttpContext.Session.Remove("CartItems"); return(RedirectToPage("/Order/CheckoutComplete")); } return(Page()); }
public async Task <IActionResult> OnPostRelizeOrder() { LoggedUser = await userManager.GetUserAsync(User); UserData = memberData.GetMemberById(LoggedUser.Id); UserData.Basket = memberData.GetAllBasketItems(UserData.Id).ToList(); SetListsBasedOnBasket(UserData.Basket); if (BasketItems.Count > 0) { //Creation of new order var order = new Order(); //Timestamp order.Time = DateTime.Now; //Setting a member id of whom the order belong to order.MemberId = UserData.Id; //Setting order.Products to products that are in basket list. order.Products = OrderedProducts(BasketItems); foreach (var orderedProduct in order.Products) { order.Cost += productData.GetById(orderedProduct.ProductId).Price; } //Setting products from user basket to unavailable to make them impossible to buy twice or add to another basket productService.SetProductsUnavailable(BasketItems); //Adding data into DB orderData.Add(order); orderData.Commit(); TempData["Message"] = "Order placed"; } else { TempData["Message"] = "Order cannot be placed"; } return(RedirectToPage("./Basket")); }