Database Reference
In-Depth Information
{ sku : '00e8da9b' , qty : 1 , details : {...} },
{ sku : '0ab42f88' , qty : 4 , details : {...} }
]
}
Note that we've copied the item details from the product document into the cart document
so we can display relevant details for each line item without fetching the original product
document. This also helps us avoid the usability problem of what to do about a SKU that
changes prices between being added to the cart and checking out; in this case, we always
charge the user the price at the time the item was added to the cart .
Operations
This section introduces operations that we'll want to support on our data model. As always,
the examples use the Python programming language and the pymongo driver, but the system
can be implemented in any language you choose.
Add an item to a shopping cart
Movinganitemfromtheavailableinventorytoacartisafundamentalrequirementforashop-
ping cart system. Our system must ensure that an item is never added to a shopping cart unless
there is sufficient inventory to fulfill the order.
PATTERNS
For this operation, and for several others in this section, we'll use patterns from Chapter 3
to keep our product and cart collections consistent.
In order to add an item to our cart, the basic approach will be to:
1. Update the cart, ensuring it is still active, and adding the line item.
2. Update the inventory, decrementing available stock, only if there is sufficient inventory
available .
3. If the inventory update failed due to lack of inventory, compensate by rolling back our
cart update and raising an exception to the user.
Search WWH ::




Custom Search