Update JobsController and JobSkillsController for blueprinter

This commit is contained in:
Justin Edmund 2022-12-21 21:46:35 -08:00
parent 349c42f839
commit 9a27ac853d
3 changed files with 25 additions and 19 deletions

View file

@ -21,6 +21,17 @@ module Api
view: :nested
end
view :job_skills do
field :job_skills do |job|
{
'0' => !job.skill0.nil? ? JobSkillBlueprint.render_as_hash(job.skill0) : nil,
'1' => !job.skill1.nil? ? JobSkillBlueprint.render_as_hash(job.skill1) : nil,
'2' => !job.skill2.nil? ? JobSkillBlueprint.render_as_hash(job.skill2) : nil,
'3' => !job.skill3.nil? ? JobSkillBlueprint.render_as_hash(job.skill3) : nil
}
end
end
view :minimal do
fields :name, :element, :shortcode, :favorited, :extra, :created_at, :updated_at
@ -31,6 +42,12 @@ module Api
blueprint: JobBlueprint
end
view :jobs do
association :job,
blueprint: JobBlueprint
include_view :job_skills
end
view :preview do
include_view :minimal
include_view :weapons
@ -40,17 +57,9 @@ module Api
include_view :preview
include_view :summons
include_view :characters
include_view :job_skills
fields :description, :extra
field :job_skills do |job|
{
'0' => !job.skill0.nil? ? JobSkillBlueprint.render_as_hash(job.skill0) : nil,
'1' => !job.skill1.nil? ? JobSkillBlueprint.render_as_hash(job.skill1) : nil,
'2' => !job.skill2.nil? ? JobSkillBlueprint.render_as_hash(job.skill2) : nil,
'3' => !job.skill3.nil? ? JobSkillBlueprint.render_as_hash(job.skill3) : nil
}
end
end
view :collection do

View file

@ -4,15 +4,13 @@ module Api
module V1
class JobSkillsController < Api::V1::ApiController
def all
@skills = JobSkill.all
render :all, status: :ok
render json: JobSkillBlueprint.render(JobSkill.all)
end
def job
job = Job.find(params[:id])
@skills = JobSkill.where(job: job).or(JobSkill.where(sub: true))
render :all, status: :ok
@skills = JobSkill.where(job: Job.find(params[:id]))
.or(JobSkill.where(sub: true))
render json: JobSkillBlueprint.render(@skills)
end
end
end

View file

@ -6,8 +6,7 @@ module Api
before_action :set, only: %w[update_job update_job_skills]
def all
@jobs = Job.all
render :all, status: :ok
render json: JobBlueprint.render(Job.all)
end
def update_job
@ -28,7 +27,7 @@ module Api
@party[key] = nil if @party[key] && mismatched_skill(@party.job, JobSkill.find(@party[key]))
end
render :update, status: :ok if @party.save!
render json: PartyBlueprint.render(@party, view: :jobs) if @party.save!
end
def update_job_skills
@ -63,7 +62,7 @@ module Api
@party.attributes = new_skill_ids
end
render :update, status: :ok if @party.save!
render json: PartyBlueprint.render(@party, view: :jobs) if @party.save!
end
private