Skip to content

Commit 90398a7

Browse files
committed
Revert "Revert "Optimize getting exercises in a course""
This reverts commit 7d77006.
1 parent 7d77006 commit 90398a7

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,24 @@ class ExercisesController < Api::V8::BaseController
3232
def index
3333
unauthorize_guest!
3434
course = Course.find_by!(id: params[:course_id]) if params[:course_id]
35+
authorize! :read, course
3536
exercises = Exercise.includes(:available_points).where(course_id: course.id)
3637

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

50-
authorize_collection :read, visible
51-
present(presentable)
64+
render json: presentable
5265
end
5366
end
5467
end

0 commit comments

Comments
 (0)