From 3791875c0850be785ced4e5755cff43e509bf656 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20Pi=C4=8Dman?= Date: Fri, 11 May 2018 10:35:45 +0200 Subject: [PATCH] REST API and pagination on collection resources #850 --- app/controllers/dmsf_controller.rb | 19 ++++- extra/api/api_client.sh | 1 + .../rest_api/dmsf_folder_api_test.rb | 79 +++++++++++++++---- 3 files changed, 81 insertions(+), 18 deletions(-) diff --git a/app/controllers/dmsf_controller.rb b/app/controllers/dmsf_controller.rb index 86061405..2450d8c0 100644 --- a/app/controllers/dmsf_controller.rb +++ b/app/controllers/dmsf_controller.rb @@ -662,20 +662,35 @@ class DmsfController < ApplicationController @url_links = [] else if @folder - @subfolders = @folder.dmsf_folders.visible.to_a + @subfolders = @folder.dmsf_folders.visible @files = @folder.dmsf_files.visible @dir_links = @folder.folder_links.visible @file_links = @folder.file_links.visible @url_links = @folder.url_links.visible @locked_for_user = @folder.locked_for_user? else - @subfolders = @project.dmsf_folders.visible.to_a + @subfolders = @project.dmsf_folders.visible @files = @project.dmsf_files.visible @dir_links = @project.folder_links.visible @file_links = @project.file_links.visible @url_links = @project.url_links.visible @locked_for_user = false end + # Limit and offset for REST API calls + if params[:limit].present? + @subfolders = @subfolders.limit(params[:limit]) + @files = @files.limit(params[:limit]) + @dir_links = @dir_links.limit(params[:limit]) + @file_links = @file_links.limit(params[:limit]) + @url_links = @url_links.limit(params[:limit]) + end + if params[:offset].present? + @subfolders = @subfolders.offset(params[:offset]) + @files = @files.offset(params[:offset]) + @dir_links = @dir_links.offset(params[:offset]) + @file_links = @file_links.offset(params[:offset]) + @url_links = @url_links.offset(params[:offset]) + end end # Remove system folders you are not allowed to see because you are not allowed to see the issue or you are not # permitted to see system folders diff --git a/extra/api/api_client.sh b/extra/api/api_client.sh index 8ee45845..7881a614 100644 --- a/extra/api/api_client.sh +++ b/extra/api/api_client.sh @@ -32,6 +32,7 @@ # 1. List of documents in a given folder or the root folder #curl -v -H "Content-Type: application/xml" -X GET -u ${1}:${2} http://localhost:3000/projects/12/dmsf.xml #curl -v -H "Content-Type: application/xml" -X GET -u ${1}:${2} http://localhost:3000/projects/12/dmsf.xml?folder_id=5155 +#curl -v -H "Content-Type: application/xml" -X GET -u ${1}:${2} "http://localhost:3000/projects/12/dmsf.xml?limit=2&offset=1" # 2. Get a document #curl -v -H "Content-Type: application/xml" -X GET -u ${1}:${2} http://localhost:3000/dmsf/files/17216.xml diff --git a/test/integration/rest_api/dmsf_folder_api_test.rb b/test/integration/rest_api/dmsf_folder_api_test.rb index cb901f52..8d28a6bb 100644 --- a/test/integration/rest_api/dmsf_folder_api_test.rb +++ b/test/integration/rest_api/dmsf_folder_api_test.rb @@ -32,6 +32,7 @@ class DmsfFolderApiTest < RedmineDmsf::Test::IntegrationTest @jsmith = User.find_by_id 2 @file1 = DmsfFile.find_by_id 1 @folder1 = DmsfFolder.find_by_id 1 + @folder6 = DmsfFolder.find_by_id 6 Setting.rest_api_enabled = '1' @role = Role.find_by_id 1 @project1 = Project.find_by_id 1 @@ -42,6 +43,7 @@ class DmsfFolderApiTest < RedmineDmsf::Test::IntegrationTest assert_kind_of User, @admin assert_kind_of User, @jsmith assert_kind_of DmsfFolder, @folder1 + assert_kind_of DmsfFolder, @folder6 assert_kind_of DmsfFile, @file1 assert_kind_of Role, @role assert_kind_of Project, @project1 @@ -55,31 +57,76 @@ class DmsfFolderApiTest < RedmineDmsf::Test::IntegrationTest assert_response :success assert_equal 'application/xml', @response.content_type # - # - # - # + # + # + # # 1 - # folder1 - # - # - # 6 + # folder1 + # + # + # 6 # folder6 - # - # - # - # + # + # + # 7 + # folder7 + # + # + # + # + # 9 + # myfile.txt + # + # + # 8 + # test.pdf + # + # # 1 - # test.txt - # - # - # - # + # test.txt + # + # + # 10 + # zero.txt + # + # + # + # # assert_select 'dmsf > dmsf_folders > folder > id', :text => @folder1.id.to_s assert_select 'dmsf > dmsf_folders > folder > title', :text => @folder1.title.to_s assert_select 'dmsf > dmsf_files > file > id', :text => @file1.id.to_s assert_select 'dmsf > dmsf_files > file > name', :text => @file1.name.to_s + end + def test_list_folder_limit_and_offset + @role.add_permission! :view_dmsf_folders + token = Token.create!(:user => @jsmith, :action => 'api') + #curl -v -H "Content-Type: application/xml" -X GET -u ${1}:${2} "http://localhost:3000/dmsf/files/17216.xml?limit=1&offset=1" + get "/projects/#{@project1.id}/dmsf.xml?key=#{token.value}&limit=1&offset=2" + assert_response :success + assert_equal 'application/xml', @response.content_type + # + # + # + # + # 6 + # folder6 + # + # + # + # + # 1 + # test.txt + # + # + # + # + # + assert_select 'dmsf > dmsf_folders', :count => 1 + assert_select 'dmsf > dmsf_folders > folder > id', :text => @folder6.id.to_s + assert_select 'dmsf > dmsf_folders > folder > title', :text => @folder6.title.to_s + assert_select 'dmsf > dmsf_files', :count => 1 end def test_create_folder