debian-mirror-gitlab/app/services/packages/conan/search_service.rb

54 lines
1.2 KiB
Ruby
Raw Normal View History

2020-07-28 23:09:34 +05:30
# frozen_string_literal: true
module Packages
module Conan
class SearchService < BaseService
include ActiveRecord::Sanitization::ClassMethods
WILDCARD = '*'
RECIPE_SEPARATOR = '@'
def execute
ServiceResponse.success(payload: { results: search_results })
end
private
def search_results
return [] if wildcard_query?
return search_for_single_package(sanitized_query) if params[:query].include?(RECIPE_SEPARATOR)
2023-07-09 08:55:56 +05:30
search_packages
2020-07-28 23:09:34 +05:30
end
def wildcard_query?
params[:query] == WILDCARD
end
2023-07-09 08:55:56 +05:30
def sanitized_query
@sanitized_query ||= sanitize_sql_like(params[:query].delete(WILDCARD))
2020-07-28 23:09:34 +05:30
end
def search_for_single_package(query)
2023-07-09 08:55:56 +05:30
::Packages::Conan::SinglePackageSearchService
.new(query, current_user)
.execute[:results]
end
2020-07-28 23:09:34 +05:30
2023-07-09 08:55:56 +05:30
def search_packages
::Packages::Conan::PackageFinder
.new(current_user, { query: build_query }, project: project)
.execute
.map(&:conan_recipe)
2020-07-28 23:09:34 +05:30
end
2023-07-09 08:55:56 +05:30
def build_query
return "#{sanitized_query}%" if params[:query].end_with?(WILDCARD)
sanitized_query
2020-07-28 23:09:34 +05:30
end
end
end
end