public async Task <IActionResult> NewOrder(OrderDto_ToCreate newProduct)
 {
     return(Ok(await _orderService.NewOrder(newProduct)));
 }
        public async Task <ServiceResponse <OrderDto_ToReturn> > NewOrder(OrderDto_ToCreate newOrder)
        {
            //Validate OrderItem
            var   errorMessage = "";
            bool  checkInput   = false;
            float totalCal     = 0;

            foreach (var item in newOrder.OrderItems)
            {
                var product = await _dbContext.Products
                              .FirstOrDefaultAsync(x => x.Id == item.ProductId && x.IsActive == true);

                if (product != null)
                {
                    if (item.Total != product.Price * item.Quantity)
                    {
                        errorMessage += "Incorrct calculate total order item.";
                        checkInput    = true;
                    }
                    if (checkInput)
                    {
                        errorMessage += "Product Id = " + item.ProductId.ToString();
                        return(ResponseResult.Failure <OrderDto_ToReturn>(errorMessage));
                    }
                    else
                    {
                        totalCal += product.Price * item.Quantity;
                    }
                }
                else
                {
                    errorMessage += "Can not find product Id = " + item.ProductId.ToString();
                    return(ResponseResult.Failure <OrderDto_ToReturn>(errorMessage));
                }
            }

            //Validate OrderHeader
            if (totalCal != newOrder.Total)
            {
                errorMessage += "Incorrct calculate total order.";
                return(ResponseResult.Failure <OrderDto_ToReturn>(errorMessage));
            }
            else
            {
                if (newOrder.Net != totalCal - newOrder.Discount)
                {
                    errorMessage += "Incorrct calculate net.";
                    return(ResponseResult.Failure <OrderDto_ToReturn>(errorMessage));
                }
                else
                {
                    //InsertOrder
                    var order = new mOrder
                    {
                        Total       = totalCal,
                        Discount    = newOrder.Discount,
                        Net         = totalCal - newOrder.Discount,
                        CreatedDate = Now(),
                        IsActive    = true,
                    };
                    _dbContext.Orders.Add(order);
                    await _dbContext.SaveChangesAsync();

                    //InsertOrderItem
                    List <mOrderItem> ordetItem = new List <mOrderItem>();
                    foreach (var item in newOrder.OrderItems)
                    {
                        var product = await _dbContext.Products.FirstOrDefaultAsync(x => x.Id == item.ProductId);

                        ordetItem.Add(new mOrderItem()
                        {
                            OrderId   = order.Id,
                            ProductId = item.ProductId,
                            Price     = product.Price,
                            Quantity  = item.Quantity,
                            Total     = item.Total
                        });
                    }
                    _dbContext.OrderItems.AddRange(ordetItem);
                    await _dbContext.SaveChangesAsync();


                    //Include List
                    int maxId = await _dbContext.Orders.MaxAsync(x => x.Id);

                    var getLastOrder = await _dbContext.Orders.Include(x => x.OrderItems).FirstOrDefaultAsync(x => x.Id == maxId);


                    return(ResponseResult.Success(_mapper.Map <OrderDto_ToReturn>(order)));
                }
            }
        }