Skip to content

Commit 6e4282d

Browse files
committed
Optimize getting exercises in a course
1 parent 40e9486 commit 6e4282d

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

app/controllers/api/v8/courses/exercises_controller.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,21 @@ def index
3434
course = Course.find_by!(id: params[:course_id]) if params[:course_id]
3535
exercises = Exercise.includes(:available_points).where(course_id: course.id)
3636

37-
visible = exercises.select { |ex| ex.visible_to?(current_user) }
38-
presentable = visible.map do |ex|
37+
unlocked_exercises = course.unlocks
38+
.where(user_id: current_user.id)
39+
.where(['valid_after IS NULL OR valid_after < ?', Time.now])
40+
.pluck(:exercise_name)
41+
42+
unless current_user.administrator? || current_user.teacher?(course.organization) || current_user.assistant?(course)
43+
exercises = exercises.where(hidden: false, disabled_status: 0)
44+
exercises = if unlocked_exercises.empty?
45+
exercises.where(unlock_spec: nil)
46+
else
47+
exercises.where(["unlock_spec IS NULL OR name IN (#{unlocked_exercises.map { |_| '?' }.join(', ')})", *unlocked_exercises])
48+
end.select { |e| e._fast_visible_to?(current_user) }
49+
end
50+
51+
presentable = exercises.map do |ex|
3952
{
4053
id: ex.id,
4154
available_points: ex.available_points,
@@ -47,8 +60,7 @@ def index
4760
}
4861
end
4962

50-
authorize_collection :read, visible
51-
present(presentable)
63+
render json: presentable
5264
end
5365
end
5466
end

0 commit comments

Comments
 (0)