private Order manageOrder(XElement orderXML, Order order) { var orderItems = orderXML.Element("Items").Elements("Item"); foreach (var item in orderItems) { var itemId = Int32.Parse(item.Element("Id").Value); var itemToAdd = db.Items.Where(x => x.Id == itemId).FirstOrDefault(); if (itemToAdd != null) { var x = itemToAdd.Price * Int32.Parse(item.Element("Quantity").Value); var line = new Line() { ItemId = Int32.Parse(item.Element("Id").Value), OrderUniqueId = order.UniqueId, LineQty = Int32.Parse(item.Element("Quantity").Value), LinePrice = x }; db.Lines.Add(line); order.Total += Convert.ToDecimal(line.LinePrice); } } return order; }
// POST api/order public Order Post(XElement orderXML) { OrderLocation loc; OrderStatus stat; Enum.TryParse(orderXML.Element("Location").Value, out loc); Enum.TryParse(orderXML.Element("Status").Value, out stat); var order = new Order() { UniqueId = Guid.NewGuid(), CustomerFirstName = orderXML.Element("CustomerFirstName").Value, CustomerLastName = orderXML.Element("CustomerLastName").Value, Location = loc, Status = stat, Total = 0 }; manageOrder(orderXML, order); db.Orders.Add(order); db.SaveChanges(); var createdOrder = db.Orders.Where(x => x.UniqueId == order.UniqueId).FirstOrDefault(); return createdOrder; }