Skip to content

Commit

Permalink
Move search users by skills to users controller
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandromontividiu03 committed Nov 1, 2024
1 parent 1d72607 commit 2c10b7a
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 21 deletions.
2 changes: 0 additions & 2 deletions app/controllers/skills_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# frozen_string_literal: true

class SkillsController < ApplicationController

def index
@skills = Skill.all.sort_by(&:name)
end
Expand All @@ -18,7 +17,6 @@ def create

private


# Only allow a trusted parameter "white list" through.
def skill_params
params.require(:skill).permit(:name)
Expand Down
16 changes: 3 additions & 13 deletions app/controllers/user_skills_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ def bulk_update
ApplicationRecord.transaction do
# Remove all existing skills for the user
UserSkill.where(user_id: params.dig(:params, :user_id)).destroy_all

# Permit and process the new list of user skills
user_skills_list_params.each do |user_skill|
new_skill = UserSkill.new(user_skill.merge(user_id: params.dig(:params, :user_id)))
unless new_skill.save
raise ActiveRecord::Rollback
end
raise ActiveRecord::Rollback unless new_skill.save
end
end

render json: { message: 'Skills updated successfully' }, status: :ok
end

Expand All @@ -34,14 +32,6 @@ 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.
Expand Down
4 changes: 4 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ class UsersController < ApplicationController

def index
query = params['query']&.split(',')
skills_filter = params['filter_by_skills']&.split(/[\s,]+/)&.map(&:downcase)

@users = if query
User.by_external_identifier(query)
else
User.all
end

@users = @users.joins(:skills).where('LOWER(skills.name) IN (?)', skills_filter) if skills_filter.present?
@users = @users.order(:first_name, :last_name)
end

Expand Down
2 changes: 1 addition & 1 deletion app/views/user_skills/_user_skill.json.jbuilder
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

# _user_service_identifier.json.jbuilder

json.extract! user_skill, :id, :last_applied_in_year, :level, :years_of_experience, :skill
json.extract! user_skill, :id, :last_applied_in_year, :level, :years_of_experience, :skill
3 changes: 1 addition & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,12 @@
resources :users
resources :customers
resources :professions, only: [:index]
resources :skills, only: [:index, :create]
resources :skills, only: %i[index create]
resources :issues, only: [:index]
resources :permissions, only: [:index]
resources :user_skills, only: [:index] do
collection do
patch :bulk_update
get :search
end
end
end
Expand Down
9 changes: 6 additions & 3 deletions spec/controllers/user_skills_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
require 'rails_helper'

RSpec.describe UserSkillsController, type: :controller do
let(:user) { double("User", id: 1) }
let(:skill) { double("Skill", id: 1, name: 'Ruby') }
let(:user_skill) { double("UserSkill", id: 1, user_id: user.id, skill_id: skill.id, years_of_experience: 3, last_applied_in_year: 2021, level: 'beginner') }
let(:user) { double('User', id: 1) }
let(:skill) { double('Skill', id: 1, name: 'Ruby') }
let(:user_skill) do
double('UserSkill', id: 1, user_id: user.id, skill_id: skill.id, years_of_experience: 3, last_applied_in_year: 2021,
level: 'beginner')
end

before do
# Mocking UserSkill model interactions
Expand Down

0 comments on commit 2c10b7a

Please sign in to comment.