2016-01-20 15:48:46 +01:00
|
|
|
class API::V1::DossiersController < APIController
|
2018-09-26 16:49:21 +02:00
|
|
|
before_action :fetch_procedure_and_check_token
|
|
|
|
|
2018-03-08 17:42:12 +01:00
|
|
|
DEFAULT_PAGE_SIZE = 100
|
2019-11-05 16:36:12 +01:00
|
|
|
MAX_PAGE_SIZE = 1000
|
2019-06-20 12:11:39 +02:00
|
|
|
ORDER_DIRECTIONS = { 'asc' => :asc, 'desc' => :desc }
|
2018-03-08 17:42:12 +01:00
|
|
|
|
2016-01-20 15:48:46 +01:00
|
|
|
def index
|
2018-11-01 14:04:32 +01:00
|
|
|
dossiers = @dossiers.page(params[:page]).per(per_page)
|
2016-11-15 09:54:17 +01:00
|
|
|
|
2018-12-24 17:28:20 +01:00
|
|
|
render json: { dossiers: dossiers.map { |dossier| DossiersSerializer.new(dossier) }, pagination: pagination(dossiers) }, status: 200
|
2017-04-14 17:05:38 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
2016-01-20 15:48:46 +01:00
|
|
|
render json: {}, status: 404
|
|
|
|
end
|
|
|
|
|
2016-01-25 16:08:10 +01:00
|
|
|
def show
|
2018-11-01 14:04:32 +01:00
|
|
|
dossier = @dossiers.for_api.find(params[:id])
|
2016-11-15 09:54:17 +01:00
|
|
|
|
2016-02-19 16:59:18 +01:00
|
|
|
respond_to do |format|
|
2017-04-14 17:38:08 +02:00
|
|
|
format.json { render json: { dossier: DossierSerializer.new(dossier).as_json }, status: 200 }
|
2016-02-19 16:59:18 +01:00
|
|
|
end
|
2017-04-14 17:05:38 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
2016-01-25 16:08:10 +01:00
|
|
|
render json: {}, status: 404
|
|
|
|
end
|
|
|
|
|
2018-09-26 16:50:39 +02:00
|
|
|
private
|
|
|
|
|
2016-01-25 16:08:10 +01:00
|
|
|
def pagination(dossiers)
|
2016-01-20 15:48:46 +01:00
|
|
|
{
|
2018-01-15 19:02:12 +01:00
|
|
|
page: dossiers.current_page,
|
|
|
|
resultats_par_page: dossiers.limit_value,
|
|
|
|
nombre_de_page: dossiers.total_pages
|
2016-01-20 15:48:46 +01:00
|
|
|
}
|
|
|
|
end
|
2017-12-28 19:31:28 +01:00
|
|
|
|
|
|
|
def per_page # inherited value from will_paginate
|
2019-11-05 16:36:12 +01:00
|
|
|
resultats_par_page = params[:resultats_par_page]&.to_i
|
|
|
|
if resultats_par_page && resultats_par_page > 0
|
|
|
|
[resultats_par_page, MAX_PAGE_SIZE].min
|
|
|
|
else
|
|
|
|
DEFAULT_PAGE_SIZE
|
|
|
|
end
|
2017-12-28 19:31:28 +01:00
|
|
|
end
|
2018-09-26 16:49:21 +02:00
|
|
|
|
|
|
|
def fetch_procedure_and_check_token
|
2018-11-01 14:04:32 +01:00
|
|
|
@procedure = Procedure.for_api.find(params[:procedure_id])
|
2018-09-26 16:49:21 +02:00
|
|
|
|
2019-03-06 15:21:25 +01:00
|
|
|
administrateur = find_administrateur_for_token(@procedure)
|
|
|
|
if administrateur
|
|
|
|
Current.administrateur = administrateur
|
|
|
|
else
|
2018-09-26 16:49:21 +02:00
|
|
|
render json: {}, status: :unauthorized
|
|
|
|
end
|
|
|
|
|
2019-06-20 12:11:39 +02:00
|
|
|
order = ORDER_DIRECTIONS.fetch(params[:order], :asc)
|
|
|
|
@dossiers = @procedure.dossiers.state_not_brouillon.order_for_api(order)
|
2018-11-01 14:04:32 +01:00
|
|
|
|
2018-09-26 16:49:21 +02:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
render json: {}, status: :not_found
|
|
|
|
end
|
2017-04-04 15:27:04 +02:00
|
|
|
end
|