Refactor PartiesController for blueprinter

* Refactored and tried to simplify methods in PartiesController
* Implemented new error render methods in ApiController
* Removed parties/[weapons, summons, characters] routes
This commit is contained in:
Justin Edmund 2022-12-21 15:34:54 -08:00
parent 2fe8b8de1a
commit 290099e2f4
4 changed files with 107 additions and 106 deletions

View file

@ -3,8 +3,28 @@
module Api module Api
module V1 module V1
class PartyBlueprint < ApiBlueprint class PartyBlueprint < ApiBlueprint
identifier :id
view :weapons do
association :weapons,
blueprint: GridWeaponBlueprint,
view: :nested
end
view :summons do
association :summons,
blueprint: GridSummonBlueprint,
view: :nested
end
view :characters do
association :characters,
blueprint: GridCharacterBlueprint,
view: :nested
end
view :preview do view :preview do
fields :id, :name, :element, :shortcode, :favorited, :extra, :created_at, :updated_at fields :name, :element, :shortcode, :favorited, :extra, :created_at, :updated_at
association :raid, association :raid,
blueprint: RaidBlueprint blueprint: RaidBlueprint
@ -12,9 +32,7 @@ module Api
association :job, association :job,
blueprint: JobBlueprint blueprint: JobBlueprint
association :weapons, include_view :weapons
blueprint: GridWeaponBlueprint,
view: :nested
end end
view :full do view :full do
@ -30,13 +48,16 @@ module Api
} }
end end
association :characters, include_view :summons
blueprint: GridCharacterBlueprint, include_view :characters
view: :nested end
association :summons, view :collection do
blueprint: GridSummonBlueprint, include_view :preview
view: :nested end
view :destroyed do
fields :name, :description, :created_at, :updated_at
end end
end end
end end

View file

@ -65,9 +65,16 @@ module Api
render action: 'errors', status: :unprocessable_entity render action: 'errors', status: :unprocessable_entity
end end
def render_not_found_response def render_validation_error_response(object)
response = { errors: [{ message: 'Record could not be found.', code: 'not_found' }] } render json: ErrorBlueprint.render_as_json(nil, errors: object.errors),
render 'not_found', status: :not_found status: :unprocessable_entity
end
def render_not_found_response(object)
render json: ErrorBlueprint.render(nil, error: {
message: "#{object.capitalize} could not be found",
code: 'not_found'
}), status: :not_found
end end
def render_unauthorized_response def render_unauthorized_response

View file

