Normalize constants in ApiController
This commit is contained in:
parent
bb15db45f6
commit
574d8e0b3e
5 changed files with 24 additions and 25 deletions
|
|
@ -6,6 +6,10 @@ module Api
|
||||||
##### Doorkeeper
|
##### Doorkeeper
|
||||||
include Doorkeeper::Rails::Helpers
|
include Doorkeeper::Rails::Helpers
|
||||||
|
|
||||||
|
##### Constants
|
||||||
|
COLLECTION_PER_PAGE = 15
|
||||||
|
SEARCH_PER_PAGE = 10
|
||||||
|
|
||||||
##### Errors
|
##### Errors
|
||||||
rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response
|
rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response
|
||||||
rescue_from ActiveRecord::RecordNotDestroyed, with: :render_unprocessable_entity_response
|
rescue_from ActiveRecord::RecordNotDestroyed, with: :render_unprocessable_entity_response
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,8 @@ module Api
|
||||||
:id, :party_id, :weapon_id,
|
:id, :party_id, :weapon_id,
|
||||||
:position, :mainhand, :uncap_level, :element,
|
:position, :mainhand, :uncap_level, :element,
|
||||||
:weapon_key1_id, :weapon_key2_id, :weapon_key3_id,
|
:weapon_key1_id, :weapon_key2_id, :weapon_key3_id,
|
||||||
:ax_modifier1, :ax_modifier2, :ax_strength1, :ax_strength2
|
:ax_modifier1, :ax_modifier2, :ax_strength1, :ax_strength2,
|
||||||
|
:awakening_type, :awakening_level
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
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]
|
||||||
|
|
@ -59,11 +57,11 @@ module Api
|
||||||
|
|
||||||
@parties = Party.where(conditions)
|
@parties = Party.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: COLLECTION_PER_PAGE)
|
||||||
.each { |party| party.favorited = current_user ? party.is_favorited(current_user) : false }
|
.each { |party| party.favorited = current_user ? party.is_favorited(current_user) : false }
|
||||||
|
|
||||||
count = Party.where(conditions).count
|
count = Party.where(conditions).count
|
||||||
total_pages = count.to_f / PER_PAGE > 1 ? (count.to_f / PER_PAGE).ceil : 1
|
total_pages = count.to_f / COLLECTION_PER_PAGE > 1 ? (count.to_f / COLLECTION_PER_PAGE).ceil : 1
|
||||||
|
|
||||||
render json: PartyBlueprint.render(@parties,
|
render json: PartyBlueprint.render(@parties,
|
||||||
view: :collection,
|
view: :collection,
|
||||||
|
|
@ -71,7 +69,7 @@ module Api
|
||||||
meta: {
|
meta: {
|
||||||
count: count,
|
count: count,
|
||||||
total_pages: total_pages,
|
total_pages: total_pages,
|
||||||
per_page: PER_PAGE
|
per_page: COLLECTION_PER_PAGE
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -84,11 +82,11 @@ module Api
|
||||||
@parties = Party.joins(:favorites)
|
@parties = Party.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: COLLECTION_PER_PAGE)
|
||||||
.each { |party| party.favorited = party.is_favorited(current_user) }
|
.each { |party| party.favorited = party.is_favorited(current_user) }
|
||||||
|
|
||||||
count = Party.joins(:favorites).where(conditions).count
|
count = Party.joins(:favorites).where(conditions).count
|
||||||
total_pages = count.to_f / PER_PAGE > 1 ? (count.to_f / PER_PAGE).ceil : 1
|
total_pages = count.to_f / COLLECTION_PER_PAGE > 1 ? (count.to_f / COLLECTION_PER_PAGE).ceil : 1
|
||||||
|
|
||||||
render json: PartyBlueprint.render(@parties,
|
render json: PartyBlueprint.render(@parties,
|
||||||
view: :collection,
|
view: :collection,
|
||||||
|
|
@ -96,7 +94,7 @@ module Api
|
||||||
meta: {
|
meta: {
|
||||||
count: count,
|
count: count,
|
||||||
total_pages: total_pages,
|
total_pages: total_pages,
|
||||||
per_page: PER_PAGE
|
per_page: COLLECTION_PER_PAGE
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
module Api
|
module Api
|
||||||
module V1
|
module V1
|
||||||
PER_PAGE = 10
|
|
||||||
|
|
||||||
class SearchController < Api::V1::ApiController
|
class SearchController < Api::V1::ApiController
|
||||||
def characters
|
def characters
|
||||||
filters = search_params[:filters]
|
filters = search_params[:filters]
|
||||||
|
|
@ -35,14 +33,14 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
count = characters.length
|
count = characters.length
|
||||||
paginated = characters.paginate(page: search_params[:page], per_page: PER_PAGE)
|
paginated = characters.paginate(page: search_params[:page], per_page: SEARCH_PER_PAGE)
|
||||||
|
|
||||||
render json: CharacterBlueprint.render(paginated,
|
render json: CharacterBlueprint.render(paginated,
|
||||||
root: :results,
|
root: :results,
|
||||||
meta: {
|
meta: {
|
||||||
count: count,
|
count: count,
|
||||||
total_pages: total_pages(count),
|
total_pages: total_pages(count),
|
||||||
per_page: PER_PAGE
|
per_page: SEARCH_PER_PAGE
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -72,14 +70,14 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
count = weapons.length
|
count = weapons.length
|
||||||
paginated = weapons.paginate(page: search_params[:page], per_page: PER_PAGE)
|
paginated = weapons.paginate(page: search_params[:page], per_page: SEARCH_PER_PAGE)
|
||||||
|
|
||||||
render json: WeaponBlueprint.render(paginated,
|
render json: WeaponBlueprint.render(paginated,
|
||||||
root: :results,
|
root: :results,
|
||||||
meta: {
|
meta: {
|
||||||
count: count,
|
count: count,
|
||||||
total_pages: total_pages(count),
|
total_pages: total_pages(count),
|
||||||
per_page: PER_PAGE
|
per_page: SEARCH_PER_PAGE
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -104,14 +102,14 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
count = summons.length
|
count = summons.length
|
||||||
paginated = summons.paginate(page: search_params[:page], per_page: PER_PAGE)
|
paginated = summons.paginate(page: search_params[:page], per_page: SEARCH_PER_PAGE)
|
||||||
|
|
||||||
render json: SummonBlueprint.render(paginated,
|
render json: SummonBlueprint.render(paginated,
|
||||||
root: :results,
|
root: :results,
|
||||||
meta: {
|
meta: {
|
||||||
count: count,
|
count: count,
|
||||||
total_pages: total_pages(count),
|
total_pages: total_pages(count),
|
||||||
per_page: PER_PAGE
|
per_page: SEARCH_PER_PAGE
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -168,21 +166,21 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
count = skills.length
|
count = skills.length
|
||||||
paginated = skills.paginate(page: search_params[:page], per_page: PER_PAGE)
|
paginated = skills.paginate(page: search_params[:page], per_page: SEARCH_PER_PAGE)
|
||||||
|
|
||||||
render json: JobSkillBlueprint.render(paginated,
|
render json: JobSkillBlueprint.render(paginated,
|
||||||
root: :results,
|
root: :results,
|
||||||
meta: {
|
meta: {
|
||||||
count: count,
|
count: count,
|
||||||
total_pages: total_pages(count),
|
total_pages: total_pages(count),
|
||||||
per_page: PER_PAGE
|
per_page: SEARCH_PER_PAGE
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def total_pages(count)
|
def total_pages(count)
|
||||||
count.to_f / PER_PAGE > 1 ? (count.to_f / PER_PAGE).ceil : 1
|
count.to_f / SEARCH_PER_PAGE > 1 ? (count.to_f / SEARCH_PER_PAGE).ceil : 1
|
||||||
end
|
end
|
||||||
|
|
||||||
# Specify whitelisted properties that can be modified.
|
# Specify whitelisted properties that can be modified.
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,6 @@
|
||||||
|
|
||||||
module Api
|
module Api
|
||||||
module V1
|
module V1
|
||||||
PER_PAGE = 15
|
|
||||||
|
|
||||||
class UsersController < Api::V1::ApiController
|
class UsersController < Api::V1::ApiController
|
||||||
class ForbiddenError < StandardError; end
|
class ForbiddenError < StandardError; end
|
||||||
|
|
||||||
|
|
@ -50,7 +48,7 @@ module Api
|
||||||
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: COLLECTION_PER_PAGE)
|
||||||
.each do |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
|
end
|
||||||
|
|
@ -63,8 +61,8 @@ module Api
|
||||||
parties: parties,
|
parties: parties,
|
||||||
meta: {
|
meta: {
|
||||||
count: count,
|
count: count,
|
||||||
total_pages: count.to_f / PER_PAGE > 1 ? (count.to_f / PER_PAGE).ceil : 1,
|
total_pages: count.to_f / COLLECTION_PER_PAGE > 1 ? (count.to_f / COLLECTION_PER_PAGE).ceil : 1,
|
||||||
per_page: PER_PAGE
|
per_page: COLLECTION_PER_PAGE
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue