Skip to content

Commit

Permalink
Optimize api v8 exercises_controller.rb
Browse files Browse the repository at this point in the history
  • Loading branch information
nygrenh committed Feb 2, 2024
1 parent 81620f5 commit 48695da
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions app/controllers/api/v8/courses/exercises_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def index
can_see_everything = current_user.administrator? || current_user.teacher?(course.organization) || current_user.assistant?(course)
authorize! :read, course

exercises = Exercise.includes(:available_points).where(course_id: course.id)
exercises = course.exercises
unlocked_exercises = course.unlocks
.where(user_id: current_user.id)
.where(['valid_after IS NULL OR valid_after < ?', Time.now])
Expand All @@ -54,19 +54,25 @@ def index

exercises = exercises.pluck(:id)

all_exercises = course.exercises.where(disabled_status: 0)
all_exercises = course.exercises.includes(:available_points).where(disabled_status: 0)
unless can_see_everything
all_exercises = course.exercises
.includes(:available_points)
.where(hidden: false)
.select(&:_fast_visible?)
end

all_awarded_points = AwardedPoints.course_user_points(course, current_user).includes(:submissions)

presentable = all_exercises.map do |ex|
points_visible = ex.points_visible_to?(current_user)
available_points = ex.available_points
available_points_names = available_points.map(&:name)
awarded_points = all_awarded_points.select { |awp| available_points_names.include?(awp.name) }.map(&:name)
{
id: ex.id,
available_points: points_visible ? ex.available_points : [],
awarded_points: points_visible ? ex.points_for(current_user) : [],
available_points: points_visible ? available_points : [],
awarded_points: points_visible ? awarded_points : [],
name: ex.name,
publish_time: ex.publish_time,
solution_visible_after: ex.solution_visible_after,
Expand Down

0 comments on commit 48695da

Please sign in to comment.