Skip to content

Commit

Permalink
perf: reduce database calls for loading units taught
Browse files Browse the repository at this point in the history
  • Loading branch information
macite committed Jan 13, 2022
1 parent 8ae92d5 commit 0e2cb6b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/api/entities/group_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class GroupEntity < Grape::Entity
expose :name
expose :tutorial_id
expose :group_set_id
expose :student_count
expose :student_count #TODO: remove this and request it dynamically when needed
expose :capacity_adjustment
expose :locked
end
Expand Down
2 changes: 1 addition & 1 deletion app/api/entities/tutorial_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class TutorialEntity < Grape::Entity
expose :tutorial_stream do |tutorial, options|
tutorial.tutorial_stream.abbreviation unless tutorial.tutorial_stream.nil?
end
expose :num_students
expose :num_students #TODO: remove this and request it dynamically when needed
expose :tutor do |tutorial, options|
Entities::UserEntity.represent tutorial.tutor, only: [:id, :name]
end
Expand Down
2 changes: 1 addition & 1 deletion app/api/entities/unit_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class UnitEntity < Grape::Entity
end

def is_staff?(user, unit)
[ Role.convenor, Role.tutor, Role.admin ].include? unit.role_for(user)
[ Role.convenor_id, Role.tutor_id, Role.admin_id ].include? unit.role_for(user).id
end

expose :code
Expand Down
14 changes: 13 additions & 1 deletion app/api/units_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,19 @@ class UnitsApi < Grape::API

desc "Get a unit's details"
get '/units/:id' do
unit = Unit.find(params[:id])
unit = Unit.includes(
{unit_roles: [:role, :user]},
{task_definitions: :tutorial_stream},
:learning_outcomes,
{tutorial_streams: :activity_type},
{tutorials: [:tutor, :tutorial_stream]},
:tutorial_enrolments,
{staff: [:role, :user]},
:group_sets,
:groups,
:group_memberships
).find(params[:id])

unless (authorise? current_user, unit, :get_unit) || (authorise? current_user, User, :admin_units)
error!({ error: "Couldn't find Unit with id=#{params[:id]}" }, 403)
end
Expand Down
3 changes: 1 addition & 2 deletions app/models/tutorial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ def self.find_by_user(user)
end

def tutor
result = UnitRole.find_by(id: unit_role_id)
result.user unless result.nil?
unit_role.user unless unit_role.nil?
end

def name
Expand Down

0 comments on commit 0e2cb6b

Please sign in to comment.