Skip to content

Commit

Permalink
Add User Skills controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Rheniery committed Sep 24, 2024
1 parent c915e6e commit b135a70
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 5 deletions.
62 changes: 62 additions & 0 deletions app/controllers/user_skills_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
class UserSkillsController < ApplicationController
before_action :set_user_skill, only: [:show, :update, :destroy]

# GET /user_skills?user_id=:id
def index
@user_skills = UserSkill.where(user_id: params[:user_id])

render json: @user_skills
end

# POST /user_skills
def create
@user_skill = UserSkill.new(user_skill_params)

if @user_skill.save
render json: @user_skill, status: :created, location: @user_skill
else
render json: @user_skill.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /user_skills
def bulk_update
ApplicationRecord.transaction do
# Remove all existing skills for the user
UserSkill.where(user_id: params[:user_id]).destroy_all

# Add the new list of user skills
params[:user_skills].each do |user_skill|
new_skill = UserSkill.new(user_skill.merge(user_id: params[:user_id]))
unless new_skill.save
raise ActiveRecord::Rollback
end
end
end

# Render success or errors
if @user_skill.errors.empty?
render json: { message: "Skills updated successfully" }
else
render json: @user_skill.errors, status: :unprocessable_entity
end
end

# DELETE /user_skills/:id
def destroy
@user_skill.destroy
head :no_content
end

private

# Use callbacks to share common setup or constraints between actions.
def set_user_skill
@user_skill = UserSkill.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def user_skill_params
params.require(:user_skill).permit(:last_applied_in_year, :level, :years_of_experience, :skill_id, :user_id)
end
end
6 changes: 5 additions & 1 deletion app/models/skill.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_skills_on_lower_name (lower((name)::text)) UNIQUE
#
class Skill < ApplicationRecord
has_many :user_skills, dependent: :destroy
has_many :users, through: :user_skills

validates :name, presence: true
validates :name, presence: true, uniqueness: { case_sensitive: false }
end
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@
resources :skills, only: [:index]
resources :issues, only: [:index]
resources :permissions, only: [:index]
resources :user_skills
end
5 changes: 5 additions & 0 deletions db/migrate/20240923192947_add_unique_index_to_skills_name.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddUniqueIndexToSkillsName < ActiveRecord::Migration[7.0]
def change
add_index :skills, "LOWER(name)", unique: true, name: "index_skills_on_lower_name"
end
end
4 changes: 2 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions spec/controllers/user_skills_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe UserSkillsController, type: :controller do

end
4 changes: 4 additions & 0 deletions spec/factories/skills.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_skills_on_lower_name (lower((name)::text)) UNIQUE
#
FactoryBot.define do
factory :skill do
name { FFaker::Skill.tech_skill }
Expand Down
4 changes: 2 additions & 2 deletions spec/models/project_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# == Schema Information
#
# Table name: project_auths
# Table name: project_reports
#
# id :bigint not null, primary key
# key :string
Expand All @@ -12,7 +12,7 @@
#
# Indexes
#
# index_project_auths_on_project_id (project_id)
# index_project_reports_on_project_id (project_id)
#
# Foreign Keys
#
Expand Down
4 changes: 4 additions & 0 deletions spec/models/skill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_skills_on_lower_name (lower((name)::text)) UNIQUE
#
require 'rails_helper'

RSpec.describe Skill, type: :model do
Expand Down

0 comments on commit b135a70

Please sign in to comment.