Save base job skills on job change
This commit is contained in:
parent
1ba16a2c61
commit
296fa0187c
1 changed files with 141 additions and 103 deletions
|
|
@ -1,123 +1,161 @@
|
||||||
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
|
|
||||||
|
render :show, status: :created if @party.save!
|
||||||
|
end
|
||||||
|
|
||||||
|
def show
|
||||||
|
render_not_found_response if @party.nil?
|
||||||
|
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
|
end
|
||||||
|
ap @party
|
||||||
|
end
|
||||||
|
|
||||||
render :show, status: :created if @party.save!
|
render :update, status: :ok if @party.save!
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def show
|
def index
|
||||||
render_not_found_response if @party.nil?
|
@per_page = 15
|
||||||
end
|
|
||||||
|
|
||||||
def index
|
now = DateTime.current
|
||||||
@per_page = 15
|
start_time =
|
||||||
|
(
|
||||||
|
now - request.params["recency"].to_i.seconds
|
||||||
|
).to_datetime.beginning_of_day unless request.params["recency"].blank?
|
||||||
|
|
||||||
now = DateTime.current
|
conditions = {}
|
||||||
start_time = (now - request.params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank?
|
conditions[:element] = request.params["element"] unless request.params[
|
||||||
|
"element"
|
||||||
|
].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 = {}
|
@parties =
|
||||||
conditions[:element] = request.params['element'] unless request.params['element'].blank?
|
Party
|
||||||
conditions[:raid] = request.params['raid'] unless request.params['raid'].blank?
|
.where(conditions)
|
||||||
conditions[:created_at] = start_time..now unless request.params['recency'].blank?
|
.order(created_at: :desc)
|
||||||
conditions[:weapons_count] = 5..13
|
.paginate(page: request.params[:page], per_page: @per_page)
|
||||||
|
.each do |party|
|
||||||
@parties = Party
|
party.favorited =
|
||||||
.where(conditions)
|
(current_user) ? party.is_favorited(current_user) : false
|
||||||
.order(created_at: :desc)
|
|
||||||
.paginate(page: request.params[:page], per_page: @per_page)
|
|
||||||
.each { |party|
|
|
||||||
party.favorited = (current_user) ? party.is_favorited(current_user) : false
|
|
||||||
}
|
|
||||||
@count = Party.where(conditions).count
|
|
||||||
|
|
||||||
render :all, status: :ok
|
|
||||||
end
|
|
||||||
|
|
||||||
def favorites
|
|
||||||
raise Api::V1::UnauthorizedError unless current_user
|
|
||||||
|
|
||||||
@per_page = 15
|
|
||||||
|
|
||||||
now = DateTime.current
|
|
||||||
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank?
|
|
||||||
|
|
||||||
conditions = {}
|
|
||||||
conditions[:element] = request.params['element'] unless request.params['element'].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 }
|
|
||||||
|
|
||||||
@parties = Party
|
|
||||||
.joins(:favorites)
|
|
||||||
.where(conditions)
|
|
||||||
.order('favorites.created_at DESC')
|
|
||||||
.paginate(page: request.params[:page], per_page: @per_page)
|
|
||||||
.each { |party|
|
|
||||||
party.favorited = party.is_favorited(current_user)
|
|
||||||
}
|
|
||||||
@count = Party.joins(:favorites).where(conditions).count
|
|
||||||
|
|
||||||
render :all, status: :ok
|
|
||||||
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
|
||||||
end
|
@count = Party.where(conditions).count
|
||||||
|
|
||||||
def destroy
|
render :all, status: :ok
|
||||||
if @party.user != current_user
|
end
|
||||||
render_unauthorized_response
|
|
||||||
else
|
|
||||||
render :destroyed, status: :ok if @party.destroy
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def weapons
|
def favorites
|
||||||
render_not_found_response if @party.nil?
|
raise Api::V1::UnauthorizedError unless current_user
|
||||||
render :weapons, status: :ok
|
|
||||||
end
|
|
||||||
|
|
||||||
def summons
|
@per_page = 15
|
||||||
render_not_found_response if @party.nil?
|
|
||||||
render :summons, status: :ok
|
|
||||||
end
|
|
||||||
|
|
||||||
def characters
|
now = DateTime.current
|
||||||
render_not_found_response if @party.nil?
|
start_time =
|
||||||
render :characters, status: :ok
|
(
|
||||||
end
|
now - params["recency"].to_i.seconds
|
||||||
|
).to_datetime.beginning_of_day unless request.params["recency"].blank?
|
||||||
|
|
||||||
private
|
conditions = {}
|
||||||
|
conditions[:element] = request.params["element"] unless request.params[
|
||||||
|
"element"
|
||||||
|
].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 }
|
||||||
|
|
||||||
def random_string
|
@parties =
|
||||||
numChars = 6
|
Party
|
||||||
o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten
|
.joins(:favorites)
|
||||||
return (0...numChars).map { o[rand(o.length)] }.join
|
.where(conditions)
|
||||||
end
|
.order("favorites.created_at DESC")
|
||||||
|
.paginate(page: request.params[:page], per_page: @per_page)
|
||||||
|
.each { |party| party.favorited = party.is_favorited(current_user) }
|
||||||
|
@count = Party.joins(:favorites).where(conditions).count
|
||||||
|
|
||||||
def set_from_slug
|
render :all, status: :ok
|
||||||
@party = Party.where("shortcode = ?", params[:id]).first
|
end
|
||||||
@party.favorited = (current_user && @party) ? @party.is_favorited(current_user) : false
|
|
||||||
end
|
|
||||||
|
|
||||||
def set
|
def destroy
|
||||||
@party = Party.where("id = ?", params[:id]).first
|
if @party.user != current_user
|
||||||
|
render_unauthorized_response
|
||||||
|
else
|
||||||
|
render :destroyed, status: :ok if @party.destroy
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def party_params
|
def weapons
|
||||||
params.require(:party).permit(:user_id, :extra, :name, :description, :raid_id, :job_id)
|
render_not_found_response if @party.nil?
|
||||||
end
|
render :weapons, status: :ok
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def summons
|
||||||
|
render_not_found_response if @party.nil?
|
||||||
|
render :summons, status: :ok
|
||||||
|
end
|
||||||
|
|
||||||
|
def characters
|
||||||
|
render_not_found_response if @party.nil?
|
||||||
|
render :characters, status: :ok
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def random_string
|
||||||
|
numChars = 6
|
||||||
|
o = [("a".."z"), ("A".."Z"), (0..9)].map(&:to_a).flatten
|
||||||
|
return (0...numChars).map { o[rand(o.length)] }.join
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_from_slug
|
||||||
|
@party = Party.where("shortcode = ?", params[:id]).first
|
||||||
|
@party.favorited =
|
||||||
|
(current_user && @party) ? @party.is_favorited(current_user) : false
|
||||||
|
end
|
||||||
|
|
||||||
|
def set
|
||||||
|
@party = Party.where("id = ?", params[:id]).first
|
||||||
|
end
|
||||||
|
|
||||||
|
def party_params
|
||||||
|
params.require(:party).permit(
|
||||||
|
:user_id,
|
||||||
|
:extra,
|
||||||
|
:name,
|
||||||
|
:description,
|
||||||
|
:raid_id,
|
||||||
|
:job_id,
|
||||||
|
:skill1_id,
|
||||||
|
:skill2_id,
|
||||||
|
:skill3_id,
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue