public IHttpActionResult PostOrder(OrderViewModel order_view_model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            Order order = new Order { Customer_Id=order_view_model.Customer_Id, OrderDate=DateTime.Now };
            //order.OrderDetails = new List<OrderDetail>();
            Product product;
            foreach (var detail in order_view_model.OrderDetails)
            {
                product = db.Products.Find(detail.Product_Id);
                order.OrderDetails.Add(new OrderDetail { Product=product, Quantity=detail.Quantity });
            }
            db.Orders.Add(order);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = order.Id }, order);
        }
        public IHttpActionResult PutOrder(int id, OrderViewModel order_view_model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != order_view_model.Id)
            {
                return BadRequest();
            }

            //把資料庫中的那筆資料讀出來
            var order = db.Orders.Find(order_view_model.Id);
            if (order == null)
            {
                return ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.NotFound, "這筆資料已被刪除!"));
            }
            else
            {
                try
                {
                    order.OrderDate = order_view_model.OrderDate;
                    order.Customer_Id = order_view_model.Customer_Id;
                    order.OrderDetails.Clear();
                    Product product;
                    foreach (var detail in order_view_model.OrderDetails)
                    {
                        product = db.Products.Find(detail.Product_Id);
                        order.OrderDetails.Add(new OrderDetail { Product = product, Quantity = detail.Quantity });
                    }
                    db.Entry(order).OriginalValues["Timestamp"] = Convert.FromBase64String(order_view_model.TimestampString);
                    db.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!OrderExists(id))
                        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound, "這筆資料已被刪除!"));
                    else
                        throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.Conflict, "這筆資料已被其他人修改!"));// ""
                }
            }

            return Ok(toOrderViewModel(order));
        }
        OrderViewModel toOrderViewModel(Order order)
        {
            var orderViewModel = new OrderViewModel {   Id=order.Id,
                                                                                Customer_Id=order.Customer_Id,
                                                                                OrderDate=order.OrderDate,
                                                                                TimestampString=Convert.ToBase64String(order.Timestamp) };

            orderViewModel.OrderDetails = new List<OrderDetailViewModel>();
            foreach (var detail in order.OrderDetails)
            {
                orderViewModel.OrderDetails.Add(new OrderDetailViewModel
                {
                    Order_Id = order.Id,
                    Product_Id = detail.Product_Id,
                    ProductName = detail.Product.Name,
                    Quantity = detail.Quantity
                });
            }
            return orderViewModel;
        }