Merge branch 'main' of github.com:jedmund/hensei-api

This commit is contained in:
Justin Edmund 2022-12-23 19:24:12 -08:00
commit f17be0ea03
5 changed files with 24 additions and 25 deletions

View file

@ -6,6 +6,10 @@ module Api
##### Doorkeeper
include Doorkeeper::Rails::Helpers
##### Constants
COLLECTION_PER_PAGE = 15
SEARCH_PER_PAGE = 10
##### Errors
rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response
rescue_from ActiveRecord::RecordNotDestroyed, with: :render_unprocessable_entity_response

View file

@ -67,7 +67,8 @@ module Api
:id, :party_id, :weapon_id,
:position, :mainhand, :uncap_level, :element,
: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

View file

@ -2,8 +2,6 @@
module Api
module V1
PER_PAGE = 15
class PartiesController < Api::V1::ApiController
before_action :set_from_slug,
except: %w[create destroy update index favorites]
@ -58,11 +56,11 @@ module Api
@parties = Party.where(conditions)
.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 }
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,
view: :collection,
@ -70,7 +68,7 @@ module Api
meta: {
count: count,
total_pages: total_pages,
per_page: PER_PAGE
per_page: COLLECTION_PER_PAGE
})
end
@ -83,11 +81,11 @@ module Api
@parties = Party.joins(:favorites)
.where(conditions)
.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) }
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,
view: :collection,
@ -95,7 +93,7 @@ module Api
meta: {
count: count,
total_pages: total_pages,
per_page: PER_PAGE
per_page: COLLECTION_PER_PAGE
})
end

View file

@ -2,8 +2,6 @@
module Api
module V1
PER_PAGE = 10
class SearchController < Api::V1::ApiController
def characters
filters = search_params[:filters]
@ -35,14 +33,14 @@ module Api
end
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,
root: :results,
meta: {
count: count,
total_pages: total_pages(count),
per_page: PER_PAGE
per_page: SEARCH_PER_PAGE
})
end
@ -72,14 +70,14 @@ module Api
end
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,
root: :results,
meta: {
count: count,
total_pages: total_pages(count),
per_page: PER_PAGE
per_page: SEARCH_PER_PAGE
})
end
@ -104,14 +102,14 @@ module Api
end
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,
root: :results,
meta: {
count: count,
total_pages: total_pages(count),
per_page: PER_PAGE
per_page: SEARCH_PER_PAGE
})
end
@ -168,21 +166,21 @@ module Api
end
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,
root: :results,
meta: {
count: count,
total_pages: total_pages(count),
per_page: PER_PAGE
per_page: SEARCH_PER_PAGE
})
end
private
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
# Specify whitelisted properties that can be modified.

View file

@ -2,8 +2,6 @@
module Api
module V1
PER_PAGE = 15
class UsersController < Api::V1::ApiController
class ForbiddenError < StandardError; end
@ -50,7 +48,7 @@ module Api
parties = Party
.where(conditions)
.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|
party.favorited = current_user ? party.is_favorited(current_user) : false
end
@ -63,8 +61,8 @@ module Api
parties: parties,
meta: {
count: count,
total_pages: count.to_f / PER_PAGE > 1 ? (count.to_f / PER_PAGE).ceil : 1,
per_page: PER_PAGE
total_pages: count.to_f / COLLECTION_PER_PAGE > 1 ? (count.to_f / COLLECTION_PER_PAGE).ceil : 1,
per_page: COLLECTION_PER_PAGE
})
end