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:
@ -5,26 +5,36 @@
|
||||
|
||||
encode_record(Record) -> lists:flatten(json:encode(record_to_ejson(Record))).
|
||||
|
||||
record_to_ejson(Record=#ts_user{}) ->
|
||||
{struct, [
|
||||
{username, atom_to_list(Record#ts_user.username)},
|
||||
{name, Record#ts_user.name},
|
||||
{email, Record#ts_user.email},
|
||||
{join_date, encode_datetime(Record#ts_user.join_date)}]};
|
||||
|
||||
record_to_ejson(Record=#ts_timeline{}) ->
|
||||
% pull out the timeline id
|
||||
{_Username, TimelineId} = Record#ts_timeline.ref,
|
||||
% pull out the username and timeline id
|
||||
{Username, TimelineId} = Record#ts_timeline.ref,
|
||||
|
||||
% create the EJSON struct
|
||||
{struct, [
|
||||
{id, atom_to_list(TimelineId)},
|
||||
{username, atom_to_list(Username)},
|
||||
{timeline_id, atom_to_list(TimelineId)},
|
||||
{created, encode_datetime(Record#ts_timeline.created)},
|
||||
{description, Record#ts_timeline.desc}]};
|
||||
|
||||
record_to_ejson(Record=#ts_entry{}) ->
|
||||
% pull out the entry id
|
||||
{_Username, _TimelineId, EntryId} = Record#ts_entry.ref,
|
||||
% pull out the username, timeline id, and entry id
|
||||
{Username, TimelineId, EntryId} = Record#ts_entry.ref,
|
||||
|
||||
% convert the timestamp to a date-time
|
||||
DateTime = calendar:gregorian_seconds_to_datetime(Record#ts_entry.timestamp),
|
||||
|
||||
% create the EJSON struct
|
||||
{struct, [
|
||||
{id, EntryId},
|
||||
{username, Username},
|
||||
{timeline_id, TimelineId},
|
||||
{entry_id, EntryId},
|
||||
{timestamp, encode_datetime(DateTime)},
|
||||
{mark, Record#ts_entry.mark},
|
||||
{notes, Record#ts_entry.notes}]}.
|
||||
@ -33,21 +43,40 @@ encode_datetime({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
lists:flatten(io_lib:format("~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0BZ",
|
||||
[Year, Month, Day, Hour, Minute, Second])).
|
||||
|
||||
ejson_to_record(Empty=#ts_timeline{}, EJSON) ->
|
||||
ejson_to_record(_Empty=#ts_user{}, EJSON) ->
|
||||
{struct, Fields} = EJSON,
|
||||
{Username, _} = Empty#ts_timeline.ref,
|
||||
|
||||
Pwd = case lists:keyfind(password, 1, Fields) of
|
||||
false -> uninit; Field -> element(2, Field) end,
|
||||
|
||||
#ts_user{
|
||||
username = element(2, lists:keyfind(username, 1, Fields)),
|
||||
pwd = Pwd,
|
||||
pwd_salt = uninit,
|
||||
name = element(2, lists:keyfind(name, 1, Fields)),
|
||||
email = element(2, lists:keyfind(email, 1, Fields)),
|
||||
join_date = decode_datetime(
|
||||
element(2, lists:keyfind(join_date, 1, Fields)))};
|
||||
|
||||
ejson_to_record(_Empty=#ts_timeline{}, EJSON) ->
|
||||
% The JSON records do not have username information
|
||||
{struct, Fields} = EJSON,
|
||||
Username = element(2, lists:keyfind(username, 1, Fields)),
|
||||
TimelineId = element(2, lists:keyfind(timeline_id, 1, Fields)),
|
||||
|
||||
#ts_timeline{
|
||||
ref = {Username, element(2, lists:keyfind(id, 1, Fields))},
|
||||
ref = {Username, TimelineId},
|
||||
created = decode_datetime(element(2, lists:keyfind(created, 1, Fields))),
|
||||
desc = element(2, lists:keyfind(description, 1, Fields))};
|
||||
|
||||
ejson_to_record(Empty=#ts_entry{}, EJSON) ->
|
||||
ejson_to_record(_Empty=#ts_entry{}, EJSON) ->
|
||||
{struct, Fields} = EJSON,
|
||||
{Username, TimelineId, _} = Empty#ts_entry.ref,
|
||||
Username = element(2, lists:keyfind(username, 1, Fields)),
|
||||
TimelineId = element(2, lists:keyfind(timeline_id, 1, Fields)),
|
||||
EntryId = element(2, lists:keyfind(entry_id, 1, Fields)),
|
||||
|
||||
#ts_entry{
|
||||
ref = {Username, TimelineId, element(2, lists:keyfind(id, 1, Fields))},
|
||||
ref = {Username, TimelineId, EntryId},
|
||||
timestamp = calendar:datetime_to_gregorian_seconds(decode_datetime(
|
||||
element(2, lists:keyfind(timestamp, 1, Fields)))),
|
||||
mark = element(2, lists:keyfind(mark, 1, Fields)),
|
||||
|
Reference in New Issue
Block a user