/// <summary> /// Get an Order by Id /// </summary> /// <param name="id"></param> /// <returns>Order and Order Items</returns> public OrderDTO Get(long id) { var result = db.Orders.Where(o => o.Id == id); if (result.Count() == 0) return null; Order order = result.ToList()[0]; // transform the order DTO OrderDTO orderDTO = new OrderDTO() { Id = order.Id, CreatedDate = order.CreatedDate, ModifiedDate = order.ModifiedDate, Status = order.Status }; // transform the order item DTOs orderDTO.OrderItems = new List<OrderItemDTO>(); foreach(OrderItem orderItem in order.OrderItems) { OrderItemDTO orderItemDTO = new OrderItemDTO() { Id = orderItem.Id, CreateDate = orderItem.CreateDate, ModifiedDate = orderItem.ModifiedDate, OrderId = orderItem.OrderId, Price = orderItem.Price, ProductCode = orderItem.ProductCode, PromotionApplied = orderItem.PromotionApplied }; orderDTO.OrderItems.Add(orderItemDTO); } return orderDTO; }
public void ShouldCreateOrder() { // save the order OrderDTO orderDTO = new OrderDTO() { CreatedDate = DateTime.Now, Status = Constants.OrderStatus.Pending }; orderDTO.OrderItems = new List<OrderItemDTO>(); OrderItemDTO orderItemAppleDTO = new OrderItemDTO() { CreateDate = DateTime.Now, Price = 1, ProductCode = "apple" }; orderDTO.OrderItems.Add(orderItemAppleDTO); OrderItemDTO orderItemNutellaDTO = new OrderItemDTO() { CreateDate = DateTime.Now, Price = 5, ProductCode = "nuttl" }; orderDTO.OrderItems.Add(orderItemNutellaDTO); orderDTO = orderService.Save(orderDTO); Assert.IsTrue(orderDTO != null && orderDTO.Id > 0, "Order has not been saved"); Assert.IsTrue(orderDTO.OrderItems != null && orderDTO.OrderItems.Count == 2, "Order items have not been saved"); }
public void CheckoutShoppingCart() { // get the shopping cart ShoppingCartDTO shoppingCart = GetShoppingCart(); // get the list of products List<ProductDTO> products = productService.GetProducts(); // turn the shopping cart into an order OrderDTO order = new OrderDTO { CreatedDate = DateTime.Now, Status = Constants.OrderStatus.Pending }; order.OrderItems = new List<OrderItemDTO>(); List<ShoppingCartItemDTO> orderedCart = shoppingCart.ShoppingCartItems.OrderBy(c => c.ProductCode).ToList(); int productItemCount = 0; string lastProductCode = ""; foreach (ShoppingCartItemDTO cartItem in orderedCart) { bool promotionApplied = false; // validate the product if (!products.Exists(p => p.Code == cartItem.ProductCode)) { Console.WriteLine("Product in cart is not valid: " + cartItem.ProductCode); break; } // get the product ProductDTO product = products.First(p => p.Code == cartItem.ProductCode); if (product.Code != lastProductCode) { productItemCount = 1; lastProductCode = product.Code; } else productItemCount++; // set the price (including a discount) decimal price = GetPrice(product, ref productItemCount, ref promotionApplied, order); // add the order item OrderItemDTO orderItem = new OrderItemDTO { CreateDate = DateTime.Now, ProductCode = cartItem.ProductCode, Price = price, PromotionApplied = promotionApplied }; order.OrderItems.Add(orderItem); } if (order.OrderItems.Count == 0) return; // save the order order.Status = Constants.OrderStatus.Complete; order = orderService.Save(order); // print the invoice DisplayInvoice(order); }