예제 #1
0
        private double GetTimeOrderIsPlaced()
        {
            DateTime currentTime      = DateTime.Now;
            double   currentTimePOSIX = DateTimeUtility.GetUnixEpochAsDouble(currentTime);

            return(currentTimePOSIX);
        }
        public async Task <IActionResult> SubmitOrder()
        {
            if (CurrentCustomer == null)
            {
                return(await LoginRedirectActionTask);
            }
            if (CurrentCart == null)
            {
                return(await LoginRedirectActionTask);                    //TODO: Redirect properly.
            }
            try
            {
                // Arrange data needed to submit the order.
                List <InventoryLineItem> tempCart   = CurrentCart;
                List <OrderLineItem>     orderItems = new List <OrderLineItem>();

                // Get the time of the order.
                double timeNowAsDouble = DateTimeUtility.GetUnixEpochAsDouble(DateTime.Now);

                // Get the total of the order.
                double total = 0.0;
                tempCart.ForEach(cartItem => total += cartItem.Product.Price * cartItem.ProductQuantity);

                // Generate a new order, without an ID.
                Order newOrder = new Order(CurrentCustomer.Id, CurrentLocation.Id, CurrentCustomer.Address, total, timeNowAsDouble);

                // Submit the order without the ID.
                var postOrderRequest = $"order/add";
                await this.PostDataAsync(postOrderRequest, newOrder);

                // Get the order back so we can have the ID.
                var getOrderRequest         = $"order/get?dateTimeDouble={timeNowAsDouble}";
                var retrievedRedundantOrder = await this.GetDataAsync <Order>(getOrderRequest);

                #region Make the order items from the cart items, using the fetched order Id.
                foreach (InventoryLineItem cartItem in tempCart)
                {
                    var newOrderItem = new OrderLineItem(retrievedRedundantOrder.Id, cartItem.ProductId, cartItem.ProductQuantity);
                    orderItems.Add(newOrderItem);
                }
                #endregion


                #region Submit the order line items.
                var postOrderLineItemRequest = $"order/lineitem/addmany";
                await this.PostDataAsync(postOrderLineItemRequest, orderItems);

                /*foreach (OrderLineItem oli in orderItems)
                 * {
                 *  var postOrderLineItemRequest = $"order/lineitem/add";
                 *  await this.PostDataAsync(postOrderLineItemRequest, oli);
                 * }*/
                #endregion

                string request = $"location/stock/get/{CurrentLocation.Id}";
                var    stock   = await this.GetDataAsync <List <InventoryLineItem> >(request);

                var processedStock = ReturnStockWithoutCartItems(stock, tempCart);

                foreach (InventoryLineItem ili in processedStock)
                {
                    string putStockRequest = $"location/stock/update";
                    await this.PutDataAsync(putStockRequest, ili);
                }

                // Clear all relevant data.
                tempCart.Clear();
                CurrentCart = null;

                // TODO: Show successful order submission.
                return(RedirectToAction("Index"));
            } catch (HttpRequestException e)
            {
                return(StatusCode(500, e));
            }
        }