Permalink
Browse files

Cleaned up cart.erl a bit

  • Loading branch information...
1 parent 6156763 commit a9eb81976b234b4ecd86022b09560f3beed79c59 Mino committed May 10, 2010
Showing with 61 additions and 124 deletions.
  1. +61 −64 src/cart.erl
  2. +0 −60 src/cartItem.erl
View
125 src/cart.erl
@@ -12,6 +12,66 @@
-include("records.hrl").
-include("utils.hrl").
+%%====================================================================
+%% API Functions
+%%====================================================================
+add(Quantity, Product) ->
+ Cart = get_cart(),
+ ProdId = product:get_Id(Product),
+ Prods = cc_add(#cartItem{prodId=ProdId,quantity=Quantity},
+ Cart#cart.products, []),
+ set_cart(Cart#cart{products=Prods}),
+ wf:update(cart_content,list()).
+
+%%====================================================================
+%% Internal Functions
+%%====================================================================
+set_cart(Cart) ->
+ wf:session(cart,Cart),
+ wf:set_cookie(cart,wf:pickle(Cart)),
+ case true of %%Check if user is logged in
+ true ->
+ []; %%add cart to db
+ _ ->
+ []
+ end.
+
+get_cart() ->
+ case true of %%check user logged in
+ false ->
+ cookie_cart();
+ true ->
+ db_cart()
+ end.
+
+cookie_cart() ->
+ case wf:get_cookie(cart) of
+ undefined ->
+ #cart{products=[]};
+ Cart ->
+ wf:depickle(Cart)
+ end.
+
+db_cart() ->
+ #cart{products=[]}.
+
+
+cc_add(Item,[],Acc) ->
+ [Item|lists:reverse(Acc)];
+cc_add(Item,[HD|TL],Acc) ->
+ case Item#cartItem.prodId == HD#cartItem.prodId of
+ true ->
+ NewQty = Item#cartItem.quantity + HD#cartItem.quantity,
+ lists:reverse(Acc,[HD#cartItem{quantity=NewQty}|TL]);
+ false ->
+ cc_add(Item,TL,[HD|Acc])
+ end.
+
+
+sum_cart([]) ->
+ 0;
+sum_cart([{Quantity,Product}|Tail]) ->
+ (Quantity * product:get_price(Product)) + sum_cart(Tail).
%%====================================================================
%% Template Functions
@@ -68,71 +128,8 @@ updateLink() ->
#link{text="update", actions=Delegate}.
%%====================================================================
-%% External Functions
-%%====================================================================
-add(Quantity, Product) ->
- Cart = get_cart(),
- ProdId = product:get_Id(Product),
- Prods = cc_add(#cartItem{prodId=ProdId,quantity=Quantity},
- Cart#cart.products, []),
- set_cart(Cart#cart{products=Prods}),
- wf:update(cart_content,list()).
-
-%%====================================================================
-%% Internal Functions
+%% Nitrogen Events
%%====================================================================
-set_cart(Cart) ->
- wf:session(cart,Cart),
- wf:set_cookie(cart,wf:pickle(Cart)),
- case true of %%Check if user is logged in
- true ->
- []; %%add cart to db
- _ ->
- []
- end.
-
-get_cart() ->
- case wf:session(cart) of
- undefined ->
- Cart = case cookie of %%check user logged in
- cookie ->
- cookie_cart();
- db ->
- db_cart()
- end,
- wf:session(cart,Cart),
- Cart;
- Cart ->
- Cart
- end.
-
-cookie_cart() ->
- case wf:get_cookie(cart) of
- undefined ->
- #cart{products=[]};
- Cart ->
- wf:depickle(Cart)
- end.
-
-db_cart() ->
- #cart{products=[]}.
-
-cc_add(Item,[],Acc) ->
- [Item|lists:reverse(Acc)];
-cc_add(Item,[HD|TL],Acc) ->
- case Item#cartItem.prodId == HD#cartItem.prodId of
- true ->
- NewQty = Item#cartItem.quantity + HD#cartItem.quantity,
- lists:reverse(Acc,[HD#cartItem{quantity=NewQty}|TL]);
- false ->
- cc_add(Item,TL,[HD|Acc])
- end.
-
-sum_cart([]) ->
- 0;
-sum_cart([{Quantity,Product}|Tail]) ->
- (Quantity * product:get_price(Product)) + sum_cart(Tail).
-
event({delete,ProdId}) ->
Cart = get_cart(),
Products = [X||X<-Cart#cart.products,X#cartItem.prodId /= ProdId],
View
60 src/cartItem.erl
@@ -1,60 +0,0 @@
-%%%-------------------------------------------------------------------
-%%% File : cartItem.erl
-%%% Author : <Mino@X60S>
-%%% Description :
-%%%
-%%% Created : 29 Apr 2010 by <Mino@X60S>
-%%%-------------------------------------------------------------------
--module (cartItem).
--compile (export_all).
-
--include_lib ("nitrogen/include/wf.inc").
--include("records.hrl").
-
-%%====================================================================
-%% API Functions
-%%====================================================================
-load(Product) when is_record(Product,product)->
- wf:state(product,Product);
-load(ProdId) ->
- wf:state(product,db:get_product(ProdId)).
-
-load() ->
- ?PRINT(wf:state(pathInfo)),
- case wf:state(pathInfo) of
- "112"++ProdId ->
- load(list_to_integer(ProdId)),
- true;
- _ -> false
- end.
-%%====================================================================
-%% Template Functions
-%%====================================================================
-
-id() ->
- product:id().
-
-name() ->
- product:name().
-
-url() ->
- product:url().
-
-img() ->
- product:img().
-
-description() ->
- product:description().
-
-price() ->
- product:price().
-
-buyLink() ->
- product:buyLink().
-
-quantityTextbox(Class) ->
- #textbox{class=Class}.
-
-
-event(Event) ->
- ?PRINT(Event).

0 comments on commit a9eb819

Please sign in to comment.