Bugfixing, compile-error-sqhashung.

This commit is contained in:
Jonathan Bernard 2011-06-15 00:54:58 -05:00
parent 7d11112226
commit 658091e947
6 changed files with 50 additions and 22 deletions

View File

@ -201,13 +201,24 @@ do_logout(YArg) ->
{status, 200}. {status, 200}.
get_user_summary(YArg, Username) -> get_user_summary(YArg, Username) ->
% find user record
case ts_user:lookup(Username) of case ts_user:lookup(Username) of
% no user record, barf
no_record -> make_json_404(YArg); no_record -> make_json_404(YArg);
% found user record, let us build the return
User -> User ->
UserExtData - ts_ext_data:get_properties(Username),
% get user extended data properties
UserExtData = ts_ext_data:get_properties(Username),
% convert to intermediate JSON form
EJSONUser = ts_json:record_to_ejson(User, UserExtData), EJSONUser = ts_json:record_to_ejson(User, UserExtData),
% get the user's timelins
Timelines = ts_timeline:list(Username, 0, 100), Timelines = ts_timeline:list(Username, 0, 100),
% get each timeline's extended data and convert to EJSON
EJSONTimelines = {array, EJSONTimelines = {array,
lists:map( lists:map(
fun(Timeline) -> fun(Timeline) ->
@ -216,17 +227,22 @@ get_user_summary(YArg, Username) ->
end, end,
Timelines)}, Timelines)},
% convert to JSON
JSONResp = json:encode({struct, JSONResp = json:encode({struct,
[{user, EJSONUser}, [{user, EJSONUser},
{timelines, EJSONTimelines} {timelines, EJSONTimelines}
]}), ]}),
% write response out
{content, "application/json", JSONResp} {content, "application/json", JSONResp}
end. end.
get_user(YArg, Username) -> get_user(YArg, Username) ->
% find the user record
case ts_user:lookup(Username) of case ts_user:lookup(Username) of
% no such user, barf
no_record -> make_json_404(YArg); no_record -> make_json_404(YArg);
% found, return a 200 with the record
User -> make_json_200(YArg, User) User -> make_json_200(YArg, User)
end. end.
@ -250,7 +266,7 @@ list_timelines(YArg, Username) ->
% list the timelines from the database % list the timelines from the database
Timelines = ts_timeline:list(Username, Start, Length), Timelines = ts_timeline:list(Username, Start, Length),
% convert them all to their EJSON form % convert them all to their EJSON form, adding in extended data for each
EJSONTimelines = {array, lists:map( EJSONTimelines = {array, lists:map(
fun (Timeline) -> fun (Timeline) ->
ts_json:record_to_ejson(Timeline, ts_json:record_to_ejson(Timeline,
@ -258,7 +274,7 @@ list_timelines(YArg, Username) ->
end, end,
Timelines)}, Timelines)},
% create resposne % convert to JSON and create resposne
JSONResponse = json:encode(EJSONTimelines), JSONResponse = json:encode(EJSONTimelines),
% return response % return response
@ -282,12 +298,16 @@ put_timeline(YArg, Username, TimelineId) ->
{TR, ExtData} = {TR, ExtData} =
try ts_json:ejson_to_record_strict( try ts_json:ejson_to_record_strict(
#ts_timeline{ref={Username, TimelineId}}, EJSON) #ts_timeline{ref={Username, TimelineId}}, EJSON)
% we can not parse it, tell the user
catch throw:{InputError, _StackTrace} -> catch throw:{InputError, _StackTrace} ->
error_logger:error_report("Bad input: ~p", [InputError]), error_logger:error_report("Bad input: ~p", [InputError]),
throw(make_json_400(YArg, {request_error, InputError)) throw(make_json_400(YArg, {request_error, InputError}))
end, end,
ts_timeline:write(TR), % write the changes.
ts_timeline:write(TR, ExtData),
% return a 200
make_json_200(YArg, TR). make_json_200(YArg, TR).
delete_timeline(_YArg, _Username, _TimelineId) -> {status, 405}. delete_timeline(_YArg, _Username, _TimelineId) -> {status, 405}.
@ -335,7 +355,7 @@ list_entries(YArg, Username, TimelineId) ->
EJSONEntries = {array, lists:map( EJSONEntries = {array, lists:map(
fun (Entry) -> fun (Entry) ->
ts_json:record_to_ejson(Entry, ts_json:record_to_ejson(Entry,
ts_ext_data:get_properties(Entry#ts_entry.ref)), ts_ext_data:get_properties(Entry#ts_entry.ref))
end, end,
Entries)}, Entries)},
@ -371,7 +391,7 @@ list_entries(YArg, Username, TimelineId) ->
EJSONEntries = {array, lists:map( EJSONEntries = {array, lists:map(
fun (Entry) -> fun (Entry) ->
ts_json:record_to_ejson(Entry, ts_json:record_to_ejson(Entry,
ts_ext_data:get_properties(Entry#ts_entry.ref)), ts_ext_data:get_properties(Entry#ts_entry.ref))
end, end,
Entries)}, Entries)},

View File

@ -41,7 +41,7 @@ update(Record, ExtData) when is_list(ExtData) ->
case do_update(Record) of case do_update(Record) of
{error, Err} -> mnesia:abort({"Cannot update record.", Err}); {error, Err} -> mnesia:abort({"Cannot update record.", Err});
UpdatedRecord -> case do_set_ext_data(Record, ExtData) of UpdatedRecord -> case do_set_ext_data(Record, ExtData) of
of -> UpdatedRecord; ok -> UpdatedRecord;
Error -> mnesia:abort({"Cannot update record.", Error}) Error -> mnesia:abort({"Cannot update record.", Error})
end end
end end
@ -105,7 +105,7 @@ do_set_ext_data(Record, ExtData) when is_list(ExtData) ->
fun({Key, Val}) -> fun({Key, Val}) ->
{atomic, ok} = ts_ext_data:set_property(Ref, Key, Val) {atomic, ok} = ts_ext_data:set_property(Ref, Key, Val)
end, end,
ExtData) ExtData),
ok. ok.
% This is somewhat ridiculous. % This is somewhat ridiculous.

View File

@ -22,7 +22,7 @@ set_property(Ref=#ts_timeline{}, entry_exclusions, ExclusionList) ->
set_property(Ref, Key, Value) -> set_property(Ref, Key, Value) ->
throw(io_lib:format("Property '~s' not available for a ~s record.", throw(io_lib:format("Property '~s' not available for a ~s record.",
[Key, element(1, Ref)]). [Key, element(1, Ref)])).
get_property(Ref, PropKey) -> get_property(Ref, PropKey) ->
{atomic, Result} = mnesia:transaction(fun() -> {atomic, Result} = mnesia:transaction(fun() ->
@ -42,6 +42,6 @@ get_properties(Ref) ->
do_set_property(Ref, PropKey, Val) -> do_set_property(Ref, PropKey, Val) ->
{atomic, Result} = mnesia:transaction(fun() -> {atomic, Result} = mnesia:transaction(fun() ->
mnesia:write(#ts_ext_data{ref = {Ref, PropKey}, value = Val}). mnesia:write(#ts_ext_data{ref = {Ref, PropKey}, value = Val})
end), end),
Result. Result.

View File

@ -1,11 +1,11 @@
-module(ts_json). -module(ts_json).
-export([encode_record/1, record_to_ejson/2, -export([encode_record/2, record_to_ejson/2,
ejson_to_record/2, ejson_to_record/3, ejson_to_record/2, ejson_to_record/3,
ejson_to_record_strict/2, ejson_to_record_strict/3]). ejson_to_record_strict/2, ejson_to_record_strict/3]).
-include("ts_db_records.hrl"). -include("ts_db_records.hrl").
encode_record(Record) -> lists:flatten(json:encode(record_to_ejson(Record))). encode_record(Record, ExtData) -> lists:flatten(json:encode(record_to_ejson(Record, ExtData))).
% User JSON record required structure: % User JSON record required structure:
% {"id": "john_doe", % {"id": "john_doe",
@ -110,12 +110,12 @@ ejson_to_record_strict(Empty, Ref, EJSON) ->
construct_record(Timeline=#ts_timeline{}, [{Key, Val}|Fields], ExtData) -> construct_record(Timeline=#ts_timeline{}, [{Key, Val}|Fields], ExtData) ->
case Key of case Key of
created -> construct_record( created -> construct_record(
Timeline#ts_timeline{created = decode_datetime(Value)}, Timeline#ts_timeline{created = decode_datetime(Val)},
Fields, ExtData); Fields, ExtData);
description -> construct_record(Timeline#ts_timeline{desc = Value}, description -> construct_record(Timeline#ts_timeline{desc = Val},
Fields, ExtData); Fields, ExtData);
Other -> _Other ->
ExtDataProp = ejson_to_ext_data({Key, Value}), ExtDataProp = ejson_to_ext_data({Key, Val}),
construct_record(Timeline, Fields, [ExtDataProp|ExtData]) construct_record(Timeline, Fields, [ExtDataProp|ExtData])
end; end;
@ -126,8 +126,8 @@ construct_record(Entry=#ts_entry{}, [{Key, Value}|Fields], ExtData) ->
decode_datetime(Value))}, decode_datetime(Value))},
Fields, ExtData); Fields, ExtData);
mark -> construct_record(Entry#ts_entry{mark=Value}, Fields, ExtData); mark -> construct_record(Entry#ts_entry{mark=Value}, Fields, ExtData);
notes -> construct_record(Entry#ts_entry{notes=Vale}, Fields, ExtData); notes -> construct_record(Entry#ts_entry{notes=Value}, Fields, ExtData);
Other -> _Other ->
ExtDataProp = ejson_to_ext_data({Key, Value}), ExtDataProp = ejson_to_ext_data({Key, Value}),
construct_record(Entry, Fields, [ExtDataProp|ExtData]) construct_record(Entry, Fields, [ExtDataProp|ExtData])
end; end;

View File

@ -1,5 +1,6 @@
-module(ts_timeline). -module(ts_timeline).
-export([create_table/1, new/1, new/2, update/1, update/2, write/1, lookup/2, list/3]). -export([create_table/1, new/1, new/2, update/1, update/2, write/1, write/2,
lookup/2, list/3]).
-include("ts_db_records.hrl"). -include("ts_db_records.hrl").
-include_lib("stdlib/include/qlc.hrl"). -include_lib("stdlib/include/qlc.hrl").
@ -21,6 +22,13 @@ update(TR = #ts_timeline{}, ExtData) when is_list(ExtData) ->
write(TR = #ts_timeline{}) -> mnesia:dirty_write(TR). write(TR = #ts_timeline{}) -> mnesia:dirty_write(TR).
write(TR = #ts_timeline{}, ExtData) ->
{atomic, Result} = mnesia:transaction(fun() ->
ok = mnesia:write(TR),
ok = ts_common:do_set_ext_data(TR, ExtData)
end),
Result.
lookup(Username, TimelineId) -> lookup(Username, TimelineId) ->
case mnesia:dirty_read(ts_timeline, {Username, TimelineId}) of case mnesia:dirty_read(ts_timeline, {Username, TimelineId}) of
[] -> no_record; [] -> no_record;
@ -29,5 +37,5 @@ lookup(Username, TimelineId) ->
list(Username, Start, Length) -> list(Username, Start, Length) ->
MatchHead = #ts_timeline{ref = {Username, '_'}, _='_'}, MatchHead = #ts_timeline{ref = {Username, '_'}, _='_'},
mnesia:dirty_select(ts_timeline, [{MatchHead, [], ['$_']}]), Timelines = mnesia:dirty_select(ts_timeline, [{MatchHead, [], ['$_']}]),
lists:sublist(Timelines, Start + 1, Length). lists:sublist(Timelines, Start + 1, Length).

View File

@ -35,7 +35,7 @@ update(UR = #ts_user{}) ->
update(UR = #ts_user{}, ExtData) -> update(UR = #ts_user{}, ExtData) ->
{atomic, Result} = mnesia:transaction(fun() -> {atomic, Result} = mnesia:transaction(fun() ->
UpdatedUser = do_update(UR), UpdatedUser = do_update(UR),
ts_common:do_set_ext_data(UR, ExtData) ts_common:do_set_ext_data(UR, ExtData),
UpdatedUser end), UpdatedUser end),
Result. Result.