Skip to content

Instantly share code, notes, and snippets.

@gumayunov
Created June 26, 2011 11:31
Show Gist options
  • Save gumayunov/1047535 to your computer and use it in GitHub Desktop.
Save gumayunov/1047535 to your computer and use it in GitHub Desktop.
%% show comment
handle(HD, {get, {comments, CommentId}}, Req) when is_integer(CommentId) ->
Result = case select_comment(person, CommentId) of
not_found ->
error_404;
Comment ->
UserId = proplists:get_value(user_id, Comment),
[Uinfo|_] = select_uinfoes_by_ids(person, [UserId]),
{ok, {[
{comment, {Comment} },
{uinfo, {Uinfo} }
]} }
end,
response(Result);
%% index comments
handle(HD, {get, {comments}}, Req) ->
[CommentableId, CommentableType] = commentable_params(Req),
Result = case ut:all_defined([CommentableType, CommentableId]) of
true ->
Key = make_key([commentable_comments, CommentableType, CommentableId]),
case get_cache(Key) of
undefined ->
Comments = select_comments(person_write, CommentableType, CommentableId),
UserIDs = lists:map(fun(Comment)-> proplists:get_value(user_id, Comment) end, Comments),
[Uinfoes, Statuses] = ut:parallel([
fun() -> select_uinfoes_by_ids(person_read, UserIDs) end,
fun() -> select_statuses_by_uids(onliners_write, UserIDs) end
]),
Res = {[
{comments, ut:jmap(Comments)},
{uinfoes, ut:jmap(Uinfoes)},
{statuses, {Statuses}}
]},
set_cache(Key, ejson:encode(Res)),
{ok, Res};
CachedJson ->
{ok, CachedJson}
end;
_ ->
error_400
end,
response(Result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment