From 50d4c07fa56fc1ae92e82003c4b40e00b64b955a Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Wed, 9 Apr 2014 22:32:24 -0300 Subject: [PATCH] adding options to select comments and articles api --- lib/api/v1/articles.rb | 17 +++++++++++++---- lib/api/v1/comments.rb | 14 +++++++++++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/api/v1/articles.rb b/lib/api/v1/articles.rb index bf49aad..4234d77 100644 --- a/lib/api/v1/articles.rb +++ b/lib/api/v1/articles.rb @@ -30,11 +30,11 @@ module API conditions[:type] = parse_content_type(params[:content_type]) conditions[:created_at] = period(from_date, until_date) if params[:reference_id] - @articles = environment.articles.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") + articles = environment.articles.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") else - @articles = environment.articles.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") + articles = environment.articles.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") end - present @articles, :with => Entities::Article + present articles, :with => Entities::Article end desc "Return the article id" @@ -43,9 +43,18 @@ module API end get ':id/children' do + from_date = DateTime.parse(params[:from]) if params[:from] + until_date = DateTime.parse(params[:until]) if params[:until] + + conditions = {} conditions[:type] = parse_content_type(params[:content_type]) conditions[:created_at] = period(from_date, until_date) - present environment.articles.find(params[:id]).children.find(:all, conditions, :limit => limit), :with => Entities::Article + if params[:reference_id] + 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") + else + articles = environment.articles.find(params[:id]).children.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") + end + present articles, :with => Entities::Article end get ':id/children/:child_id' do diff --git a/lib/api/v1/comments.rb b/lib/api/v1/comments.rb index 6502add..166c870 100644 --- a/lib/api/v1/comments.rb +++ b/lib/api/v1/comments.rb @@ -16,10 +16,18 @@ module API # Example Request: # GET /articles/12/comments?oldest&limit=10&reference_id=23 get ":id/comments" do + from_date = DateTime.parse(params[:from]) if params[:from] + until_date = DateTime.parse(params[:until]) if params[:until] + conditions = {} -#FIXME See a way to use desc and cres - conditions = ["id #{params.key?(:oldest) ? '<' : '>'} ?", params[:reference_id]] if params[:reference_id] - present environment.articles.find(params[:id]).comments.find(:all, :conditions => conditions, :limit => limit), :with => Entities::Comment + conditions[:created_at] = period(from_date, until_date) + if params[:reference_id] + 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") + else + comments = environment.articles.find(params[:id]).comments.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC") + end + present comments, :with => Entities::Comment + end get ":id/comments/:comment_id" do -- libgit2 0.21.2