public async Task <IActionResult> MakeOrder() { //Gets Current User var user = await _userManager.GetUserAsync(User); var userID = user.Id; //Gets all Books in Cart var booksInCart = _cartService.GetCartBooks(userID); //Creates a new Order var newOrder = new OrderInputModel() { UserID = userID, Books = booksInCart, }; //Checks if user has current order to see if we will be adding order or updating order. bool userHasCurrentOrder = _orderService.UserHasCurrentOrder(userID); //If User does not have a current order. if (!userHasCurrentOrder) { //Adds new Order _orderService.AddOrder(newOrder); var orderID = _orderService.GetCurrentOrderID(userID); //Adds new Order Book Connections for all books in cart. for (int i = 0; i < booksInCart.Count; i++) { _obcService.AddConnection((int)orderID, booksInCart[i].ID, booksInCart[i].Amount); } } else { //Updates order with the new Cart. var orderID = _orderService.GetCurrentOrderID(userID); _orderService.UpdateOrder(newOrder, (int)orderID); //Updates order book connections with new amounts or new books. for (int i = 0; i < booksInCart.Count; i++) { _obcService.UpdateConnection((int)orderID, booksInCart[i].ID, booksInCart[i].Amount); } } //Lastly returns User to The Check out window for order. return(RedirectToAction("CheckOut", "Order")); }