Commit 8136b8d20ab72437eef7302232fbd1232e5811da
Exists in
master
and in
29 other branches
Merge branch 'api_pagination' into api
Showing
4 changed files
with
100 additions
and
1 deletions
Show diff stats
lib/noosfero/api/entities.rb
| @@ -33,6 +33,7 @@ module Noosfero | @@ -33,6 +33,7 @@ module Noosfero | ||
| 33 | class Profile < Entity | 33 | class Profile < Entity |
| 34 | expose :identifier, :name, :id | 34 | expose :identifier, :name, :id |
| 35 | expose :created_at, :format_with => :timestamp | 35 | expose :created_at, :format_with => :timestamp |
| 36 | + expose :updated_at, :format_with => :timestamp | ||
| 36 | expose :image, :using => Image | 37 | expose :image, :using => Image |
| 37 | end | 38 | end |
| 38 | 39 | ||
| @@ -77,6 +78,7 @@ module Noosfero | @@ -77,6 +78,7 @@ module Noosfero | ||
| 77 | expose :body | 78 | expose :body |
| 78 | expose :abstract | 79 | expose :abstract |
| 79 | expose :created_at, :format_with => :timestamp | 80 | expose :created_at, :format_with => :timestamp |
| 81 | + expose :updated_at, :format_with => :timestamp | ||
| 80 | expose :title, :documentation => {:type => "String", :desc => "Title of the article"} | 82 | expose :title, :documentation => {:type => "String", :desc => "Title of the article"} |
| 81 | expose :created_by, :as => :author, :using => Profile | 83 | expose :created_by, :as => :author, :using => Profile |
| 82 | expose :profile, :using => Profile | 84 | expose :profile, :using => Profile |
lib/noosfero/api/helpers.rb
| @@ -131,6 +131,26 @@ module Noosfero | @@ -131,6 +131,26 @@ module Noosfero | ||
| 131 | params[:order] || "created_at DESC" | 131 | params[:order] || "created_at DESC" |
| 132 | end | 132 | end |
| 133 | 133 | ||
| 134 | + def make_page_number_with_parameters(params) | ||
| 135 | + params[:page] || 1 | ||
| 136 | + end | ||
| 137 | + | ||
| 138 | + def make_per_page_with_parameters(params) | ||
| 139 | + params[:per_page] ||= limit | ||
| 140 | + params[:per_page].to_i | ||
| 141 | + end | ||
| 142 | + | ||
| 143 | + def make_timestamp_with_parameters_and_method(params, method) | ||
| 144 | + timestamp = nil | ||
| 145 | + if params[:timestamp] | ||
| 146 | + datetime = DateTime.parse(params[:timestamp]) | ||
| 147 | + table_name = method.to_s.singularize.camelize.constantize.table_name | ||
| 148 | + timestamp = "#{table_name}.updated_at >= '#{datetime}'" | ||
| 149 | + end | ||
| 150 | + | ||
| 151 | + timestamp | ||
| 152 | + end | ||
| 153 | + | ||
| 134 | def by_reference(scope, params) | 154 | def by_reference(scope, params) |
| 135 | if params[:reference_id] | 155 | if params[:reference_id] |
| 136 | created_at = scope.find(params[:reference_id]).created_at | 156 | created_at = scope.find(params[:reference_id]).created_at |
| @@ -143,10 +163,14 @@ module Noosfero | @@ -143,10 +163,14 @@ module Noosfero | ||
| 143 | def select_filtered_collection_of(object, method, params) | 163 | def select_filtered_collection_of(object, method, params) |
| 144 | conditions = make_conditions_with_parameter(params) | 164 | conditions = make_conditions_with_parameter(params) |
| 145 | order = make_order_with_parameters(params) | 165 | order = make_order_with_parameters(params) |
| 166 | + page_number = make_page_number_with_parameters(params) | ||
| 167 | + per_page = make_per_page_with_parameters(params) | ||
| 168 | + timestamp = make_timestamp_with_parameters_and_method(params, method) | ||
| 146 | 169 | ||
| 147 | objects = object.send(method) | 170 | objects = object.send(method) |
| 148 | objects = by_reference(objects, params) | 171 | objects = by_reference(objects, params) |
| 149 | - objects = objects.where(conditions).limit(limit).order(order) | 172 | + |
| 173 | + objects = objects.where(conditions).where(timestamp).page(page_number).per_page(per_page).order(order) | ||
| 150 | 174 | ||
| 151 | objects | 175 | objects |
| 152 | end | 176 | end |
test/unit/api/articles_test.rb
| @@ -82,6 +82,43 @@ class ArticlesTest < ActiveSupport::TestCase | @@ -82,6 +82,43 @@ class ArticlesTest < ActiveSupport::TestCase | ||
| 82 | assert_not_includes json['articles'].map {|a| a['id']}, child.id | 82 | assert_not_includes json['articles'].map {|a| a['id']}, child.id |
| 83 | end | 83 | end |
| 84 | 84 | ||
| 85 | + should 'list articles with pagination' do | ||
| 86 | + Article.destroy_all | ||
| 87 | + article_one = fast_create(Article, :profile_id => user.person.id, :name => "Another thing") | ||
| 88 | + article_two = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | ||
| 89 | + | ||
| 90 | + params[:page] = 1 | ||
| 91 | + params[:per_page] = 1 | ||
| 92 | + get "/api/v1/articles/?#{params.to_query}" | ||
| 93 | + json_page_one = JSON.parse(last_response.body) | ||
| 94 | + | ||
| 95 | + params[:page] = 2 | ||
| 96 | + params[:per_page] = 1 | ||
| 97 | + get "/api/v1/articles/?#{params.to_query}" | ||
| 98 | + json_page_two = JSON.parse(last_response.body) | ||
| 99 | + | ||
| 100 | + assert_includes json_page_one["articles"].map { |a| a["id"] }, article_two.id | ||
| 101 | + assert_not_includes json_page_one["articles"].map { |a| a["id"] }, article_one.id | ||
| 102 | + | ||
| 103 | + assert_includes json_page_two["articles"].map { |a| a["id"] }, article_one.id | ||
| 104 | + assert_not_includes json_page_two["articles"].map { |a| a["id"] }, article_two.id | ||
| 105 | + end | ||
| 106 | + | ||
| 107 | + should 'list articles with timestamp' do | ||
| 108 | + article_one = fast_create(Article, :profile_id => user.person.id, :name => "Another thing") | ||
| 109 | + article_two = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | ||
| 110 | + | ||
| 111 | + article_one.updated_at = Time.now + 3.hours | ||
| 112 | + article_one.save! | ||
| 113 | + | ||
| 114 | + params[:timestamp] = Time.now + 1.hours | ||
| 115 | + get "/api/v1/articles/?#{params.to_query}" | ||
| 116 | + json = JSON.parse(last_response.body) | ||
| 117 | + | ||
| 118 | + assert_includes json["articles"].map { |a| a["id"] }, article_one.id | ||
| 119 | + assert_not_includes json["articles"].map { |a| a["id"] }, article_two.id | ||
| 120 | + end | ||
| 121 | + | ||
| 85 | ############################# | 122 | ############################# |
| 86 | # Profile Articles # | 123 | # Profile Articles # |
| 87 | ############################# | 124 | ############################# |
test/unit/api/communities_test.rb
| @@ -120,4 +120,40 @@ class CommunitiesTest < ActiveSupport::TestCase | @@ -120,4 +120,40 @@ class CommunitiesTest < ActiveSupport::TestCase | ||
| 120 | assert_equivalent [c1.id], json['communities'].map {|c| c['id']} | 120 | assert_equivalent [c1.id], json['communities'].map {|c| c['id']} |
| 121 | end | 121 | end |
| 122 | 122 | ||
| 123 | + should 'list communities with pagination' do | ||
| 124 | + community1 = fast_create(Community, :public_profile => true) | ||
| 125 | + community2 = fast_create(Community) | ||
| 126 | + | ||
| 127 | + params[:page] = 2 | ||
| 128 | + params[:per_page] = 1 | ||
| 129 | + get "/api/v1/communities?#{params.to_query}" | ||
| 130 | + json_page_two = JSON.parse(last_response.body) | ||
| 131 | + | ||
| 132 | + params[:page] = 1 | ||
| 133 | + params[:per_page] = 1 | ||
| 134 | + get "/api/v1/communities?#{params.to_query}" | ||
| 135 | + json_page_one = JSON.parse(last_response.body) | ||
| 136 | + | ||
| 137 | + | ||
| 138 | + assert_includes json_page_one["communities"].map { |a| a["id"] }, community1.id | ||
| 139 | + assert_not_includes json_page_one["communities"].map { |a| a["id"] }, community2.id | ||
| 140 | + | ||
| 141 | + assert_includes json_page_two["communities"].map { |a| a["id"] }, community2.id | ||
| 142 | + assert_not_includes json_page_two["communities"].map { |a| a["id"] }, community1.id | ||
| 143 | + end | ||
| 144 | + | ||
| 145 | + should 'list communities with timestamp' do | ||
| 146 | + community1 = fast_create(Community, :public_profile => true) | ||
| 147 | + community2 = fast_create(Community) | ||
| 148 | + | ||
| 149 | + community1.updated_at = Time.now + 3.hours | ||
| 150 | + community1.save! | ||
| 151 | + | ||
| 152 | + params[:timestamp] = Time.now + 1.hours | ||
| 153 | + get "/api/v1/communities/?#{params.to_query}" | ||
| 154 | + json = JSON.parse(last_response.body) | ||
| 155 | + | ||
| 156 | + assert_includes json["communities"].map { |a| a["id"] }, community1.id | ||
| 157 | + assert_not_includes json["communities"].map { |a| a["id"] }, community2.id | ||
| 158 | + end | ||
| 123 | end | 159 | end |