Commit 5566c021 authored by Andreas Brandl's avatar Andreas Brandl

Refactor load/prepare/present pattern

parent 5b27e0e0
...@@ -75,15 +75,18 @@ module API ...@@ -75,15 +75,18 @@ module API
mutually_exclusive :import_url, :template_name, :template_project_id mutually_exclusive :import_url, :template_name, :template_project_id
end end
def load_projects def find_projects
ProjectsFinder.new(current_user: current_user, params: project_finder_params).execute ProjectsFinder.new(current_user: current_user, params: project_finder_params).execute
end end
def present_projects(projects, options = {}) # Prepare the full projects query
# None if this is supposed to actually execute any database query
def prepare_query(projects)
projects = reorder_projects(projects) projects = reorder_projects(projects)
projects = apply_filters(projects) projects = apply_filters(projects)
projects = paginate(projects) projects = paginate(projects)
projects, options = with_custom_attributes(projects, options)
projects, options = with_custom_attributes(projects)
options = options.reverse_merge( options = options.reverse_merge(
with: current_user ? Entities::ProjectWithAccess : Entities::BasicProjectDetails, with: current_user ? Entities::ProjectWithAccess : Entities::BasicProjectDetails,
...@@ -91,10 +94,17 @@ module API ...@@ -91,10 +94,17 @@ module API
current_user: current_user, current_user: current_user,
license: false license: false
) )
options[:with] = Entities::BasicProjectDetails if params[:simple] options[:with] = Entities::BasicProjectDetails if params[:simple]
projects = options[:with].prepare_relation(projects, options) projects = options[:with].prepare_relation(projects, options)
[projects, options]
end
def prepare_and_present(project_relation)
projects, options = prepare_query(project_relation)
# Refresh count caches # Refresh count caches
options[:with].execute_batch_counting(projects) options[:with].execute_batch_counting(projects)
...@@ -123,7 +133,7 @@ module API ...@@ -123,7 +133,7 @@ module API
params[:user] = user params[:user] = user
present_projects load_projects prepare_and_present find_projects
end end
desc 'Get projects starred by a user' do desc 'Get projects starred by a user' do
...@@ -139,7 +149,7 @@ module API ...@@ -139,7 +149,7 @@ module API
not_found!('User') unless user not_found!('User') unless user
starred_projects = StarredProjectsFinder.new(user, params: project_finder_params, current_user: current_user).execute starred_projects = StarredProjectsFinder.new(user, params: project_finder_params, current_user: current_user).execute
present_projects starred_projects prepare_and_present starred_projects
end end
end end
...@@ -155,7 +165,7 @@ module API ...@@ -155,7 +165,7 @@ module API
use :with_custom_attributes use :with_custom_attributes
end end
get do get do
present_projects load_projects prepare_and_present find_projects
end end
desc 'Create new project' do desc 'Create new project' do
...@@ -292,7 +302,7 @@ module API ...@@ -292,7 +302,7 @@ module API
get ':id/forks' do get ':id/forks' do
forks = ForkProjectsFinder.new(user_project, params: project_finder_params, current_user: current_user).execute forks = ForkProjectsFinder.new(user_project, params: project_finder_params, current_user: current_user).execute
present_projects forks prepare_and_present forks
end end
desc 'Check pages access of this project' desc 'Check pages access of this project'
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment