Merge pull request #3980 from tchak/add-order-to-api

Add order param to API
This commit is contained in:
Paul Chavard 2019-06-20 15:40:36 +02:00 committed by GitHub
commit b468eb8dec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View file

@ -2,6 +2,7 @@ class API::V1::DossiersController < APIController
before_action :fetch_procedure_and_check_token
DEFAULT_PAGE_SIZE = 100
ORDER_DIRECTIONS = { 'asc' => :asc, 'desc' => :desc }
def index
dossiers = @dossiers.page(params[:page]).per(per_page)
@ -45,7 +46,8 @@ class API::V1::DossiersController < APIController
render json: {}, status: :unauthorized
end
@dossiers = @procedure.dossiers.state_not_brouillon.order_for_api
order = ORDER_DIRECTIONS.fetch(params[:order], :asc)
@dossiers = @procedure.dossiers.state_not_brouillon.order_for_api(order)
rescue ActiveRecord::RecordNotFound
render json: {}, status: :not_found

View file

@ -24,7 +24,8 @@ describe API::V1::DossiersController do
end
describe 'GET index' do
let(:retour) { get :index, params: { token: token, procedure_id: procedure_id } }
let(:order) { nil }
let(:retour) { get :index, params: { token: token, procedure_id: procedure_id, order: order }.compact }
subject { retour }
@ -80,6 +81,23 @@ describe API::V1::DossiersController do
it { expect(subject[:state]).to eq("initiated") }
it { expect(subject.keys.size).to eq(4) }
end
describe 'order' do
let!(:dossier1) { create(:dossier, :with_entreprise, procedure: procedure, state: Dossier.states.fetch(:en_construction)) }
let!(:dossier2) { create(:dossier, :with_entreprise, procedure: procedure, state: Dossier.states.fetch(:en_construction)) }
context 'asc' do
let(:order) { 'asc' }
it { expect(subject.map { |dossier| dossier[:id] }).to eq([dossier.id, dossier1.id, dossier2.id]) }
end
context 'desc' do
let(:order) { 'desc' }
it { expect(subject.map { |dossier| dossier[:id] }).to eq([dossier2.id, dossier1.id, dossier.id]) }
end
end
end
context 'when there are multiple pages' do