From 290099e2f4a271bef89ee999fc15de06f2903406 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Wed, 21 Dec 2022 15:34:54 -0800 Subject: [PATCH] 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 --- app/blueprints/api/v1/party_blueprint.rb | 41 +++-- app/controllers/api/v1/api_controller.rb | 13 +- app/controllers/api/v1/parties_controller.rb | 156 ++++++++----------- config/routes.rb | 3 - 4 files changed, 107 insertions(+), 106 deletions(-) diff --git a/app/blueprints/api/v1/party_blueprint.rb b/app/blueprints/api/v1/party_blueprint.rb index 20927a1..969760c 100644 --- a/app/blueprints/api/v1/party_blueprint.rb +++ b/app/blueprints/api/v1/party_blueprint.rb @@ -3,8 +3,28 @@ module Api module V1 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 - fields :id, :name, :element, :shortcode, :favorited, :extra, :created_at, :updated_at + fields :name, :element, :shortcode, :favorited, :extra, :created_at, :updated_at association :raid, blueprint: RaidBlueprint @@ -12,9 +32,7 @@ module Api association :job, blueprint: JobBlueprint - association :weapons, - blueprint: GridWeaponBlueprint, - view: :nested + include_view :weapons end view :full do @@ -30,13 +48,16 @@ module Api } end - association :characters, - blueprint: GridCharacterBlueprint, - view: :nested + include_view :summons + include_view :characters + end - association :summons, - blueprint: GridSummonBlueprint, - view: :nested + view :collection do + include_view :preview + end + + view :destroyed do + fields :name, :description, :created_at, :updated_at end end end diff --git a/app/controllers/api/v1/api_controller.rb b/app/controllers/api/v1/api_controller.rb index e7a7c37..4a14f31 100644 --- a/app/controllers/api/v1/api_controller.rb +++ b/app/controllers/api/v1/api_controller.rb @@ -65,9 +65,16 @@ module Api render action: 'errors', status: :unprocessable_entity end - def render_not_found_response - response = { errors: [{ message: 'Record could not be found.', code: 'not_found' }] } - render 'not_found', status: :not_found + def render_validation_error_response(object) + render json: ErrorBlueprint.render_as_json(nil, errors: object.errors), + 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 def render_unauthorized_response diff --git a/app/controllers/api/v1/parties_controller.rb b/app/controllers/api/v1/parties_controller.rb index 5a7d88a..ccf131d 100644 --- a/app/controllers/api/v1/parties_controller.rb +++ b/app/controllers/api/v1/parties_controller.rb @@ -2,6 +2,8 @@ module Api module V1 + PER_PAGE = 15 + class PartiesController < Api::V1::ApiController before_action :set_from_slug, except: %w[create destroy update index favorites] @@ -11,6 +13,7 @@ module Api @party = Party.new(shortcode: random_string) @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? if job job_skills = JobSkill.where(job: job.id, main: true) @@ -21,122 +24,95 @@ module Api @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 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 def update - if @party.user != current_user - render_unauthorized_response - else - @party.attributes = party_params.except(:skill1_id, :skill2_id, :skill3_id) - end + render_unauthorized_response if @party.user != current_user - 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 def index - @per_page = 15 + conditions = build_conditions(request.params) - now = DateTime.current - unless request.params['recency'].blank? - start_time = - ( - now - request.params['recency'].to_i.seconds - ).to_datetime.beginning_of_day - end + @parties = Party.where(conditions) + .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 } - 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[:weapons_count] = 5..13 + count = Party.where(conditions).count + total_pages = count.to_f / PER_PAGE > 1 ? (count.to_f / PER_PAGE).ceil : 1 - @parties = - Party - .where(conditions) - .order(created_at: :desc) - .paginate(page: request.params[:page], per_page: @per_page) - .each do |party| - party.favorited = - current_user ? party.is_favorited(current_user) : false - end - @count = Party.where(conditions).count - - render :all, status: :ok + render json: PartyBlueprint.render(@parties, + view: :collection, + root: :results, + meta: { + total_pages: total_pages, + count: count + }) end def favorites raise Api::V1::UnauthorizedError unless current_user - @per_page = 15 - - 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 = build_conditions(request.params) 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 + @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) } - render :all, status: :ok - end + count = Party.joins(:favorites).where(conditions).count + total_pages = count.to_f / PER_PAGE > 1 ? (count.to_f / PER_PAGE).ceil : 1 - def destroy - if @party.user != current_user - render_unauthorized_response - elsif @party.destroy - render :destroyed, status: :ok - end - 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 + render json: PartyBlueprint.render(@parties, + view: :collection, + root: :results, + meta: { + total_pages: total_pages, + count: count + }) end 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 num_chars = 6 o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten diff --git a/config/routes.rb b/config/routes.rb index 831dbd7..de3e5f7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -15,9 +15,6 @@ Rails.application.routes.draw do get 'parties/favorites', to: 'parties#favorites' 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/job_skills', to: 'jobs#update_job_skills'