Save base job skills on job change

This commit is contained in:
Justin Edmund 2022-12-01 04:46:53 -08:00
parent 1ba16a2c61
commit 296fa0187c

View file

@ -1,14 +1,13 @@
class Api::V1::PartiesController < Api::V1::ApiController class Api::V1::PartiesController < Api::V1::ApiController
before_action :set_from_slug, except: ['create', 'destroy', 'update', 'index', 'favorites'] before_action :set_from_slug,
before_action :set, only: ['update', 'destroy'] except: %w[create destroy update index favorites]
before_action :set, only: %w[update destroy]
def create def create
@party = Party.new(shortcode: random_string) @party = Party.new(shortcode: random_string)
@party.extra = party_params['extra'] @party.extra = party_params["extra"]
if current_user @party.user = current_user if current_user
@party.user = current_user
end
render :show, status: :created if @party.save! render :show, status: :created if @party.save!
end end
@ -17,25 +16,54 @@ class Api::V1::PartiesController < Api::V1::ApiController
render_not_found_response if @party.nil? render_not_found_response if @party.nil?
end end
def update
if @party.user != current_user
render_unauthorized_response
else
@party.attributes = party_params
if party_params["job_id"].present?
job_skills = JobSkill.where(job_id: party_params["job_id"], main: true)
job_skills.each_with_index do |skill, i|
@party["skill#{i}_id"] = skill.id
end
ap @party
end
render :update, status: :ok if @party.save!
end
end
def index def index
@per_page = 15 @per_page = 15
now = DateTime.current now = DateTime.current
start_time = (now - request.params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank? start_time =
(
now - request.params["recency"].to_i.seconds
).to_datetime.beginning_of_day unless request.params["recency"].blank?
conditions = {} conditions = {}
conditions[:element] = request.params['element'] unless request.params['element'].blank? conditions[:element] = request.params["element"] unless request.params[
conditions[:raid] = request.params['raid'] unless request.params['raid'].blank? "element"
conditions[:created_at] = start_time..now unless request.params['recency'].blank? ].blank?
conditions[:raid] = request.params["raid"] unless request.params[
"raid"
].blank?
conditions[:created_at] = start_time..now unless request.params[
"recency"
].blank?
conditions[:weapons_count] = 5..13 conditions[:weapons_count] = 5..13
@parties = Party @parties =
Party
.where(conditions) .where(conditions)
.order(created_at: :desc) .order(created_at: :desc)
.paginate(page: request.params[:page], per_page: @per_page) .paginate(page: request.params[:page], per_page: @per_page)
.each { |party| .each do |party|
party.favorited = (current_user) ? party.is_favorited(current_user) : false party.favorited =
} (current_user) ? party.is_favorited(current_user) : false
end
@count = Party.where(conditions).count @count = Party.where(conditions).count
render :all, status: :ok render :all, status: :ok
@ -47,36 +75,35 @@ class Api::V1::PartiesController < Api::V1::ApiController
@per_page = 15 @per_page = 15
now = DateTime.current now = DateTime.current
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank? start_time =
(
now - params["recency"].to_i.seconds
).to_datetime.beginning_of_day unless request.params["recency"].blank?
conditions = {} conditions = {}
conditions[:element] = request.params['element'] unless request.params['element'].blank? conditions[:element] = request.params["element"] unless request.params[
conditions[:raid] = request.params['raid'] unless request.params['raid'].blank? "element"
conditions[:created_at] = start_time..now unless request.params['recency'].blank? ].blank?
conditions[:raid] = request.params["raid"] unless request.params[
"raid"
].blank?
conditions[:created_at] = start_time..now unless request.params[
"recency"
].blank?
conditions[:favorites] = { user_id: current_user.id } conditions[:favorites] = { user_id: current_user.id }
@parties = Party @parties =
Party
.joins(:favorites) .joins(:favorites)
.where(conditions) .where(conditions)
.order('favorites.created_at DESC') .order("favorites.created_at DESC")
.paginate(page: request.params[:page], per_page: @per_page) .paginate(page: request.params[:page], per_page: @per_page)
.each { |party| .each { |party| party.favorited = party.is_favorited(current_user) }
party.favorited = party.is_favorited(current_user)
}
@count = Party.joins(:favorites).where(conditions).count @count = Party.joins(:favorites).where(conditions).count
render :all, status: :ok render :all, status: :ok
end end
def update
if @party.user != current_user
render_unauthorized_response
else
@party.attributes = party_params
render :update, status: :ok if @party.save!
end
end
def destroy def destroy
if @party.user != current_user if @party.user != current_user
render_unauthorized_response render_unauthorized_response
@ -104,13 +131,14 @@ class Api::V1::PartiesController < Api::V1::ApiController
def random_string def random_string
numChars = 6 numChars = 6
o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten o = [("a".."z"), ("A".."Z"), (0..9)].map(&:to_a).flatten
return (0...numChars).map { o[rand(o.length)] }.join return (0...numChars).map { o[rand(o.length)] }.join
end end
def set_from_slug def set_from_slug
@party = Party.where("shortcode = ?", params[:id]).first @party = Party.where("shortcode = ?", params[:id]).first
@party.favorited = (current_user && @party) ? @party.is_favorited(current_user) : false @party.favorited =
(current_user && @party) ? @party.is_favorited(current_user) : false
end end
def set def set
@ -118,6 +146,16 @@ class Api::V1::PartiesController < Api::V1::ApiController
end end
def party_params def party_params
params.require(:party).permit(:user_id, :extra, :name, :description, :raid_id, :job_id) params.require(:party).permit(
:user_id,
:extra,
:name,
:description,
:raid_id,
:job_id,
:skill1_id,
:skill2_id,
:skill3_id,
)
end end
end end