Add API index on Dossier.
This commit is contained in:
parent
6dfdb474a5
commit
78bd982f97
6 changed files with 107 additions and 4 deletions
18
app/controllers/api/v1/dossiers_controller.rb
Normal file
18
app/controllers/api/v1/dossiers_controller.rb
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
class API::V1::DossiersController < APIController
|
||||||
|
|
||||||
|
def index
|
||||||
|
procedure = current_administrateur.procedures.find(params[:procedure_id])
|
||||||
|
dossiers = procedure.dossiers.paginate(page: params[:page]).decorate
|
||||||
|
render json: dossiers, meta: pagination(dossiers), meta_key: 'pagination', status: 200
|
||||||
|
rescue ActiveRecord::RecordNotFound => e
|
||||||
|
render json: {}, status: 404
|
||||||
|
end
|
||||||
|
|
||||||
|
def pagination dossiers
|
||||||
|
{
|
||||||
|
page: dossiers.current_page,
|
||||||
|
resultats_par_page: dossiers.per_page,
|
||||||
|
nombre_de_page: dossiers.total_pages
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
5
app/serializers/dossier_serializer.rb
Normal file
5
app/serializers/dossier_serializer.rb
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class DossierSerializer < ActiveModel::Serializer
|
||||||
|
attributes :id,
|
||||||
|
:nom_projet,
|
||||||
|
:updated_at
|
||||||
|
end
|
|
@ -86,7 +86,9 @@ Rails.application.routes.draw do
|
||||||
|
|
||||||
namespace :api do
|
namespace :api do
|
||||||
namespace :v1 do
|
namespace :v1 do
|
||||||
resources :procedures, only: [:index, :show]
|
resources :procedures, only: [:index, :show] do
|
||||||
|
resources :dossiers, only: [:index]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
class RemoveDefaultDateToDossier < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
change_column_default(:dossiers, :created_at, nil)
|
||||||
|
change_column_default(:dossiers, :updated_at, nil)
|
||||||
|
end
|
||||||
|
end
|
|
@ -11,7 +11,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20160106100227) do
|
ActiveRecord::Schema.define(version: 20160120141602) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
@ -63,8 +63,8 @@ ActiveRecord::Schema.define(version: 20160106100227) do
|
||||||
t.boolean "autorisation_donnees"
|
t.boolean "autorisation_donnees"
|
||||||
t.string "nom_projet"
|
t.string "nom_projet"
|
||||||
t.integer "procedure_id"
|
t.integer "procedure_id"
|
||||||
t.datetime "created_at", default: '2015-12-07 09:51:23'
|
t.datetime "created_at"
|
||||||
t.datetime "updated_at", default: '2015-12-07 09:51:23'
|
t.datetime "updated_at"
|
||||||
t.string "state"
|
t.string "state"
|
||||||
t.integer "user_id"
|
t.integer "user_id"
|
||||||
t.text "json_latlngs"
|
t.text "json_latlngs"
|
||||||
|
|
72
spec/controllers/api/v1/dossiers_controller_spec.rb
Normal file
72
spec/controllers/api/v1/dossiers_controller_spec.rb
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
require 'spec_helper'
|
||||||
|
|
||||||
|
describe API::V1::DossiersController do
|
||||||
|
let(:admin) { create(:administrateur) }
|
||||||
|
let(:procedure) { create(:procedure, administrateur: admin) }
|
||||||
|
let(:wrong_procedure) { create(:procedure) }
|
||||||
|
|
||||||
|
it { expect(described_class).to be < APIController }
|
||||||
|
describe 'GET index' do
|
||||||
|
let(:response) { get :index, token: admin.api_token, procedure_id: procedure_id }
|
||||||
|
subject { response }
|
||||||
|
|
||||||
|
|
||||||
|
context 'when procedure is not found' do
|
||||||
|
let(:procedure_id) { 99_999_999 }
|
||||||
|
it { expect(subject.code).to eq('404') }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when procedure does not belong to admin' do
|
||||||
|
let(:procedure_id) { wrong_procedure.id }
|
||||||
|
it { expect(subject.code).to eq('404') }
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when procedure is found and belongs to admin' do
|
||||||
|
let(:procedure_id) { procedure.id }
|
||||||
|
let(:date_creation) { Time.local(2008, 9, 1, 10, 5, 0) }
|
||||||
|
let!(:dossier) { Timecop.freeze(date_creation) { create(:dossier, :with_entreprise, :with_user, procedure: procedure) } }
|
||||||
|
let(:body) { JSON.parse(response.body, symbolize_names: true) }
|
||||||
|
it { expect(response.code).to eq('200') }
|
||||||
|
it { expect(body).to have_key :pagination }
|
||||||
|
it { expect(body).to have_key :dossiers }
|
||||||
|
|
||||||
|
describe 'pagination' do
|
||||||
|
subject { body[:pagination] }
|
||||||
|
it { is_expected.to have_key(:page) }
|
||||||
|
it { expect(subject[:page]).to eq(1) }
|
||||||
|
it { is_expected.to have_key(:resultats_par_page) }
|
||||||
|
it { expect(subject[:resultats_par_page]).to eq(12) }
|
||||||
|
it { is_expected.to have_key(:nombre_de_page) }
|
||||||
|
it { expect(subject[:nombre_de_page]).to eq(1) }
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'dossiers' do
|
||||||
|
subject { body[:dossiers] }
|
||||||
|
it { expect(subject).to be_an(Array) }
|
||||||
|
describe 'dossier' do
|
||||||
|
subject { super().first }
|
||||||
|
it { expect(subject[:id]).to eq(dossier.id) }
|
||||||
|
it { expect(subject[:nom_projet]).to eq(dossier.nom_projet) }
|
||||||
|
it { expect(subject[:updated_at]).to eq("2008-09-01T08:05:00.000Z") }
|
||||||
|
it { expect(subject.keys.size).to eq(3) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when there are multiple pages' do
|
||||||
|
let(:response) { get :index, token: admin.api_token, procedure_id: procedure_id, page: 2 }
|
||||||
|
let!(:dossier1) { create(:dossier, :with_entreprise, :with_user, procedure: procedure) }
|
||||||
|
let!(:dossier2) { create(:dossier, :with_entreprise, :with_user, procedure: procedure) }
|
||||||
|
before do
|
||||||
|
allow(Dossier).to receive(:per_page).and_return(1)
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'pagination' do
|
||||||
|
subject { body[:pagination] }
|
||||||
|
it { expect(subject[:page]).to eq(2) }
|
||||||
|
it { expect(subject[:resultats_par_page]).to eq(1) }
|
||||||
|
it { expect(subject[:nombre_de_page]).to eq(3) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue