Further updates to page. Small update to internal API.

Internal API update functions now accept partial updates, allowing one to
selectively update only some fields of a particular record.
This commit is contained in:
Jonathan Bernard
2011-02-14 17:16:37 -06:00
parent ddc12cec64
commit 1a3c0d5c4e
6 changed files with 75 additions and 64 deletions

View File

@ -1,5 +1,5 @@
-module(ts_common).
-export([new/1, update/1, list/3, order_datetimes/2]).
-export([new/1, update/1, update_record/2, list/3, order_datetimes/2]).
-include_lib("stdlib/include/qlc.hrl").
@ -25,9 +25,22 @@ update(Record) ->
% record does not exist, cannot update
[] -> no_record;
% record does exist, update
[_ExistingRecord] -> mnesia:dirty_write(Record)
[ExistingRecord] ->
mnesia:dirty_write(update_record(ExistingRecord, Record))
end.
update_record(Record, UpdateData) ->
update_record(tuple_to_list(Record), tuple_to_list(UpdateData), []).
update_record([], [], Updated) -> list_to_tuple(lists:reverse(Updated));
update_record([Field|RecordFields], [FieldReplacement|UpdateData], Acc) ->
UpdatedField = case FieldReplacement of
undefined -> Field;
NewValue -> NewValue
end,
update_record(RecordFields, UpdateData, [UpdatedField|Acc]).
%% list <Length> number of records, skipping the first <Start>
list(Table, Start, Length)
when is_atom(Table) and is_integer(Start) and is_integer(Length) ->

View File

@ -25,7 +25,8 @@ update(ER = #ts_entry{}) ->
% record does not exist
[] -> no_record;
% record exists, update it
[_Record] -> mnesia:dirty_write(ER)
[ExistingRecord] ->
mnesia:dirty_write(ts_common:update_record(ExistingRecord, ER))
end.
lookup(Username, TimelineId, EntryId) ->

View File

@ -47,7 +47,7 @@ ejson_to_record(_Empty=#ts_timeline{}, EJSON) ->
{struct, Fields} = EJSON,
#ts_timeline{
ref = {undef, undef},
ref = {undefined, undefined},
created = decode_datetime(element(2, lists:keyfind(created, 1, Fields))),
desc = element(2, lists:keyfind(description, 1, Fields))};
@ -55,7 +55,7 @@ ejson_to_record(_Empty=#ts_entry{}, EJSON) ->
{struct, Fields} = EJSON,
#ts_entry{
ref = {undef, undef, undef},
ref = {undefined, undefined, undefined},
timestamp = calendar:datetime_to_gregorian_seconds(decode_datetime(
element(2, lists:keyfind(timestamp, 1, Fields)))),
mark = element(2, lists:keyfind(mark, 1, Fields)),