public static Book ToModel(this IBook book, bool details = false) { if(book == null) return null; var bookDto = new Book() { Id = book.Id, Publisher = book.Publisher.ToModel(), Title = book.Title, Price = book.Price, PublishedOn = book.PublishedOn, Category = book.Category, Editions = book.Editions, Authors = book.Authors.Select(a => a.ToModel(false)).ToList() }; if(book.CoverImage != null) bookDto.CoverImageId = book.CoverImage.Id; if(details) { bookDto.Description = book.Description; bookDto.Abstract = book.Abstract; bookDto.LatestReviews = GetLatestReviews(book, 5); } return bookDto; }
public void TestUserOrders() { var client = SetupHelper.Client; //Get dora's order(s) - she has 1 order for c# book; LoginAs("dora"); // dora's userId will be forced by controller into query condition var doraOrders = client.ExecuteGet<SearchResults<BookOrder>>("api/user/orders?booktitle={0}&publisher=ms&mintotal=5", "c#"); Assert.AreEqual(1, doraOrders.Results.Count, "Expected c# book order for dora."); var order0 = doraOrders.Results[0]; Assert.IsNull(order0.Items, "Expected no items"); //items are returned when we get single order by id //get order details var orderDet = client.ExecuteGet<BookOrder>("api/user/orders/{0}", order0.Id); Assert.IsTrue(orderDet.Items.Count > 0, "Expected items in Dora's book order."); //there must be 2 items //cart var cart = client.ExecuteGet<BookOrder>("api/user/cart"); Assert.IsNull(cart, "Expected empty cart."); // Dora decides to buy iron man book var ironManSearch = client.ExecuteGet<SearchResults<Book>>("api/books?title=iron"); Assert.IsTrue(ironManSearch.Results.Count == 1, "Iron man book not found."); var ironManBook = ironManSearch.Results[0]; // Create cart by simply adding a book to new order var ironManBookSlim = new Book() { Id = ironManBook.Id }; //we could use ironManBook, but we actually need only book Id there var ironManItem = new BookOrderItem() {Book = ironManBookSlim, Quantity = 1}; ironManItem = client.ExecutePost<BookOrderItem, BookOrderItem>(ironManItem, "api/user/cart/item"); Assert.IsNotNull(ironManItem, "Expected order item"); // cart now is not empty and holds one item; cart total should be updated automatically cart = client.ExecuteGet<BookOrder>("api/user/cart"); Assert.IsNotNull(cart, "Expected not empty cart"); Assert.AreEqual(1, cart.Items.Count, "Expected one item in the cart"); Assert.IsTrue(Math.Abs(ironManBook.Price - cart.Total) < 0.1m, "Cart total does not match book price"); // Let's change quantity (2 Iron Man books) and add "Windows Programming" book, and update order cart.Items[0].Quantity = 2; var winBook = client.ExecuteGet<SearchResults<Book>>("api/books?title=windows").Results[0]; var winBookItem = new BookOrderItem() { Book = winBook, Quantity = 3 }; cart.Items.Add(winBookItem); //Update whole order var updatedCart = client.ExecutePut<BookOrder, BookOrder>(cart, "api/user/cart"); var expectedTotal = ironManBook.Price * 2 + winBook.Price * 3; Assert.IsTrue(Math.Abs(expectedTotal - updatedCart.Total) < 0.1m, "Cart total does not match books price"); //Let's remove windows book by setting Quantity = 0 winBookItem.Quantity = 0; updatedCart = client.ExecutePut<BookOrder, BookOrder>(cart, "api/user/cart"); Assert.IsTrue(updatedCart.Items.Count == 1, "Expected only one book in cart."); /* // Cancel order - tested once, now commented, submitting instead client.ExecuteDelete("api/user/cart"); cart = client.ExecuteGet<BookOrder>("api/user/cart"); Assert.IsNull(cart, "Expected empty cart."); */ //Sumbit order var order = client.ExecutePut<object, BookOrder>(null, "api/user/cart/submit"); //no promo/coupon Assert.IsNotNull(order, "Expected submitted order"); Assert.AreEqual(OrderStatus.Completed, order.Status, "Expected completed order"); Assert.IsTrue(Math.Abs(order.Total - ironManBook.Price * 2) < 0.1m, "Total does not match."); //cart should be empty now cart = client.ExecuteGet<BookOrder>("api/user/cart"); Assert.IsNull(cart, "Expected empty cart."); Logout(); }