35 lines
1000 B
Erlang
35 lines
1000 B
Erlang
|
-module(ts_timeline).
|
||
|
-export([create_table/1, new/1, update/1, list/3]).
|
||
|
|
||
|
-include("ts_db_records.hrl").
|
||
|
-include_lib("stdlib/include/qlc.hrl").
|
||
|
|
||
|
create_table(TableOpts) ->
|
||
|
mnesia:create_table(ts_timeline,
|
||
|
TableOpts ++ [{attributes, record_info(fields, ts_timeline)},
|
||
|
{type, ordered_set}]).
|
||
|
|
||
|
new(TR = #ts_timeline{}) ->
|
||
|
case mnesia:dirty_read(ts_timeline, TR#ts_timeline.ref) of
|
||
|
[ExistingRecord] -> {error, {record_exists, ExistingRecord}};
|
||
|
[] -> mnesia:dirty_write(TR)
|
||
|
end.
|
||
|
|
||
|
update(TR = #ts_timeline{}) ->
|
||
|
|
||
|
% look for the existing record
|
||
|
case mnesia:dirty_read(ts_timeline, TR#ts_timeline.ref) of
|
||
|
% record does not exist
|
||
|
[] -> no_record;
|
||
|
% record exists, update
|
||
|
[Record] -> mnesia:dirty_write(TR)
|
||
|
end.
|
||
|
|
||
|
list(Username, Start, Length) ->
|
||
|
ts_common:list(
|
||
|
qlc:q([T || T <- mnesia:table(ts_timeline),
|
||
|
T#ts_timeline.ref = {Username, _}]),
|
||
|
Start, Length).
|
||
|
|
||
|
|