fix model

This commit is contained in:
安礼成 2023-02-23 20:22:55 +08:00
parent e5731cada3
commit 70386fbed2
4 changed files with 47 additions and 7 deletions

View File

@ -16,7 +16,7 @@
%% helper methods %% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handle_request(_, "/api/booking", _, _Params) -> handle_request("GET", "/api/booking", _, _Params) ->
{ok, 200, iot_util:json_data(<<"success">>)}; {ok, 200, iot_util:json_data(<<"success">>)};
handle_request("POST", "/api/booking", _, _Params) -> handle_request("POST", "/api/booking", _, _Params) ->
{ok, 200, iot_util:json_data(<<"success">>)}. {ok, 200, iot_util:json_data(<<"success">>)}.

View File

@ -16,8 +16,34 @@
%% helper methods %% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handle_request(_, "/api/booking", _, _Params) -> handle_request(_, "/host/list", Params, _) ->
{ok, 200, iot_util:json_data(<<"success">>)}; Page0 = maps:get(<<"page">>, Params, <<"1">>),
Size0 = maps:get(<<"size">>, Params, <<"10">>),
Page = binary_to_integer(Page0),
Size = binary_to_integer(Size0),
true = Page > 0 andalso Size > 0,
Start = (Page - 1) * Size,
case host_model:get_hosts(Start, Size) of
{ok, Hosts, Stat} ->
Response = #{
<<"hosts">> => lists:map(fun(Host) -> host_model:to_map(Host) end, Hosts),
<<"stat">> => lists:map(fun({Status, Num}) ->
#{
<<"status">> => Status,
<<"num">> => Num
}
end, maps:to_list(Stat))
},
lager:debug("resp is: ~p", [Response]),
{ok, 200, iot_util:json_data(Response)};
{error, Reason} ->
lager:warning("[host_handler] get a error: ~p", [Reason]),
{ok, 200, iot_util:json_error(404, <<"database error">>)}
end;
handle_request("POST", "/host/update", _, _Params) -> handle_request("POST", "/host/update", _, _Params) ->
lager:debug("[host_handler] post params is: ~p", [_Params]), lager:debug("[host_handler] post params is: ~p", [_Params]),

View File

@ -29,7 +29,7 @@ init_database() ->
{attributes, record_info(fields, host)}, {attributes, record_info(fields, host)},
{record_name, host}, {record_name, host},
{disc_copies, [node()]}, {disc_copies, [node()]},
{type, set} {type, ordered_set}
]), ]),
%% %%
@ -37,7 +37,7 @@ init_database() ->
{attributes, record_info(fields, terminal)}, {attributes, record_info(fields, terminal)},
{record_name, terminal}, {record_name, terminal},
{disc_copies, [node()]}, {disc_copies, [node()]},
{type, set} {type, ordered_set}
]), ]),
%% %%
@ -45,7 +45,7 @@ init_database() ->
{attributes, record_info(fields, service)}, {attributes, record_info(fields, service)},
{record_name, service}, {record_name, service},
{disc_copies, [node()]}, {disc_copies, [node()]},
{type, set} {type, ordered_set}
]), ]),
ok. ok.

View File

@ -13,6 +13,7 @@
%% API %% API
-export([get_host/1, get_hosts/2, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3]). -export([get_host/1, get_hosts/2, add_host/1, change_status/2, delete/1, table_size/0, find_hosts/3]).
-export([to_map/1]).
get_host(HostId) when is_binary(HostId) -> get_host(HostId) when is_binary(HostId) ->
case mnesia:dirty_read(host, HostId) of case mnesia:dirty_read(host, HostId) of
@ -113,4 +114,17 @@ table_size() ->
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% helper methods %% helper methods
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
to_map(#host{host_id = HostId, name = Name, model = Model, cell_id = CellId, activated_ts = ActivatedTs,
update_ts = UpdateTs, status = Status}) ->
#{
<<"host_id">> => HostId,
<<"name">> => Name,
<<"model">> => Model,
<<"cell_id">> => CellId,
<<"activated_ts">> => ActivatedTs,
<<"update_ts">> => UpdateTs,
<<"status">> => Status
}.