2020-06-23 15:45:38 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Provides methods for Project
|
|
|
|
class ProjectService
|
2023-06-30 11:15:37 +02:00
|
|
|
def search(params, current_user, paginate: true)
|
2020-06-23 15:45:38 +02:00
|
|
|
connection = ActiveRecord::Base.connection
|
|
|
|
return { error: 'invalid adapter' } unless connection.instance_values['config'][:adapter] == 'postgresql'
|
|
|
|
|
2023-06-30 11:15:37 +02:00
|
|
|
search_from_postgre(params, current_user, paginate: paginate)
|
2020-06-23 15:45:38 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-06-30 11:15:37 +02:00
|
|
|
def search_from_postgre(params, current_user, paginate: true)
|
|
|
|
query_params = JSON.parse(params[:search] || "{}")
|
2020-06-23 15:45:38 +02:00
|
|
|
|
|
|
|
records = Project.published_or_drafts(current_user&.statistic_profile&.id)
|
|
|
|
records = Project.user_projects(current_user&.statistic_profile&.id) if query_params['from'] == 'mine'
|
|
|
|
records = Project.collaborations(current_user&.id) if query_params['from'] == 'collaboration'
|
|
|
|
|
|
|
|
records = records.with_machine(query_params['machine_id']) if query_params['machine_id'].present?
|
|
|
|
records = records.with_component(query_params['component_id']) if query_params['component_id'].present?
|
|
|
|
records = records.with_theme(query_params['theme_id']) if query_params['theme_id'].present?
|
2023-06-29 08:47:42 +02:00
|
|
|
records = records.with_project_category(query_params['project_category_id']) if query_params['project_category_id'].present?
|
2020-06-23 15:45:38 +02:00
|
|
|
records = records.with_space(query_params['space_id']) if query_params['space_id'].present?
|
2023-01-19 10:29:23 +01:00
|
|
|
records = records.with_status(query_params['status_id']) if query_params['status_id'].present?
|
2023-06-29 14:32:06 +02:00
|
|
|
|
|
|
|
if query_params['member_id'].present?
|
|
|
|
member = User.find(query_params['member_id'])
|
|
|
|
if member
|
|
|
|
records = records.where(id: Project.user_projects(member.statistic_profile.id)).or(Project.where(id: Project.collaborations(member.id)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-03 11:42:29 +02:00
|
|
|
created_from = Time.zone.parse(query_params['from_date']).beginning_of_day if query_params['from_date'].present?
|
|
|
|
created_to = Time.zone.parse(query_params['to_date']).end_of_day if query_params['to_date'].present?
|
|
|
|
if created_from || created_to
|
|
|
|
records = records.where(created_at: created_from..created_to)
|
|
|
|
end
|
|
|
|
|
2020-06-23 17:06:59 +02:00
|
|
|
records = if query_params['q'].present?
|
|
|
|
records.search(query_params['q'])
|
|
|
|
else
|
|
|
|
records.order(created_at: :desc)
|
|
|
|
end
|
2020-06-23 15:45:38 +02:00
|
|
|
|
2023-06-30 11:15:37 +02:00
|
|
|
records = records.includes(:users, :project_image)
|
|
|
|
records = records.page(params[:page]) if paginate
|
2023-09-19 14:41:06 +02:00
|
|
|
total = paginate ? records.total_count : records.count
|
2023-06-30 11:15:37 +02:00
|
|
|
|
2023-09-19 14:41:06 +02:00
|
|
|
{ total: total, projects: records }
|
2020-06-23 15:45:38 +02:00
|
|
|
end
|
2023-01-19 10:29:23 +01:00
|
|
|
end
|