Commit 539cff2c8d2713230e65825013be65ba8c81f854

Authored by Leandro Santos
Committed by Rodrigo Souto
1 parent a7493038

adding options to select comments and articles api

Showing 2 changed files with 24 additions and 7 deletions   Show diff stats
lib/api/v1/articles.rb
@@ -30,11 +30,11 @@ module API @@ -30,11 +30,11 @@ module API
30 conditions[:type] = parse_content_type(params[:content_type]) 30 conditions[:type] = parse_content_type(params[:content_type])
31 conditions[:created_at] = period(from_date, until_date) 31 conditions[:created_at] = period(from_date, until_date)
32 if params[:reference_id] 32 if params[:reference_id]
33 - @articles = environment.articles.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") 33 + articles = environment.articles.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
34 else 34 else
35 - @articles = environment.articles.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") 35 + articles = environment.articles.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
36 end 36 end
37 - present @articles, :with => Entities::Article 37 + present articles, :with => Entities::Article
38 end 38 end
39 39
40 desc "Return the article id" 40 desc "Return the article id"
@@ -43,9 +43,18 @@ module API @@ -43,9 +43,18 @@ module API
43 end 43 end
44 44
45 get ':id/children' do 45 get ':id/children' do
  46 + from_date = DateTime.parse(params[:from]) if params[:from]
  47 + until_date = DateTime.parse(params[:until]) if params[:until]
  48 +
  49 + conditions = {}
46 conditions[:type] = parse_content_type(params[:content_type]) 50 conditions[:type] = parse_content_type(params[:content_type])
47 conditions[:created_at] = period(from_date, until_date) 51 conditions[:created_at] = period(from_date, until_date)
48 - present environment.articles.find(params[:id]).children.find(:all, conditions, :limit => limit), :with => Entities::Article 52 + if params[:reference_id]
  53 + articles = environment.articles.find(params[:id]).children.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
  54 + else
  55 + articles = environment.articles.find(params[:id]).children.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
  56 + end
  57 + present articles, :with => Entities::Article
49 end 58 end
50 59
51 get ':id/children/:child_id' do 60 get ':id/children/:child_id' do
lib/api/v1/comments.rb
@@ -16,10 +16,18 @@ module API @@ -16,10 +16,18 @@ module API
16 # Example Request: 16 # Example Request:
17 # GET /articles/12/comments?oldest&limit=10&reference_id=23 17 # GET /articles/12/comments?oldest&limit=10&reference_id=23
18 get ":id/comments" do 18 get ":id/comments" do
  19 + from_date = DateTime.parse(params[:from]) if params[:from]
  20 + until_date = DateTime.parse(params[:until]) if params[:until]
  21 +
19 conditions = {} 22 conditions = {}
20 -#FIXME See a way to use desc and cres  
21 - conditions = ["id #{params.key?(:oldest) ? '<' : '>'} ?", params[:reference_id]] if params[:reference_id]  
22 - present environment.articles.find(params[:id]).comments.find(:all, :conditions => conditions, :limit => limit), :with => Entities::Comment 23 + conditions[:created_at] = period(from_date, until_date)
  24 + if params[:reference_id]
  25 + comments = environment.articles.find(params[:id]).comments.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
  26 + else
  27 + comments = environment.articles.find(params[:id]).comments.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
  28 + end
  29 + present comments, :with => Entities::Comment
  30 +
23 end 31 end
24 32
25 get ":id/comments/:comment_id" do 33 get ":id/comments/:comment_id" do