Implemented ts_api:get_user/2, ts_api:put_user/1, and ts_api:post_user/1.

Created a utility function for OK results, ts_api:make_json_200/2
Created ts_common:new/1 and ts_common:update/1 to generalize record creation and update.
Refactored ts_timeline:new/1 and ts_timeline:update/1 to use the ts_common functions.
Implemented ts_json:record_to_ejson/1 and ts_json:ejson_to_record/1 for ts_user records.
Implemented ts_user module.
This commit is contained in:
Jonathan Bernard
2011-02-03 17:23:40 -06:00
parent 6fe9184c8e
commit 1575e25898
9 changed files with 168 additions and 76 deletions

View File

@ -85,11 +85,50 @@ dispatch_entry_by_date(YArg, Username, Timeline, Params) -> todo.
% ======== IMPLEMENTATION ====== %
% ============================== %
get_user(YArg, Username) -> todo.
get_user(YArg, Username) ->
case ts_user:lookup(Username) of
no_record -> make_json_404(YArg);
User -> make_json_200(YArg, User)
end.
put_user(YArg, Username) -> todo.
put_user(YArg, Username) ->
% parse the request body
{done, {ok, EJSON}, _} = json:decode([], binary_to_list(YArg#arg.clidata)),
post_user(YArg, Username) -> todo.
% parse into a user record
NewRecord = ts_json:ejson_to_record(#ts_user{}, EJSON),
case ts_user:new(NewRecord) of
% record created
ok -> [{status, 201}, make_json_200(YArg, NewRecord)];
% will not create, record exists
{error, {record_exists, ExistingRecord}} ->
JSONReturn = json:encode({struct, [
{status, "username taken"},
{see_docs, "/ts_api_doc/user#PUT"}
]}),
[{status, 409}, {content, "application/json", JSONReturn}];
_Error -> make_json_500(YArg)
end.
post_user(YArg, Username) ->
% parse the POST data
{done, {ok, EJSON}, _} = json:decode([], binary_to_list(YArg#arg.clidata)),
% create the user record
NewRecord = ts_json:ejson_to_record(#ts_user{}, EJSON),
case ts_user:update(NewRecord) of
ok -> make_json_200(YArg, NewRecord);
no_record -> make_json_404(YArg,
[{status, "no_record"}, {see_docs, "/ts_api_doc/user#POST"}]);
_Error -> make_json_500(YArg)
end.
delete_user(YArg, Username) -> todo.
@ -98,13 +137,7 @@ get_timeline(YArg, Username, TimelineId) ->
no_record -> make_json_404(YArg);
Timeline ->
EJSONTimeline = ts_json:record_to_ejson(Timeline),
JSONReturn = json:encode({struct, [
{status, "ok"},
{timeline, EJSONTimeline}
]}),
{content, "application/json", JSONReturn}
Timeline -> make_json_200(YArg, Timeline)
end.
put_timeline(YArg, Username, TimelineId) ->
@ -119,16 +152,7 @@ put_timeline(YArg, Username, TimelineId) ->
% insert into the database
case ts_timeline:new(NewRecord) of
% record created
ok ->
EJSONRec = ts_json:record_to_ejson(NewRecord),
JSONReturn = json:encode({struct, [
{status, "ok"},
{timeline, EJSONRec}
]}),
% return the new record
[{status, 201}, {content, "application/json", JSONReturn}];
ok -> [{status, 201}, make_json_200(YArg, NewRecord)];
% will not create, record exists
{error, {record_exists, ExistingRecord}} ->
@ -154,18 +178,10 @@ post_timeline(YArg, Username, TimelineId) ->
NewRecord = ts_json:ejson_to_record(EmptyTimeline, EJSON),
case ts_timeline:update(NewRecord) of
ok ->
ok -> make_json_200(YArg, NewRecord);
EJSONRec = ts_json:record_to_ejson(NewRecord),
JSONReturn = json:encode({struct, [
{status, "updated"},
{timeline, EJSONRec}
]}),
{content, "application/json", JSONReturn};
no_record -> make_json_404(YArg, [{status, "no_record"},
{see_docs, "/ts_api_doc/timeline#PSOT"}]);
no_record -> make_json_404(YArg,
[{status, "no_record"}, {see_docs, "/ts_api_doc/timeline#PSOT"}]);
_Error -> make_json_500(YArg)
end.
@ -194,9 +210,7 @@ list_entries(YArg, Username, Timeline) ->
Entries = case SortOrder of
asc -> ts_entry:list_asc({Username, Timeline}, Start, Length);
desc -> ts_entry:list_desc({Username, Timeline}, Start, Length)
end
.
end.
get_entry_by_id(YArg, Username, Timeline, EventId) ->
case ts_entry:lookup(Username, Timeline, EventId) of
@ -205,17 +219,7 @@ get_entry_by_id(YArg, Username, Timeline, EventId) ->
no_record -> make_json_404(YArg);
% record found
Entry ->
% convert to EJSON
EJSONRec = ts_json:record_to_ejson(Entry),
% create response
JSONReturn = json:encode({struct, [
{status, "ok"},
{entry, EJSONRec}
]}),
{content, "application/json", JSONReturn}
Entry -> make_json_200(YArg, Entry)
end.
% ============================== %
@ -226,6 +230,16 @@ get_entry_by_id(YArg, Username, Timeline, EventId) ->
path_element_to_atom(PE) ->
list_to_atom(re:replace(PE, "\\s", "_", [{return, list}])).
%% Create a JSON 200 response.
make_json_200(YArg, Record) ->
EJSONRecord = ts_json:record_to_ejson(Record),
JSONReturn = json:encode({struct, [
{status, "ok"},
{element(1, Record), EJSONRecord}
]}),
{content, "application/json", JSONReturn}.
%% Create a JSON 404 response.
make_json_404(YArg) -> make_json_404(YArg, []).
make_json_404(YArg, Fields) ->