-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathstarter_file_groups_controller.rb
More file actions
167 lines (152 loc) · 6.86 KB
/
starter_file_groups_controller.rb
File metadata and controls
167 lines (152 loc) · 6.86 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module Api
# Api controller for starter file groups
class StarterFileGroupsController < MainApiController
def create
assignment = Assignment.find_by(id: params[:assignment_id])
other_params = params.permit(:entry_rename, :use_rename, :name).to_h.symbolize_keys
starter_file_group = StarterFileGroup.new(assessment_id: assignment.id, **other_params)
if starter_file_group.save
render 'shared/http_status', locals: { code: '201', message:
HttpStatusHelper::ERROR_CODE['message']['201'] }, status: :created
else
render 'shared/http_status', locals: { code: '500', message:
starter_file_group.errors.full_messages.first }, status: :internal_server_error
end
end
def update
starter_file_group = record
if starter_file_group.update(params.permit(:name, :entry_rename, :use_rename))
if starter_file_group.assignment.starter_file_type == 'shuffle' &&
(starter_file_group.saved_change_to_entry_rename? || starter_file_group.saved_change_to_use_rename?)
starter_file_group.assignment.groupings.update_all(starter_file_changed: true)
end
render 'shared/http_status', locals: { code: '200', message:
HttpStatusHelper::ERROR_CODE['message']['200'] }, status: :ok
else
render 'shared/http_status', locals: { code: '500', message:
starter_file_group.errors.full_messages.first }, status: :internal_server_error
end
end
def destroy
starter_file_group = record
if starter_file_group.destroy
render 'shared/http_status', locals: { code: '200', message:
HttpStatusHelper::ERROR_CODE['message']['200'] }, status: :ok
else
render 'shared/http_status', locals: { code: '500', message:
starter_file_group.errors.full_messages.first }, status: :internal_server_error
end
end
def index
assignment = Assignment.find_by(id: params[:assignment_id])
respond_to do |format|
format.xml { render xml: assignment.starter_file_groups.to_xml(skip_types: 'true') }
format.json { render json: assignment.starter_file_groups.to_json }
end
end
def show
starter_file_group = record
respond_to do |format|
format.xml { render xml: starter_file_group.to_xml(skip_types: 'true') }
format.json { render json: starter_file_group.to_json }
end
end
def create_file
starter_file_group = record
if has_missing_params?([:filename, :file_content])
# incomplete/invalid HTTP params
render 'shared/http_status', locals: { code: '422', message:
HttpStatusHelper::ERROR_CODE['message']['422'] }, status: :unprocessable_entity
return
end
if params[:file_content].respond_to? :read # binary data
content = params[:file_content].read
else
content = params[:file_content]
end
file_path = File.join(starter_file_group.path, params[:filename])
File.write(file_path, content, mode: 'wb')
update_entries_and_warn(starter_file_group)
render 'shared/http_status',
locals: { code: '201', message: HttpStatusHelper::ERROR_CODE['message']['201'] },
status: :created
rescue StandardError => e
message = "#{HttpStatusHelper::ERROR_CODE['message']['500']}\n\n#{e.message}"
render 'shared/http_status', locals: { code: '500', message: message }, status: :internal_server_error
end
def create_folder
starter_file_group = record
if has_missing_params?([:folder_path])
# incomplete/invalid HTTP params
render 'shared/http_status', locals: { code: '422', message:
HttpStatusHelper::ERROR_CODE['message']['422'] }, status: :unprocessable_entity
return
end
folder_path = File.join(starter_file_group.path, params[:folder_path])
FileUtils.mkdir_p(folder_path)
update_entries_and_warn(starter_file_group)
render 'shared/http_status',
locals: { code: '201', message: HttpStatusHelper::ERROR_CODE['message']['201'] },
status: :created
rescue StandardError => e
message = "#{HttpStatusHelper::ERROR_CODE['message']['500']}\n\n#{e.message}"
render 'shared/http_status', locals: { code: '500', message: message }, status: :internal_server_error
end
def remove_file
starter_file_group = record
if has_missing_params?([:filename])
# incomplete/invalid HTTP params
render 'shared/http_status', locals: { code: '422', message:
HttpStatusHelper::ERROR_CODE['message']['422'] }, status: :unprocessable_entity
return
end
file_path = File.join(starter_file_group.path, params[:filename])
File.delete(file_path)
update_entries_and_warn(starter_file_group)
render 'shared/http_status',
locals: { code: '200', message: HttpStatusHelper::ERROR_CODE['message']['200'] },
status: :ok
rescue StandardError => e
message = "#{HttpStatusHelper::ERROR_CODE['message']['500']}\n\n#{e.message}"
render 'shared/http_status', locals: { code: '500', message: message }, status: :internal_server_error
end
def remove_folder
starter_file_group = record
if has_missing_params?([:folder_path])
# incomplete/invalid HTTP params
render 'shared/http_status', locals: { code: '422', message:
HttpStatusHelper::ERROR_CODE['message']['422'] }, status: :unprocessable_entity
return
end
folder_path = File.join(starter_file_group.path, params[:folder_path])
FileUtils.rm_rf(folder_path)
update_entries_and_warn(starter_file_group)
render 'shared/http_status',
locals: { code: '200', message: HttpStatusHelper::ERROR_CODE['message']['200'] },
status: :ok
rescue StandardError => e
message = "#{HttpStatusHelper::ERROR_CODE['message']['500']}\n\n#{e.message}"
render 'shared/http_status', locals: { code: '500', message: message }, status: :internal_server_error
end
def download_entries
starter_file_group = record
zip_path = starter_file_group.zip_starter_file_files(current_role)
send_file zip_path, filename: File.basename(zip_path)
end
def entries
starter_file_group = record
respond_to do |format|
paths = starter_file_group.files_and_dirs
format.xml { render xml: paths.to_xml(root: 'paths', skip_types: 'true') }
format.json { render json: paths.to_json }
end
end
private
# Update starter file entries for +starter_file_group+ and set the starter_file_changed
# attribute to true for all groupings affected by the change.
def update_entries_and_warn(starter_file_group)
starter_file_group.assignment.assignment_properties.update!(starter_file_updated_at: Time.current)
starter_file_group.update_entries
end
end
end