From 6f1cd36fb26c1305c78f696e0c8393b369fba7ea Mon Sep 17 00:00:00 2001 From: Alessandro Date: Thu, 31 Oct 2024 16:55:11 -0300 Subject: [PATCH] Add endpoint to search users by skill names --- app/controllers/user_skills_controller.rb | 8 ++++++++ config/routes.rb | 1 + 2 files changed, 9 insertions(+) diff --git a/app/controllers/user_skills_controller.rb b/app/controllers/user_skills_controller.rb index b9964d8..5a53e3c 100644 --- a/app/controllers/user_skills_controller.rb +++ b/app/controllers/user_skills_controller.rb @@ -36,6 +36,14 @@ def destroy head :not_found end + # GET /user_skills/search?query=:query + def search + return render json: [] if params[:query].blank? + query = params[:query].split(/[\s,]+/).map(&:downcase) + users = UserSkill.joins(:skill).where('LOWER(skills.name) IN (?)', query).pluck(:user_id).uniq + render json: User.where(id: users) + end + private # Use callbacks to share common setup or constraints between actions. diff --git a/config/routes.rb b/config/routes.rb index 361f39f..6640726 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,6 +41,7 @@ resources :user_skills, only: [:index] do collection do patch :bulk_update + get :search end end end