Commit 50d4c07fa56fc1ae92e82003c4b40e00b64b955a

Authored by Leandro Santos
Committed by Rodrigo Souto
1 parent a7e59688

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 30 conditions[:type] = parse_content_type(params[:content_type])
31 31 conditions[:created_at] = period(from_date, until_date)
32 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 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 36 end
37   - present @articles, :with => Entities::Article
  37 + present articles, :with => Entities::Article
38 38 end
39 39  
40 40 desc "Return the article id"
... ... @@ -43,9 +43,18 @@ module API
43 43 end
44 44  
45 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 50 conditions[:type] = parse_content_type(params[:content_type])
47 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 58 end
50 59  
51 60 get ':id/children/:child_id' do
... ...
lib/api/v1/comments.rb
... ... @@ -16,10 +16,18 @@ module API
16 16 # Example Request:
17 17 # GET /articles/12/comments?oldest&limit=10&reference_id=23
18 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 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 31 end
24 32  
25 33 get ":id/comments/:comment_id" do
... ...