-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathschools_controller.rb
More file actions
103 lines (88 loc) · 2.75 KB
/
schools_controller.rb
File metadata and controls
103 lines (88 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# frozen_string_literal: true
module Api
class SchoolsController < ApiController
before_action :authorize_user
load_and_authorize_resource
skip_load_and_authorize_resource only: :import
def index
@schools = School.accessible_by(current_ability)
render :index, formats: [:json], status: :ok
end
def show
render :show, formats: [:json], status: :ok
end
def create
result = School::Create.call(school_params:, creator_id: current_user.id, token: current_user.token)
if result.success?
@school = result[:school]
render :show, formats: [:json], status: :created
else
render json: {
error: result[:error],
error_types: result[:error_types]
}, status: :unprocessable_content
end
end
def update
school = School.find(params[:id])
result = School::Update.call(school:, school_params:)
if result.success?
@school = result[:school]
render :show, formats: [:json], status: :ok
else
render json: { error: result[:error] }, status: :unprocessable_content
end
end
def destroy
result = School::Delete.call(school_id: params[:id])
if result.success?
head :no_content
else
render json: { error: result[:error] }, status: :unprocessable_content
end
end
def import
authorize! :import, School
if params[:csv_file].blank?
render json: { error: SchoolImportError.format_error(:csv_file_required, 'CSV file is required') },
status: :unprocessable_content
return
end
result = School::ImportBatch.call(
csv_file: params[:csv_file],
current_user: current_user
)
if result.success?
@job_id = result[:job_id]
@total_schools = result[:total_schools]
render :import, formats: [:json], status: :accepted
else
render json: { error: result[:error] }, status: :unprocessable_content
end
end
private
def school_params
params.expect(
school: %i[name
website
reference
district_name
district_nces_id
school_roll_number
address_line_1
address_line_2
municipality
administrative_area
postal_code
country_code
creator_role
creator_department
creator_agree_authority
creator_agree_terms_and_conditions
creator_agree_to_ux_contact
creator_agree_responsible_safeguarding
user_origin]
)
end
end
end