comments.rb
1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
module API
module V1
class Comments < Grape::API
before { authenticate! }
resource :articles do
# Collect comments from articles
#
# Parameters:
# reference_id - comment id used as reference to collect comment
# oldest - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected
# limit - amount of comments returned. The default value is 20
#
# Example Request:
# GET /articles/12/comments?oldest&limit=10&reference_id=23
get ":id/comments" do
conditions = make_conditions_with_parameter(params)
if params[:reference_id]
comments = environment.articles.find(params[:id]).comments.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit)
else
comments = environment.articles.find(params[:id]).comments.reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit)
end
present comments, :with => Entities::Comment
end
get ":id/comments/:comment_id" do
present environment.articles.find(params[:id]).comments.find(params[:comment_id]), :with => Entities::Comment
end
# Example Request:
# POST api/v1/articles/12/comments?private_toke=234298743290432&body=new comment
post ":id/comments" do
present environment.articles.find(params[:id]).comments.create(:author => current_person, :body => params[:body]), :with => Entities::Comment
end
end
end
end
end