public BookOrderItem AddOrderItem(BookOrderItem item) { var session = OpenSession(); var bk = session.GetEntity<IBook>(item.Book.Id); Context.ThrowIfNull(bk, ClientFaultCodes.ObjectNotFound, "BookId", "Book not found."); var cart = GetOpenOrder(session, LockOptions.ForUpdate, create: true); var itemEnt = cart.Add(bk, item.Quantity); cart.ScheduleUpdateTotal(); session.SaveChanges(); return itemEnt.ToModel(); }
public BookOrderItem AddOrderItem(BookOrderItem item) { var session = OpenSession(); var bk = session.GetEntity <IBook>(item.Book.Id); Context.ThrowIfNull(bk, ClientFaultCodes.ObjectNotFound, "BookId", "Book not found."); var cart = GetOpenOrder(session, LockType.ForUpdate, create: true); var itemEnt = cart.Add(bk, item.Quantity); cart.ScheduleUpdateTotal(); session.SaveChanges(); return(itemEnt.ToModel()); }
public static BookOrderItem ToModel(this IBookOrderLine line) { if (line == null) { return(null); } var item = new BookOrderItem() { Id = line.Id, OrderId = line.Order.Id, Book = line.Book.ToModel(), Quantity = line.Quantity }; return(item); }
public BookOrderItem UpdateOrderItem(BookOrderItem item) { var session = OpenSession(); //Lock order var order = session.GetEntity <IBookOrder>(item.OrderId, LockType.ForUpdate); var itemEnt = session.GetEntity <IBookOrderLine>(item.Id); Context.ThrowIfNull(itemEnt, ClientFaultCodes.ObjectNotFound, "BookId", "Book not found."); if (item.Quantity == 0) { session.DeleteEntity(itemEnt); } else { itemEnt.Quantity = item.Quantity; } itemEnt.Order.ScheduleUpdateTotal(); session.SaveChanges(); return(itemEnt.ToModel()); }
public BookOrderItem UpdateOrderItem(BookOrderItem item) { var session = OpenSession(); //Lock order var order = session.GetEntity<IBookOrder>(item.OrderId, LockOptions.ForUpdate); var itemEnt = session.GetEntity<IBookOrderLine>(item.Id); Context.ThrowIfNull(itemEnt, ClientFaultCodes.ObjectNotFound, "BookId", "Book not found."); if (item.Quantity == 0) session.DeleteEntity(itemEnt); else itemEnt.Quantity = item.Quantity; itemEnt.Order.ScheduleUpdateTotal(); session.SaveChanges(); return itemEnt.ToModel(); }
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(); }
public static BookOrderItem ToModel(this IBookOrderLine line) { if(line == null) return null; var item = new BookOrderItem() { Id = line.Id, OrderId = line.Order.Id, Book = line.Book.ToModel(), Quantity = line.Quantity }; return item; }