Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

advice on implementing a shopping cart using playframework

I am learning to use playframework by writing code to implement a webstore for selling items.I have implemented the Admin area using the crud and secure modules.Now, I want to create a shopping cart to which a user can add items and proceed to checkout .

My knowledge of ecommerce is minimal,but I had gone through some textbooks which implement shopping carts and some webshop functionality using servlets.In the books ,the cart used to keep a Set of CartItems ,each of which contained an instance of the Product and quantity.After,the user added items to cart,the cart was stored in user session.

So,anytime the user went to the cart details page,it showed all the added items.Only when the session was cleared,(either due to session timeout as defined in the server,or when the order was placed)the CartItems were removed from the ShoppingCart.

I guess ,I can use the Cache in playframework to do the above.After adding a CartItem to ShoppingCart instance .I can

shopcart.add(mycartItem);
Cache.set(session.getId(), shopcart);
..

and later,in another page ,I can retrieve,the cart and its contents,process them and clear the cart.

ShopCart cart = Cache.get(session.getId(),ShopCart.class);
Set<CartItem> items = cart.getCartItems();
processOrder(items,userinfo);
...
cart.clearItems();

Is this the right way to go about this?If the way I am thinking is not correct,please help me with suggestions.

like image 869
Damon Julian Avatar asked Aug 30 '11 13:08

Damon Julian


2 Answers

Wrong. Sorry for being so harsh, but what you are doing may fail horribly.

Play is share nothing and stateless. All relevant data MUST be stored in the database. That means that you must persist the data of the shopping cart when the user adds a new item, and remove it once the transaction finishes.

Why can't you use the cache? Because the cache is volatile and the elements in it may be removed without previous warning. That means that at any moment your cache may return a null (item not found) and you get a NullPointerException in your code and your user has lost the cart.

Cache is there only to help performance, but you can't expect it to have all the items you needs always.

You should turn your ShopCart into an entity (if it's not one) and add to it something like this:

public static ShopCart findCart(String sessionId){
   ShopCart sc = Cache.get(session.getId(),ShopCart.class);
   if(sc == null){
      sc = ShopCart.findCurrentUserCart(); //implement to find the cart of the current user
      Cache.set(sessionId,sc);
   }
   return sc; 
}

Then use this method to retrieve the cart before processing.

like image 183
Pere Villega Avatar answered Oct 22 '22 14:10

Pere Villega


I haven't got much experience with Play! but I think it is great. Your idea should work just fine. I would add a timeout for the cached cart object as in :

Cache.set(session.getId(), shopcart, "30mn");

the above code will cache the cart for 30 minutes.

note that Cache.get may return null so your code for processing should change to:

ShopCart cart = Cache.get(session.getId(),ShopCart.class);
if (cart != null) { 
  Set<CartItem> items = cart.getCartItems();
  processOrder(items,userinfo);
  cart.clearItems();
} else {
  // maybe show expired session warning?
}

also you should probably remove the cart object from the cache after a successful call to processOrder(...).

Cache.delete(session.getId());

Hope this helps.

like image 26
buritos Avatar answered Oct 22 '22 13:10

buritos