-module (product, [
id,
Name :: string ()
Price :: float ()
Description :: string ()]).
-compile (export_all). new (id, Req: post_param ("name"), Req: post_param ("price"), Req: post_param ("description")). -module(utils_lib).
-export([record_replace/2]).
record_replace(Model,Req) ->
Attributes = [X || X <- boss_record_lib:attribute_names(Model),X =/= id],
AttributeTypes = boss_record_lib:attribute_types(Model),
Data = lists:map(fun(X) ->
Param = Req:post_param(atom_to_list(X)),
Value = case proplists:get_value(X,AttributeTypes) of
string -> Param;
integer -> try list_to_integer(Param) of Integer -> Integer catch _:_ -> Param end;
float -> try list_to_float(Param) of Float -> Float catch _:_ -> Param end;
_ -> Param
end,
{X,Value}
end,
Attributes),
[X || X <- Data, proplists:get_value(list_to_atom(lists:nth(1,io_lib:format("~s",lists:map(fun(Key) -> Key end, proplists:get_keys([X]))))),[X]) =/= []].product_create(Method, []) ->
Model = product,
case Method of
'GET' ->
{render_other, [{action, "product/product_create"}]};
'POST' ->
Data = utils_lib:record_replace(Model,Req),
Record = boss_record:new(Model,Data),
case Record:save() of
{ok, SavedRecord} ->
{redirect, [{action, "product_list"}]};
{error, Errors} ->
{render_other, [{action, "product/product_create"}], [{errors, Errors}, {record, Record}]}
end
end.