コード例 #1
0
        public async Task <ActionResult> createOrder(OrderDto model)
        {
            double totalAmount = 0;

            foreach (var item in model.OrderDetail)
            {
                if (item.Quantity <= 0 || item.Quantity > 5)
                {
                    return(BadRequest());
                }
                var product = _productService.GetProduct(item.ProductId);
                if (product == null)
                {
                    return(BadRequest());
                }
                totalAmount += product.CurrentPrice * item.Quantity;
            }

            var order = model.Adapt <Order>();

            order.TotalAmount = totalAmount;
            order.Status      = OrderCurrentStatus.received.ToString();
            _orderService.CreateOrder(order);
            foreach (var item in model.OrderDetail)
            {
                var orderDetail = item.Adapt <OrderDetail>();
                orderDetail.OrderId = order.Id;
                _detailService.CreateOrderDetail(orderDetail);
            }
            _orderService.SaveChanges();
            return(Ok());
        }