Added user_summary to api.

Added ts_api:dispatch_app/3
Added ts_api:get_user_summary/2
Fixed compile errors in ts_user.
Added comments.
This commit is contained in:
Jonathan Bernard
2011-02-24 07:29:30 -06:00
parent 1a3c0d5c4e
commit 4492f87a39
6 changed files with 66 additions and 27 deletions

View File

@ -14,7 +14,7 @@ out(YArg) ->
% split the path
PathElements = case PathString of
undefined -> []; %handle no end slash: /ts_api
_Any -> re:split(PathString, "/", [{return, list}])
_Any -> string:tokens(PathString, "/")
end,
% process the request
@ -43,12 +43,37 @@ dispatch_request(YArg, Session, [H|T]) ->
{not_logged_in, _} -> make_json_401(YArg);
{session_expired, _} -> make_json_401(YArg, [{status, "session expired"}]);
{_S, app} -> dispatch_app(YArg, Session, T);
{_S, users} -> dispatch_user(YArg, Session, T);
{_S, timelines} -> dispatch_timeline(YArg, Session, T);
{_S, entries} -> dispatch_entry(YArg, Session, T);
{_S, _Other} -> make_json_404(YArg, [{see_docs, "/ts_api_doc/"}])
end.
% -------- Dispatch for /app -------- %
dispatch_app(YArg, Session, Params) ->
HTTPMethod = (YArg#arg.req)#http_request.method,
case {HTTPMethod, Params} of
{'GET', ["user_summary", UsernameStr]} ->
case {Session#ts_api_session.username,
path_element_to_atom(UsernameStr)} of
{Username, Username} -> get_user_summary(YArg, Username);
_ -> make_json_401(YArg)
end;
{_BadMethod, ["user_summary", _UsernameStr]} ->
make_json_405(YArg, [{see_docs, "/ts_api_docs/app.html"}]);
_Other -> make_json_404(YArg, [{see_docs, "/ts_api_docs/app.html"}])
end.
% -------- Dispatch for /user -------- %
dispatch_user(YArg, _Session, []) ->
make_json_404(YArg, [{see_docs, "/ts_api_doc/"}]);
@ -67,6 +92,8 @@ dispatch_user(YArg, Session, [H]) ->
_Other -> make_json_401(YArg, [{see_docs, "/ts_api_doc/users.html"}])
end.
% -------- Dispatch for /timeline -------- %
dispatch_timeline(YArg, _Session, []) ->
make_json_404(YArg, [{see_docs, "/ts_api_doc/timelines.html"}]);
@ -104,6 +131,8 @@ dispatch_timeline(YArg, [UrlUsername, UrlTimelineId]) ->
dispatch_timeline(YArg, _Other) ->
make_json_404(YArg, [{see_docs, "/ts_api_doc/timelines.html"}]).
% -------- Dispatch for /entry -------- %
dispatch_entry(YArg, _Session, []) ->
make_json_404(YArg, [{see_docs, "/ts_aip_doc/entries.html"}]);
@ -183,6 +212,25 @@ do_logout(YArg) ->
CookieVal = yaws_api:find_cookie_val("ts_api_session", Cookie),
ts_api_session:logout(CookieVal).
get_user_summary(YArg, Username) ->
case ts_user:lookup(Username) of
no_record -> make_json_404(YArg);
User ->
EJSONUser = ts_json:record_to_ejson(User),
Timelines = ts_timeline:list(Username, 0, 100),
EJSONTimelines = {array,
lists:map(fun ts_json:record_to_ejson/1, Timelines)},
JSONResp = json:encode({struct,
[{status, "ok"},
{user, EJSONUser},
{timelines, EJSONTimelines}
]}),
{content, "application/json", JSONResp}
end.
get_user(YArg, Username) ->
case ts_user:lookup(Username) of
no_record -> make_json_404(YArg);

View File

@ -22,10 +22,10 @@ update(UR = #ts_user{}) ->
[] -> no_record;
[Record] ->
UpdatedRecord = ts_common:update_record(Record, UR),
HashedRecord = case UR#ts_user.password of
HashedRecord = case UR#ts_user.pwd of
undefined -> UpdatedRecord;
_Password -> hash_input_record(UpdatedRecord)
end.
end,
mnesia:dirty_write(HashedRecord)
end.