Fixed timeline creation, API logical fixes.
* Resolved issues: * #0006: Fix timeline menu UI. * #0015: Create new timeline button in timeline menu. * #0017: Implement timeline creation. * Removed ts_api:post_timeline/3, corresponded to `POST` to `/ts_api/timelines/<user-id>`, which makes no sense as there is no way to auto-generate new timeline-ids. * Changed ts_api:put_timeline/3 to use ts_timeline:write. This resolved #0017. * Changed ts_api:put_entry/4 to use ts_entry:write. * Implemented ts_entry:write/1 and ts_timeline:write/1. These functions are simple passthroughs to mnesia:dirty_write/1. * Added clarifing documentation for ts_json:record_to_ejson JSON formats. * Renamed ts_json:ejson_to_record/2 to ts_json:ejson_to_record_strict/2 and added ts_json:ejson_to_record/2 as a lax version of the same. * Updated all ts_api functions to use updated ts_json methods.
This commit is contained in:
@ -1,10 +1,17 @@
|
||||
-module(ts_json).
|
||||
-export([encode_record/1, record_to_ejson/1, ejson_to_record/2]).
|
||||
-export([encode_record/1, record_to_ejson/1, ejson_to_record/2, ejson_to_record_strict/2]).
|
||||
|
||||
-include("ts_db_records.hrl").
|
||||
|
||||
encode_record(Record) -> lists:flatten(json:encode(record_to_ejson(Record))).
|
||||
|
||||
% User JSON record structure:
|
||||
% {"id": "john_doe",
|
||||
% "name": "John Doe",
|
||||
% "email": "john.doe@example.com",
|
||||
% "join_date": "2011-01-01T12:00.000Z",
|
||||
% "ext_data": {"last_timeline", "personal"}}
|
||||
|
||||
record_to_ejson(Record=#ts_user{}) ->
|
||||
{struct, [
|
||||
{id, Record#ts_user.username},
|
||||
@ -13,6 +20,12 @@ record_to_ejson(Record=#ts_user{}) ->
|
||||
{join_date, encode_datetime(Record#ts_user.join_date)},
|
||||
{ext_data, Record#ts_user.ext_data}]};
|
||||
|
||||
% Timeline JSON record stucture:
|
||||
% {"user_id": "john_doe",
|
||||
% "id": "personal",
|
||||
% "created": "2011-01-01T14:00.000Z",
|
||||
% "description:"Personal time-tracking."}
|
||||
|
||||
record_to_ejson(Record=#ts_timeline{}) ->
|
||||
% pull out the username and timeline id
|
||||
{Username, TimelineId} = Record#ts_timeline.ref,
|
||||
@ -24,6 +37,14 @@ record_to_ejson(Record=#ts_timeline{}) ->
|
||||
{created, encode_datetime(Record#ts_timeline.created)},
|
||||
{description, Record#ts_timeline.desc}]};
|
||||
|
||||
% Entry JSON record structure:
|
||||
% {"user_id": "john_doe",
|
||||
% "timeline_id": "personal",
|
||||
% "id": "1",
|
||||
% "timestamp": "2011-01-01T14:01.000Z",
|
||||
% "mark": "Workout.",
|
||||
% "notes": "First workout after a long break."}
|
||||
|
||||
record_to_ejson(Record=#ts_entry{}) ->
|
||||
% pull out the username, timeline id, and entry id
|
||||
{Username, TimelineId, EntryId} = Record#ts_entry.ref,
|
||||
@ -44,19 +65,58 @@ 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.0B.~3.10.0BZ",
|
||||
[Year, Month, Day, Hour, Minute, Second, 000])).
|
||||
|
||||
ejson_to_record(_Empty=#ts_timeline{}, EJSON) ->
|
||||
ejson_to_record(Rec=#ts_timeline{}, EJSON) ->
|
||||
{struct, Fields} = EJSON,
|
||||
|
||||
#ts_timeline{
|
||||
ref = {undefined, undefined},
|
||||
Created = case lists:keyfind(created, 1, Fields) of
|
||||
false -> undefined;
|
||||
{created, CreatedVal} -> decode_datetime(CreatedVal)
|
||||
end,
|
||||
|
||||
Desc = case lists:keyfind(description, 1, Fields) of
|
||||
false -> undefined;
|
||||
{description, DescVal} -> DescVal
|
||||
end,
|
||||
|
||||
Rec#ts_timeline{
|
||||
created = Created,
|
||||
desc = Desc };
|
||||
|
||||
ejson_to_record(Rec=#ts_entry{}, EJSON) ->
|
||||
{struct, Fields} = EJSON,
|
||||
|
||||
Timestamp = case lists:keyfind(timestamp, 1, Fields) of
|
||||
false -> undefined;
|
||||
{timestamp, TSVal} -> calendar:datetime_to_gregorian_seconds(
|
||||
decode_datetime(TSVal))
|
||||
end,
|
||||
|
||||
Mark = case lists:keyfind(mark, 1, Fields) of
|
||||
false -> undefined;
|
||||
{mark, MarkVal} -> MarkVal
|
||||
end,
|
||||
|
||||
Notes = case lists:keyfind(notes, 1, Fields) of
|
||||
false -> undefined;
|
||||
{notes, NotesVal} -> NotesVal
|
||||
end,
|
||||
|
||||
Rec#ts_entry{
|
||||
timestamp = Timestamp,
|
||||
mark = Mark,
|
||||
notes = Notes}.
|
||||
|
||||
ejson_to_record_strict(Rec=#ts_timeline{}, EJSON) ->
|
||||
{struct, Fields} = EJSON,
|
||||
|
||||
Rec#ts_timeline{
|
||||
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_strict(Rec=#ts_entry{}, EJSON) ->
|
||||
{struct, Fields} = EJSON,
|
||||
|
||||
#ts_entry{
|
||||
ref = {undefined, undefined, undefined},
|
||||
Rec#ts_entry{
|
||||
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