This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eb21c06
commit cc349c5
Showing
1 changed file
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
class PiasController < ApplicationController | ||
before_action :set_pia, only: %i[show update destroy duplicate] | ||
before_action :set_serializer, only: %i[index show] | ||
|
||
# GET /pias | ||
def index | ||
sorting = sorting_params | ||
sorting = nil unless Pia.attribute_names.include?(sorting[:column]) | ||
sorting[:direction] = 'asc' if sorting && sorting[:direction] != 'desc' | ||
@pias = Pia.all | ||
@pias = @pias.order("#{sorting[:column]} #{sorting[:direction]}") if sorting.present? | ||
|
||
render json: @pias, each_serializer: @index_serializer | ||
end | ||
|
||
# GET /pias/example | ||
def example | ||
pia = Pia.find_by(is_example: 1) | ||
render json: pia, serializer: @index_serializer | ||
end | ||
|
||
# GET /pias/1 | ||
def show | ||
render json: @pia, serializer: @index_serializer | ||
end | ||
|
||
# POST /pias | ||
def create | ||
pia_parameters = pia_params | ||
#pia_parameters[:structure_data] = JSON.parse(pia_parameters[:structure_data]) if pia_parameters[:structure_data] | ||
@pia = Pia.new(pia_parameters) | ||
|
||
if @pia.save | ||
render json: @pia, status: :created | ||
else | ||
render json: @pia.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# PATCH/PUT /pias/1 | ||
def update | ||
pia_parameters = pia_params | ||
#pia_parameters[:structure_data] = JSON.parse(pia_parameters[:structure_data]) if pia_parameters[:structure_data] | ||
|
||
if @pia.update(pia_parameters) | ||
render json: @pia | ||
else | ||
render json: @pia.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# DELETE /pias/1 | ||
def destroy | ||
@pia.destroy | ||
end | ||
|
||
def duplicate | ||
@clone = @pia.duplicate | ||
|
||
render json: @clone | ||
end | ||
|
||
def import | ||
@import_params = import_params | ||
import_data_io = @import_params[:data] | ||
json_str = import_data_io.read | ||
Pia.import(json_str) | ||
end | ||
|
||
private | ||
|
||
def import_params | ||
params.fetch(:import, {}).permit(:data) | ||
end | ||
|
||
# Set seralizer for pias index | ||
def set_serializer | ||
@index_serializer = params[:export].present? ? ExportPiaSerializer : PiaSerializer | ||
end | ||
|
||
# Use callbacks to share common setup or constraints between actions. | ||
def set_pia | ||
@pia = Pia.find(params[:id]) | ||
end | ||
|
||
# Only allow trusted sorting parameters | ||
def sorting_params | ||
params.fetch(:sort, {}).permit(:column, :direction) | ||
end | ||
|
||
# Only allow a trusted parameter "white list" through. | ||
def pia_params | ||
params.fetch(:pia, {}).permit(:status, | ||
:name, | ||
:author_name, | ||
:evaluator_name, | ||
:validator_name, | ||
:dpo_status, | ||
:dpo_opinion, | ||
:dpos_names, | ||
:people_names, | ||
:concerned_people_opinion, | ||
:concerned_people_status, | ||
:concerned_people_searched_content, | ||
:concerned_people_searched_opinion, | ||
:rejection_reason, | ||
:applied_adjustments, | ||
:is_example, | ||
:structure_id, | ||
:structure_name, | ||
:structure_sector_name, | ||
:structure_data) | ||
end | ||
end |