Permalink
Browse files

it is now possible to add and remove items from the cart

  • Loading branch information...
1 parent 49cf139 commit 3dd150edd96f6e3d9a40807bd4761cc22a1a1cca Mino committed May 9, 2010
View
@@ -1 +1,2 @@
ebin/*
+Mnesia*
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
View
@@ -14,6 +14,7 @@
cat_id,
img,
price,
+ quantity,
description}).
-record(price,
@@ -26,7 +27,7 @@
-record(cart,
{products,
- sum}).
+ totalPrice}).
-record(cartItem,
{prodId,
View
@@ -11,8 +11,10 @@
-include_lib ("nitrogen/include/wf.inc").
-include("records.hrl").
+-define(i2l(Integer),integer_to_list(Integer)).
+
%%====================================================================
-%% API Functions
+%% Template Functions
%%====================================================================
list()->
Cart = get_cart(),
@@ -25,11 +27,45 @@ list()->
#panel{id=cart_content, body =
[#bind{ data = Data, map = Map, body = Body}]}.
+list(Template) ->
+ File = "./wwwroot/" ++ Template,
+ Cart = get_cart(),
+ CartItemList = Cart#cart.products,
+ Data = db:get_products(CartItemList),
+
+ Render = fun({Quantity,Product}) ->
+ product:load(Product#product{quantity=Quantity}),
+ wf:state(template_was_called,false),
+ wf:render(#template{ file=File })
+ end,
+
+ [Render(X)||X<-Data].
+
+quantityTextbox() ->
+ quantityTextbox("").
+quantityTextbox(Class) ->
+ #textbox{class=Class, text=?i2l(product:quantity())}.
+
+rowTotal() ->
+ price:toString(product:quantity() * product:raw_price()).
+
+totalPrice() ->
+ Cart = get_cart(),
+ CartItemList = Cart#cart.products,
+ Data = db:get_products(CartItemList),
+ price:toString(sum_cart(Data)).
+
+deleteLink() ->
+ Delegate = #event{type=click, delegate=cart,
+ postback={delete,product:id()}},
+ #link{text="delete", actions=Delegate}.
+
%%====================================================================
%% External Functions
%%====================================================================
-add(Quantity, ProdId) ->
+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}),
@@ -49,7 +85,6 @@ set_cart(Cart) ->
end.
get_cart() ->
- ?PRINT(wf:session(cart)),
case wf:session(cart) of
undefined ->
Cart = case cookie of %%check user logged in
@@ -85,3 +120,18 @@ cc_add(Item,[HD|TL],Acc) ->
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],
+
+ set_cart(Cart#cart{products=Products}),
+ wf:redirect("").
+
+
+
View
@@ -1,44 +0,0 @@
--module(cart).
--compile(export_all).
-
--include_lib ("nitrogen/include/wf.inc").
--include("records.hrl").
-
-list()->
- Cart = get_cart(),
- CartItemList = Cart#cart.products,
- ProdIdList = [X#cartItem.prodId||X<-CartItemList],
- Data = db:get_products(ProdIdList),
- Map = #product{name=name@text, url=name@url},
- Body = [#link{ id=name },
- #br{}],
- #bind{ data = Data, map = Map, body = Body}.
-
-add(Quantity, ProdId) ->
- Cart = get_cart(),
- Prods = cc_add(#cartItem{prodId=ProdId,quantity=Quantity},
- Cart#cart.products, []),
- wf:set_cookie(cart,wf:pickle(Cart#cart{products=Prods})).
-
-%%%%%% Internal Functions %%%%%%
-
-get_cart() ->
- case wf:get_cookie(cart) of
- undefined ->
- #cart{products=[]};
- Cart ->
- wf:depickle(Cart)
- end.
-
-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.
-
-
View
@@ -20,7 +20,6 @@ load(CatId) ->
wf:state(category,db:get_category(CatId)).
load() ->
- ?PRINT(wf:state(pathInfo)),
case wf:state(pathInfo) of
"99"++CatId ->
load(list_to_integer(CatId)),
@@ -45,7 +44,6 @@ list()->
listProducts() ->
Data = db:get_products((?CATEGORY)#category.id),
- wf:state(product_cache,Data),
Map = #product{name=name@text, url=name@url},
Body = [ #link{id=name},
#br{}],
@@ -67,10 +65,9 @@ list(Template) ->
listProducts(Template) ->
File = "./wwwroot/" ++ Template,
Data = db:get_products((?CATEGORY)#category.id),
- wf:state(prod_cache,Data),
-
+
Render = fun(Product) ->
- product:load(Product),
+ product:load(Product#product{quantity="1"}),
wf:state(template_was_called,false),
wf:render(#template{ file=File })
end,
View
@@ -1,76 +0,0 @@
--module (category).
--compile (export_all).
-
--include_lib ("nitrogen/include/wf.inc").
--include("records.hrl").
-
--define(CATEGORY,wf:state(category)).
-
-load(CatId) ->
- wf:state(category,db:get_category(CatId)),
-
- IdStr = integer_to_list(CatId),
- Path = "./tpl/" ++ IdStr ++ ".tpl",
- case file:read_file_info(Path) of
- {ok,_} ->
- #template { file=Path };
- {error,_} ->
- #template{ file="./tpl/CC_category.tpl" }
- end.
-
-%%%%%% Default %%%%%%
-
-list()->
- Data = db:get_categories(),
- Map = #category{name=name@text, id=name@url},
- Body = [#link{ id=name },
- #br{}],
- #bind{ data = Data, map = Map, body = Body}.
-
-listProducts() ->
- Data = db:get_product_list(wf:state(cat_id)),
- wf:state(product_cache,Data),
- Map = #product{name=name@text, url=name@url},
- Body = [ #link{id=name},
- #br{}],
- #bind{ data = Data, map = Map, body = Body}.
-
-%%%%%% Custom %%%%%%
-
-list(Template) ->
- File = "./wwwroot/"++Template,
- Data = db:get_categories(),
-
- Render = fun(Category) ->
- wf:state(category,Category),
- wf:state(template_was_called,false),
- wf:render(#template{ file=File })
- end,
-[Render(X)||X<-Data].
-
-listProducts(Template) ->
- File = "./wwwroot/" ++ Template,
- Data = db:get_product_list(id()),
- wf:state(prod_cache,Data),
-
- Render = fun(Product) ->
- product:load(Product),
- wf:state(template_was_called,false),
- wf:render(#template{ file=File })
- end,
-
- [Render(X)||X<-Data].
-
-%%%%%% Field Access Functions %%%%%%
-
-id() ->
- (?CATEGORY)#category.id.
-
-name() ->
- (?CATEGORY)#category.name.
-
-url() ->
- "./"++id().
-
-event(Event) ->
- ?PRINT(Event).
View
@@ -1,16 +0,0 @@
--module (cookieCart).
--compile (export_all).
-
--include_lib ("nitrogen/include/wf.inc").
-
-
-categories() ->
- "categories".
-
-template(Template) ->
- %% Hacka-dodel-doo
- wf:state(template_was_called,false),
- #template { file="./wwwroot/" ++ Template }.
-
-event(Event) ->
- ?PRINT(Event).
View
@@ -1,72 +0,0 @@
--module (cookieCart_app).
--include_lib ("nitrogen/include/wf.inc").
--compile(export_all).
-
-%%% APPLICATION FUNCTIONS %%%
-
-start(_, _) ->
- %db:start(),
- nitrogen:start().
-stop(_) -> nitrogen:stop().
-
-%% route/1 lets you define new URL routes to your web pages,
-%% or completely create a new routing scheme.
-%% The 'Path' argument specifies the request path. Your
-%% function should return either an atom which is the page module
-%% to run, or a tuple containing {Module, PathInfo}. PathInfo
-%% can be accessed using wf:get_path_info().
-%%
-%% Uncomment the line below to direct requests
-%% from "/web/newroute" to the web_index module:
-%%
-%% route("/web/newroute") -> web_index;
-%%
-%% Uncomment the line below to direct requests
-%% from "/web/newroute" to the web_index module,
-%% with trailing PathInfo included:
-%%
-%% route("/web/newroute/" ++ PathInfo) -> {web_index, PathInfo};
-
-route("/web/" ++ PathInfo) -> {web_index,PathInfo};
-route(Path) -> nitrogen:route(Path).
-
-
-%% request/1 is executed before every Nitrogen page, and lets
-%% you add authentication and authorization. The 'Module' argument
-%% is the name of the page module.
-%% This function should return either 'ok' if processing can proceed,
-%% or it can return a full-fledged page by treating it just like the main function
-%% of a page. Alternatively, you can use the wf:redirect* functions to
-%% issue a client-side redirect to a new page.
-
-
-request(Module) ->
- ProtectedPages = [web_postwork,web_PM,web_privchat],
-
- case lists:member(Module,ProtectedPages) of
- false ->
- nitrogen:request(Module);
- true ->
- case wf:user() of
- undefined ->
- wf:redirect_to_login("/web/login");
- _->
- ok
- end
- end.
-
-%request(Module) ->
-% nitrogen:request(Module).
-
-% Code for testing basic authentication:
-%
-% request(Module) ->
-% wf_http_basic_auth:run(Module, ?MODULE).
-%
-% realm() -> "nitrogen".
-%
-% is_authenticated(Module, _) -> false.
-%
-% authenticate(Module, User, Password) ->
-% User == "Rusty" andalso Password == "password".ubuntu@ubuntu:/usr/local/lib/erlang/lib/nitrogen-master/Quickstart/src$
-
Oops, something went wrong.

0 comments on commit 3dd150e

Please sign in to comment.