-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathschool_students_controller.rb
96 lines (76 loc) · 2.58 KB
/
school_students_controller.rb
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
# frozen_string_literal: true
module Api
class SchoolStudentsController < ApiController
before_action :authorize_user
load_and_authorize_resource :school
authorize_resource :school_student, class: false
before_action :create_safeguarding_flags
def index
result = SchoolStudent::List.call(school: @school, token: current_user.token)
if result.success?
@school_students = result[:school_students]
render :index, formats: [:json], status: :ok
else
render json: { error: result[:error] }, status: :unprocessable_entity
end
end
def create
result = SchoolStudent::Create.call(school: @school, school_student_params:, token: current_user.token)
if result.success?
head :no_content
else
render json: { error: result[:error] }, status: :unprocessable_entity
end
end
def create_batch
result = SchoolStudent::CreateBatch.call(school: @school, uploaded_file: params[:file], token: current_user.token)
if result.success?
head :no_content
else
render json: { error: result[:error] }, status: :unprocessable_entity
end
end
def update
result = SchoolStudent::Update.call(
school: @school, student_id: params[:id], school_student_params:, token: current_user.token
)
if result.success?
head :no_content
else
render json: { error: result[:error] }, status: :unprocessable_entity
end
end
def destroy
result = SchoolStudent::Delete.call(school: @school, student_id: params[:id], token: current_user.token)
if result.success?
head :no_content
else
render json: { error: result[:error] }, status: :unprocessable_entity
end
end
private
def school_student_params
params.require(:school_student).permit(:username, :password, :name)
end
def create_safeguarding_flags
create_teacher_safeguarding_flag
create_owner_safeguarding_flag
end
def create_teacher_safeguarding_flag
return unless current_user.school_teacher?(@school)
ProfileApiClient.create_safeguarding_flag(
token: current_user.token,
flag: ProfileApiClient::SAFEGUARDING_FLAGS[:teacher],
email: current_user.email
)
end
def create_owner_safeguarding_flag
return unless current_user.school_owner?(@school)
ProfileApiClient.create_safeguarding_flag(
token: current_user.token,
flag: ProfileApiClient::SAFEGUARDING_FLAGS[:owner],
email: current_user.email
)
end
end
end