@ -2,6 +2,8 @@
module Api module Api
module V1 module V1
PER_PAGE = 15
class PartiesController < Api::V1::ApiController class PartiesController < Api::V1::ApiController
before_action :set_from_slug, before_action :set_from_slug,
except: %w[create destroy update index favorites] except: %w[create destroy update index favorites]
@ -11,6 +13,7 @@ module Api
@party = Party.new(shortcode: random_string) @party = Party.new(shortcode: random_string)
@party.extra = party_params['extra'] @party.extra = party_params['extra']
# TODO: Extract this into a different method
job = Job.find(party_params['job_id']) if party_params['job_id'].present? job = Job.find(party_params['job_id']) if party_params['job_id'].present?
if job if job
job_skills = JobSkill.where(job: job.id, main: true) job_skills = JobSkill.where(job: job.id, main: true)
@ -21,122 +24,95 @@ module Api
@party.user = current_user if current_user @party.user = current_user if current_user
render :show, status: :created if @party.save! if @party.save!
return render json: PartyBlueprint.render(@party, view: :full, root: :party),
status: :created
end
render_validation_error_response(@party)
end end
def show def show
render_not_found_response if @party.nil? return render json: PartyBlueprint.render(@party, view: :full, root: :party) if @party
render_not_found_response('project')
end end
def update def update
if @party.user != current_user render_unauthorized_response if @party.user != current_user
render_unauthorized_response
else
@party.attributes = party_params.except(:skill1_id, :skill2_id, :skill3_id)
end
render :update, status: :ok if @party.save! @party.attributes = party_params.except(:skill1_id, :skill2_id, :skill3_id)
return render json: PartyBlueprint.render(@party, view: :full, root: :party) if @party.save!
render_validation_error_response(@party)
end
def destroy
render_unauthorized_response if @party.user != current_user
return render json: PartyBlueprint.render(@party, view: :destroyed, root: :checkin) if @party.destroy
end end
def index def index
@per_page = 15 conditions = build_conditions(request.params)
now = DateTime.current @parties = Party.where(conditions)
unless request.params['recency'].blank? .order(created_at: :desc)
start_time = .paginate(page: request.params[:page], per_page: PER_PAGE)
( .each { |party| party.favorited = current_user ? party.is_favorited(current_user) : false }
now - request.params['recency'].to_i.seconds
).to_datetime.beginning_of_day
end
conditions = {} count = Party.where(conditions).count
conditions[:element] = request.params['element'] unless request.params[ total_pages = count.to_f / PER_PAGE > 1 ? (count.to_f / PER_PAGE).ceil : 1
'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
@parties = render json: PartyBlueprint.render(@parties,
Party view: :collection,
.where(conditions) root: :results,
.order(created_at: :desc) meta: {
.paginate(page: request.params[:page], per_page: @per_page) total_pages: total_pages,
.each do |party| count: count
party.favorited = })
current_user ? party.is_favorited(current_user) : false
end
@count = Party.where(conditions).count
render :all, status: :ok
end end
def favorites def favorites
raise Api::V1::UnauthorizedError unless current_user raise Api::V1::UnauthorizedError unless current_user
@per_page = 15 conditions = build_conditions(request.params)
now = DateTime.current
unless request.params['recency'].blank?
start_time =
(
now - params['recency'].to_i.seconds
).to_datetime.beginning_of_day
end
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 } conditions[:favorites] = { user_id: current_user.id }
@parties = @parties = Party.joins(:favorites)
Party .where(conditions)
.joins(:favorites) .order('favorites.created_at DESC')
.where(conditions) .paginate(page: request.params[:page], per_page: PER_PAGE)
.order('favorites.created_at DESC') .each { |party| party.favorited = party.is_favorited(current_user) }
.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 count = Party.joins(:favorites).where(conditions).count
end total_pages = count.to_f / PER_PAGE > 1 ? (count.to_f / PER_PAGE).ceil : 1
def destroy render json: PartyBlueprint.render(@parties,
if @party.user != current_user view: :collection,
render_unauthorized_response root: :results,
elsif @party.destroy meta: {
render :destroyed, status: :ok total_pages: total_pages,
end count: count
end })
def weapons
render_not_found_response if @party.nil?
render :weapons, status: :ok
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 end
private private
def build_conditions(params)
unless params['recency'].blank?
start_time = (DateTime.current - params['recency'].to_i.seconds)
.to_datetime.beginning_of_day
end
{
element: (params['element'] unless params['element'].blank?),
raid: (params['raid'] unless params['raid'].blank?),
created_at: (start_time..now unless params['recency'].blank?),
weapons_count: 5..13
}
end
def random_string def random_string
num_chars = 6 num_chars = 6
o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten

View file

@ -15,9 +15,6 @@ Rails.application.routes.draw do
get 'parties/favorites', to: 'parties#favorites' get 'parties/favorites', to: 'parties#favorites'
get 'parties/:id', to: 'parties#show' get 'parties/:id', to: 'parties#show'
get 'parties/:id/weapons', to: 'parties#weapons'
get 'parties/:id/summons', to: 'parties#summons'
get 'parties/:id/characters', to: 'parties#characters'
put 'parties/:id/jobs', to: 'jobs#update_job' put 'parties/:id/jobs', to: 'jobs#update_job'
put 'parties/:id/job_skills', to: 'jobs#update_job_skills' put 'parties/:id/job_skills', to: 'jobs#update_job_skills'