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

Binary file not shown.

Binary file not shown.

View File

@ -1,13 +1,5 @@
/jdbernard -- information about the user 'jdbernard' /ts_api/users/jdbernard
/jdbernard/work -- information about the 'work' timeline /ts_api/timelines/jdbernard/work
/jdbernard/work/list /ts_api/entries/jdbernard/work/1
/jdbernard/work/by_id/13
/jdbernard/work/by_date/2010-11-14
/jdbernard/work/by_date/2010-11-14T12.34.26
/jdbernard/work/since/DATE-REF /ts_api/app/user_summary/jdbernard
/jdbernard/work/since/2010
/jdbernard/work/since/2010/11
/jdbernard/work/since/january
/jdbernard/work/since/monday
/jdbernard/work/since

View File

@ -14,7 +14,7 @@ out(YArg) ->
% split the path % split the path
PathElements = case PathString of PathElements = case PathString of
undefined -> []; %handle no end slash: /ts_api undefined -> []; %handle no end slash: /ts_api
_Any -> re:split(PathString, "/", [{return, list}]) _Any -> string:tokens(PathString, "/")
end, end,
% process the request % process the request
@ -43,12 +43,37 @@ dispatch_request(YArg, Session, [H|T]) ->
{not_logged_in, _} -> make_json_401(YArg); {not_logged_in, _} -> make_json_401(YArg);
{session_expired, _} -> make_json_401(YArg, [{status, "session expired"}]); {session_expired, _} -> make_json_401(YArg, [{status, "session expired"}]);
{_S, app} -> dispatch_app(YArg, Session, T);
{_S, users} -> dispatch_user(YArg, Session, T); {_S, users} -> dispatch_user(YArg, Session, T);
{_S, timelines} -> dispatch_timeline(YArg, Session, T); {_S, timelines} -> dispatch_timeline(YArg, Session, T);
{_S, entries} -> dispatch_entry(YArg, Session, T); {_S, entries} -> dispatch_entry(YArg, Session, T);
{_S, _Other} -> make_json_404(YArg, [{see_docs, "/ts_api_doc/"}]) {_S, _Other} -> make_json_404(YArg, [{see_docs, "/ts_api_doc/"}])
end. 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, []) -> dispatch_user(YArg, _Session, []) ->
make_json_404(YArg, [{see_docs, "/ts_api_doc/"}]); 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"}]) _Other -> make_json_401(YArg, [{see_docs, "/ts_api_doc/users.html"}])
end. end.
% -------- Dispatch for /timeline -------- %
dispatch_timeline(YArg, _Session, []) -> dispatch_timeline(YArg, _Session, []) ->
make_json_404(YArg, [{see_docs, "/ts_api_doc/timelines.html"}]); make_json_404(YArg, [{see_docs, "/ts_api_doc/timelines.html"}]);
@ -104,6 +131,8 @@ dispatch_timeline(YArg, [UrlUsername, UrlTimelineId]) ->
dispatch_timeline(YArg, _Other) -> dispatch_timeline(YArg, _Other) ->
make_json_404(YArg, [{see_docs, "/ts_api_doc/timelines.html"}]). make_json_404(YArg, [{see_docs, "/ts_api_doc/timelines.html"}]).
% -------- Dispatch for /entry -------- %
dispatch_entry(YArg, _Session, []) -> dispatch_entry(YArg, _Session, []) ->
make_json_404(YArg, [{see_docs, "/ts_aip_doc/entries.html"}]); 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), CookieVal = yaws_api:find_cookie_val("ts_api_session", Cookie),
ts_api_session:logout(CookieVal). 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) -> get_user(YArg, Username) ->
case ts_user:lookup(Username) of case ts_user:lookup(Username) of
no_record -> make_json_404(YArg); no_record -> make_json_404(YArg);

View File

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

View File

@ -1,11 +1,18 @@
function login(event) {
}
function logout(event) {
alert("TODO: log user out via AJAX.");
event.preventDefault();
}
function toggleUserInfo(event) { function toggleUserInfo(event) {
$("#user-info").slideToggle("slow"); $("#user-info").slideToggle("slow");
event.preventDefault(); event.preventDefault();
} }
function showChangePwd(event) { function showChangePwd(event) { $("#change-pwd").slideToggle("slow"); }
$("#change-pwd").slideToggle("slow");
}
function updateUser(event) { function updateUser(event) {
alert("TODO: update user via AJAX."); alert("TODO: update user via AJAX.");
@ -27,17 +34,9 @@ function updateTimeline(event) {
event.preventDefault(); event.preventDefault();
} }
function showNewNotes(event) { function showNewNotes(event) { $("#add-notes").slideToggle("slow"); }
$("#add-notes").slideToggle("slow");
}
function newEntry(event) { function newEntry(event) {
alert("TODO: create entry vi AJAX"); alert("TODO: create entry vi AJAX");
event.preventDefault(); event.preventDefault();
} }
function logout(event) {
alert("TODO: log user out via AJAX.");
event.preventDefault();
}