Run RuboCop on everything
yolo
This commit is contained in:
parent
1fe1c1bb36
commit
c1716c1e4f
45 changed files with 1251 additions and 1132 deletions
|
|
@ -1,4 +1,7 @@
|
|||
module Api::V1
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class ApiController < ActionController::API
|
||||
##### Doorkeeper
|
||||
include Doorkeeper::Rails::Helpers
|
||||
|
|
@ -34,18 +37,18 @@ module Api::V1
|
|||
def current_user
|
||||
@current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
|
||||
|
||||
return @current_user
|
||||
@current_user
|
||||
end
|
||||
|
||||
# Set the response content-type
|
||||
def set_content_type(content_type)
|
||||
response.headers["Content-Type"] = content_type
|
||||
response.headers['Content-Type'] = content_type
|
||||
end
|
||||
|
||||
# Set the default response content-type to application/javascript
|
||||
# with a UTF-8 charset
|
||||
def set_default_content_type
|
||||
set_content_type("application/javascript; charset=utf-8")
|
||||
set_content_type('application/javascript; charset=utf-8')
|
||||
end
|
||||
|
||||
### Error response methods
|
||||
|
|
@ -63,7 +66,7 @@ module Api::V1
|
|||
end
|
||||
|
||||
def render_not_found_response
|
||||
response = { errors: [{ message: "Record could not be found.", code: "not_found" }] }
|
||||
response = { errors: [{ message: 'Record could not be found.', code: 'not_found' }] }
|
||||
render 'not_found', status: :not_found
|
||||
end
|
||||
|
||||
|
|
@ -77,4 +80,5 @@ module Api::V1
|
|||
raise UnauthorizedError unless current_user
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,26 +1,25 @@
|
|||
class Api::V1::FavoritesController < Api::V1::ApiController
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class FavoritesController < Api::V1::ApiController
|
||||
before_action :set_party, only: ['create']
|
||||
|
||||
def create
|
||||
party_id = favorite_params[:party_id]
|
||||
party = Party.find(party_id)
|
||||
|
||||
if !current_user
|
||||
raise Api::V1::UnauthorizedError
|
||||
elsif party.user && current_user.id == party.user.id
|
||||
raise Api::V1::SameFavoriteUserError
|
||||
elsif Favorite.where(user_id: current_user.id, party_id: party_id).length > 0
|
||||
raise Api::V1::FavoriteAlreadyExistsError
|
||||
else
|
||||
object = {
|
||||
user_id: current_user.id,
|
||||
party_id: favorite_params[:party_id]
|
||||
}
|
||||
raise Api::V1::UnauthorizedError unless current_user
|
||||
raise Api::V1::SameFavoriteUserError if party.user && current_user.id == party.user.id
|
||||
raise Api::V1::FavoriteAlreadyExistsError if Favorite.where(user_id: current_user.id,
|
||||
party_id: party_id).length.positive?
|
||||
|
||||
@favorite = Favorite.new(object)
|
||||
@favorite = Favorite.new({
|
||||
user_id: current_user.id,
|
||||
party_id: party_id
|
||||
})
|
||||
render :show, status: :created if @favorite.save!
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
raise Api::V1::UnauthorizedError unless current_user
|
||||
|
|
@ -32,10 +31,12 @@ class Api::V1::FavoritesController < Api::V1::ApiController
|
|||
private
|
||||
|
||||
def set_party
|
||||
@party = Party.where("id = ?", params[:party_id]).first
|
||||
@party = Party.where('id = ?', params[:party_id]).first
|
||||
end
|
||||
|
||||
def favorite_params
|
||||
params.require(:favorite).permit(:party_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,26 +1,26 @@
|
|||
class Api::V1::GridCharactersController < Api::V1::ApiController
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class GridCharactersController < Api::V1::ApiController
|
||||
def create
|
||||
party = Party.find(character_params[:party_id])
|
||||
incoming_character = Character.find(character_params[:character_id])
|
||||
|
||||
if current_user
|
||||
if party.user != current_user
|
||||
render_unauthorized_response
|
||||
end
|
||||
end
|
||||
render_unauthorized_response if current_user && (party.user != current_user)
|
||||
|
||||
current_characters = party.characters.map { |c|
|
||||
current_characters = party.characters.map do |c|
|
||||
Character.find(c.character.id).character_id
|
||||
}.flatten
|
||||
end.flatten
|
||||
|
||||
# Check all character ids on incoming character against current characters
|
||||
conflict_ids = (current_characters & incoming_character.character_id)
|
||||
|
||||
if conflict_ids.length > 0
|
||||
if conflict_ids.length.positive?
|
||||
# Find conflicting character ids in party characters
|
||||
conflict_characters = party.characters.filter { |c|
|
||||
c if (conflict_ids & c.character.character_id).length > 0
|
||||
}.flatten
|
||||
conflict_characters = party.characters.filter do |c|
|
||||
c if (conflict_ids & c.character.character_id).length.positive?
|
||||
end.flatten
|
||||
|
||||
# Render a template with the conflicting and incoming characters,
|
||||
# as well as the selected position, so the user can be presented with
|
||||
|
|
@ -40,7 +40,8 @@ class Api::V1::GridCharactersController < Api::V1::ApiController
|
|||
|
||||
# Otherwise, create a new grid character
|
||||
else
|
||||
@character = GridCharacter.create!(character_params.merge(party_id: party.id, character_id: incoming_character.id))
|
||||
@character = GridCharacter.create!(character_params.merge(party_id: party.id,
|
||||
character_id: incoming_character.id))
|
||||
end
|
||||
|
||||
render :show, status: :created if @character.save!
|
||||
|
|
@ -57,9 +58,9 @@ class Api::V1::GridCharactersController < Api::V1::ApiController
|
|||
|
||||
# Destroy the character at the desired position if it exists
|
||||
existing_character = GridCharacter.where(party: party.id, position: resolve_params[:position]).first
|
||||
GridCharacter.destroy(existing_character.id) unless !existing_character
|
||||
GridCharacter.destroy(existing_character.id) if existing_character
|
||||
|
||||
if (incoming.special)
|
||||
if incoming.special
|
||||
uncap_level = 3
|
||||
uncap_level = 5 if incoming.ulb
|
||||
uncap_level = 4 if incoming.flb
|
||||
|
|
@ -69,34 +70,33 @@ class Api::V1::GridCharactersController < Api::V1::ApiController
|
|||
uncap_level = 5 if incoming.flb
|
||||
end
|
||||
|
||||
@character = GridCharacter.create!(party_id: party.id, character_id: incoming.id, position: resolve_params[:position], uncap_level: uncap_level)
|
||||
@character = GridCharacter.create!(party_id: party.id, character_id: incoming.id,
|
||||
position: resolve_params[:position], uncap_level: uncap_level)
|
||||
render :show, status: :created if @character.save!
|
||||
end
|
||||
|
||||
def update_uncap_level
|
||||
@character = GridCharacter.find(character_params[:id])
|
||||
|
||||
if current_user
|
||||
if @character.party.user != current_user
|
||||
render_unauthorized_response
|
||||
end
|
||||
end
|
||||
render_unauthorized_response if current_user && (@character.party.user != current_user)
|
||||
|
||||
@character.uncap_level = character_params[:uncap_level]
|
||||
render :show, status: :ok if @character.save!
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
def destroy; end
|
||||
|
||||
private
|
||||
|
||||
# Specify whitelisted properties that can be modified.
|
||||
def character_params
|
||||
params.require(:character).permit(:id, :party_id, :character_id, :position, :uncap_level, :conflicting, :incoming)
|
||||
params.require(:character).permit(:id, :party_id, :character_id, :position, :uncap_level, :conflicting,
|
||||
:incoming)
|
||||
end
|
||||
|
||||
def resolve_params
|
||||
params.require(:resolve).permit(:position, :incoming, :conflicting => [])
|
||||
params.require(:resolve).permit(:position, :incoming, conflicting: [])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,18 +1,18 @@
|
|||
class Api::V1::GridSummonsController < Api::V1::ApiController
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class GridSummonsController < Api::V1::ApiController
|
||||
def create
|
||||
party = Party.find(summon_params[:party_id])
|
||||
canonical_summon = Summon.find(summon_params[:summon_id])
|
||||
|
||||
if current_user
|
||||
if party.user != current_user
|
||||
render_unauthorized_response
|
||||
end
|
||||
end
|
||||
render_unauthorized_response if current_user && (party.user != current_user)
|
||||
|
||||
if grid_summon = GridSummon.where(
|
||||
if (grid_summon = GridSummon.where(
|
||||
party_id: party.id,
|
||||
position: summon_params[:position]
|
||||
).first
|
||||
).first)
|
||||
GridSummon.destroy(grid_summon.id)
|
||||
end
|
||||
|
||||
|
|
@ -23,18 +23,13 @@ class Api::V1::GridSummonsController < Api::V1::ApiController
|
|||
def update_uncap_level
|
||||
@summon = GridSummon.find(summon_params[:id])
|
||||
|
||||
if current_user
|
||||
if @summon.party.user != current_user
|
||||
render_unauthorized_response
|
||||
end
|
||||
end
|
||||
render_unauthorized_response if current_user && (@summon.party.user != current_user)
|
||||
|
||||
@summon.uncap_level = summon_params[:uncap_level]
|
||||
render :show, status: :ok if @summon.save!
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
def destroy; end
|
||||
|
||||
private
|
||||
|
||||
|
|
@ -42,4 +37,6 @@ class Api::V1::GridSummonsController < Api::V1::ApiController
|
|||
def summon_params
|
||||
params.require(:summon).permit(:id, :party_id, :summon_id, :position, :main, :friend, :uncap_level)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,15 +1,15 @@
|
|||
class Api::V1::GridWeaponsController < Api::V1::ApiController
|
||||
before_action :set, except: ['create', 'update_uncap_level', 'destroy']
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class GridWeaponsController < Api::V1::ApiController
|
||||
before_action :set, except: %w[create update_uncap_level destroy]
|
||||
|
||||
def create
|
||||
party = Party.find(weapon_params[:party_id])
|
||||
canonical_weapon = Weapon.find(weapon_params[:weapon_id])
|
||||
|
||||
if current_user
|
||||
if party.user != current_user
|
||||
render_unauthorized_response
|
||||
end
|
||||
end
|
||||
render_unauthorized_response if current_user && (party.user != current_user)
|
||||
|
||||
if grid_weapon = GridWeapon.where(
|
||||
party_id: party.id,
|
||||
|
|
@ -20,7 +20,7 @@ class Api::V1::GridWeaponsController < Api::V1::ApiController
|
|||
|
||||
@weapon = GridWeapon.create!(weapon_params.merge(party_id: party.id, weapon_id: canonical_weapon.id))
|
||||
|
||||
if (@weapon.position == -1)
|
||||
if @weapon.position == -1
|
||||
party.element = @weapon.weapon.element
|
||||
party.save!
|
||||
end
|
||||
|
|
@ -29,11 +29,9 @@ class Api::V1::GridWeaponsController < Api::V1::ApiController
|
|||
end
|
||||
|
||||
def update
|
||||
if current_user
|
||||
if @weapon.party.user != current_user
|
||||
render_unauthorized_response
|
||||
end
|
||||
end
|
||||
render_unauthorized_response if current_user && (@weapon.party.user != current_user)
|
||||
|
||||
ap weapon_params
|
||||
|
||||
# TODO: Server-side validation of weapon mods
|
||||
# We don't want someone modifying the JSON and adding
|
||||
|
|
@ -47,11 +45,7 @@ class Api::V1::GridWeaponsController < Api::V1::ApiController
|
|||
def update_uncap_level
|
||||
@weapon = GridWeapon.find(weapon_params[:id])
|
||||
|
||||
if current_user
|
||||
if party.user != current_user
|
||||
render_unauthorized_response
|
||||
end
|
||||
end
|
||||
render_unauthorized_response if current_user && (party.user != current_user)
|
||||
|
||||
@weapon.uncap_level = weapon_params[:uncap_level]
|
||||
render :show, status: :ok if @weapon.save!
|
||||
|
|
@ -60,7 +54,7 @@ class Api::V1::GridWeaponsController < Api::V1::ApiController
|
|||
private
|
||||
|
||||
def set
|
||||
@weapon = GridWeapon.where("id = ?", params[:id]).first
|
||||
@weapon = GridWeapon.where('id = ?', params[:id]).first
|
||||
end
|
||||
|
||||
# Specify whitelisted properties that can be modified.
|
||||
|
|
@ -72,4 +66,6 @@ class Api::V1::GridWeaponsController < Api::V1::ApiController
|
|||
:ax_modifier1, :ax_modifier2, :ax_strength1, :ax_strength2
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,6 +1,10 @@
|
|||
class Api::V1::JobSkillsController < Api::V1::ApiController
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class JobSkillsController < Api::V1::ApiController
|
||||
def all
|
||||
@skills = JobSkill.all()
|
||||
@skills = JobSkill.all
|
||||
render :all, status: :ok
|
||||
end
|
||||
|
||||
|
|
@ -10,4 +14,6 @@ class Api::V1::JobSkillsController < Api::V1::ApiController
|
|||
@skills = JobSkill.where(job: job).or(JobSkill.where(sub: true))
|
||||
render :all, status: :ok
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
class Api::V1::JobsController < Api::V1::ApiController
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class JobsController < Api::V1::ApiController
|
||||
before_action :set, only: %w[update_job update_job_skills]
|
||||
|
||||
def all
|
||||
@jobs = Job.all()
|
||||
@jobs = Job.all
|
||||
render :all, status: :ok
|
||||
end
|
||||
|
||||
|
|
@ -45,7 +49,8 @@ class Api::V1::JobsController < Api::V1::ApiController
|
|||
new_skill_ids = new_skill_keys.map { |key| job_params[key] }
|
||||
new_skill_ids.map do |id|
|
||||
skill = JobSkill.find(id)
|
||||
raise Api::V1::IncompatibleSkillError.new(job: @party.job, skill: skill) if mismatched_skill(@party.job, skill)
|
||||
raise Api::V1::IncompatibleSkillError.new(job: @party.job, skill: skill) if mismatched_skill(@party.job,
|
||||
skill)
|
||||
end
|
||||
|
||||
positions = extract_positions_from_keys(new_skill_keys)
|
||||
|
|
@ -157,4 +162,6 @@ class Api::V1::JobsController < Api::V1::ApiController
|
|||
:skill3_id
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,4 +1,8 @@
|
|||
class Api::V1::PartiesController < Api::V1::ApiController
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class PartiesController < Api::V1::ApiController
|
||||
before_action :set_from_slug,
|
||||
except: %w[create destroy update index favorites]
|
||||
before_action :set, only: %w[update destroy]
|
||||
|
|
@ -38,20 +42,22 @@ class Api::V1::PartiesController < Api::V1::ApiController
|
|||
@per_page = 15
|
||||
|
||||
now = DateTime.current
|
||||
unless request.params['recency'].blank?
|
||||
start_time =
|
||||
(
|
||||
now - request.params["recency"].to_i.seconds
|
||||
).to_datetime.beginning_of_day unless request.params["recency"].blank?
|
||||
now - request.params['recency'].to_i.seconds
|
||||
).to_datetime.beginning_of_day
|
||||
end
|
||||
|
||||
conditions = {}
|
||||
conditions[:element] = request.params["element"] unless request.params[
|
||||
"element"
|
||||
conditions[:element] = request.params['element'] unless request.params[
|
||||
'element'
|
||||
].blank?
|
||||
conditions[:raid] = request.params["raid"] unless request.params[
|
||||
"raid"
|
||||
conditions[:raid] = request.params['raid'] unless request.params[
|
||||
'raid'
|
||||
].blank?
|
||||
conditions[:created_at] = start_time..now unless request.params[
|
||||
"recency"
|
||||
'recency'
|
||||
].blank?
|
||||
conditions[:weapons_count] = 5..13
|
||||
|
||||
|
|
@ -75,20 +81,22 @@ class Api::V1::PartiesController < Api::V1::ApiController
|
|||
@per_page = 15
|
||||
|
||||
now = DateTime.current
|
||||
unless request.params['recency'].blank?
|
||||
start_time =
|
||||
(
|
||||
now - params["recency"].to_i.seconds
|
||||
).to_datetime.beginning_of_day unless request.params["recency"].blank?
|
||||
now - params['recency'].to_i.seconds
|
||||
).to_datetime.beginning_of_day
|
||||
end
|
||||
|
||||
conditions = {}
|
||||
conditions[:element] = request.params["element"] unless request.params[
|
||||
"element"
|
||||
conditions[:element] = request.params['element'] unless request.params[
|
||||
'element'
|
||||
].blank?
|
||||
conditions[:raid] = request.params["raid"] unless request.params[
|
||||
"raid"
|
||||
conditions[:raid] = request.params['raid'] unless request.params[
|
||||
'raid'
|
||||
].blank?
|
||||
conditions[:created_at] = start_time..now unless request.params[
|
||||
"recency"
|
||||
'recency'
|
||||
].blank?
|
||||
conditions[:favorites] = { user_id: current_user.id }
|
||||
|
||||
|
|
@ -96,7 +104,7 @@ class Api::V1::PartiesController < Api::V1::ApiController
|
|||
Party
|
||||
.joins(:favorites)
|
||||
.where(conditions)
|
||||
.order("favorites.created_at DESC")
|
||||
.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
|
||||
|
|
@ -130,19 +138,18 @@ class Api::V1::PartiesController < Api::V1::ApiController
|
|||
private
|
||||
|
||||
def random_string
|
||||
numChars = 6
|
||||
o = [("a".."z"), ("A".."Z"), (0..9)].map(&:to_a).flatten
|
||||
return (0...numChars).map { o[rand(o.length)] }.join
|
||||
num_chars = 6
|
||||
o = [('a'..'z'), ('A'..'Z'), (0..9)].map(&:to_a).flatten
|
||||
(0...num_chars).map { o[rand(o.length)] }.join
|
||||
end
|
||||
|
||||
def set_from_slug
|
||||
@party = Party.where("shortcode = ?", params[:id]).first
|
||||
@party.favorited =
|
||||
current_user && @party ? @party.is_favorited(current_user) : false
|
||||
@party = Party.where('shortcode = ?', params[:id]).first
|
||||
@party.favorited = current_user && @party ? @party.is_favorited(current_user) : false
|
||||
end
|
||||
|
||||
def set
|
||||
@party = Party.where("id = ?", params[:id]).first
|
||||
@party = Party.where('id = ?', params[:id]).first
|
||||
end
|
||||
|
||||
def party_params
|
||||
|
|
@ -159,4 +166,6 @@ class Api::V1::PartiesController < Api::V1::ApiController
|
|||
:skill3_id
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,6 +1,12 @@
|
|||
class Api::V1::RaidsController < Api::V1::ApiController
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class RaidsController < Api::V1::ApiController
|
||||
def all
|
||||
@raids = Raid.all()
|
||||
@raids = Raid.all
|
||||
render :all, status: :ok
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,4 +1,8 @@
|
|||
class Api::V1::SearchController < Api::V1::ApiController
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class SearchController < Api::V1::ApiController
|
||||
def characters
|
||||
filters = search_params[:filters]
|
||||
locale = search_params[:locale] || 'en'
|
||||
|
|
@ -7,8 +11,14 @@ class Api::V1::SearchController < Api::V1::ApiController
|
|||
if filters
|
||||
conditions[:rarity] = filters['rarity'] unless filters['rarity'].blank? || filters['rarity'].empty?
|
||||
conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty?
|
||||
conditions[:proficiency1] = filters['proficiency1'] unless filters['proficiency1'].blank? || filters['proficiency1'].empty?
|
||||
conditions[:proficiency2] = filters['proficiency2'] unless filters['proficiency2'].blank? || filters['proficiency2'].empty?
|
||||
unless filters['proficiency1'].blank? || filters['proficiency1'].empty?
|
||||
conditions[:proficiency1] =
|
||||
filters['proficiency1']
|
||||
end
|
||||
unless filters['proficiency2'].blank? || filters['proficiency2'].empty?
|
||||
conditions[:proficiency2] =
|
||||
filters['proficiency2']
|
||||
end
|
||||
# conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty?
|
||||
end
|
||||
|
||||
|
|
@ -34,7 +44,10 @@ class Api::V1::SearchController < Api::V1::ApiController
|
|||
if filters
|
||||
conditions[:rarity] = filters['rarity'] unless filters['rarity'].blank? || filters['rarity'].empty?
|
||||
conditions[:element] = filters['element'] unless filters['element'].blank? || filters['element'].empty?
|
||||
conditions[:proficiency] = filters['proficiency1'] unless filters['proficiency1'].blank? || filters['proficiency1'].empty?
|
||||
unless filters['proficiency1'].blank? || filters['proficiency1'].empty?
|
||||
conditions[:proficiency] =
|
||||
filters['proficiency1']
|
||||
end
|
||||
conditions[:series] = filters['series'] unless filters['series'].blank? || filters['series'].empty?
|
||||
end
|
||||
|
||||
|
|
@ -136,4 +149,6 @@ class Api::V1::SearchController < Api::V1::ApiController
|
|||
def search_params
|
||||
params.require(:search).permit!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,8 +1,12 @@
|
|||
class Api::V1::UsersController < Api::V1::ApiController
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class UsersController < Api::V1::ApiController
|
||||
class ForbiddenError < StandardError; end
|
||||
|
||||
before_action :set, except: ['create', 'check_email', 'check_username']
|
||||
before_action :set_by_id, only: ['info', 'update']
|
||||
before_action :set, except: %w[create check_email check_username]
|
||||
before_action :set_by_id, only: %w[info update]
|
||||
|
||||
def create
|
||||
@user = User.new(user_params)
|
||||
|
|
@ -14,7 +18,8 @@ class Api::V1::UsersController < Api::V1::ApiController
|
|||
scopes: 'public'
|
||||
).token
|
||||
|
||||
if @user.save!
|
||||
return unless @user.save!
|
||||
|
||||
@presenter = {
|
||||
user_id: @user.id,
|
||||
username: @user.username,
|
||||
|
|
@ -23,8 +28,6 @@ class Api::V1::UsersController < Api::V1::ApiController
|
|||
|
||||
render :create, status: :created
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def update
|
||||
render :info, status: :ok if @user.update(user_params)
|
||||
|
|
@ -39,7 +42,9 @@ class Api::V1::UsersController < Api::V1::ApiController
|
|||
@per_page = 15
|
||||
|
||||
now = DateTime.current
|
||||
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank?
|
||||
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?
|
||||
|
|
@ -51,9 +56,9 @@ class Api::V1::UsersController < Api::V1::ApiController
|
|||
.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
|
||||
}
|
||||
.each do |party|
|
||||
party.favorited = current_user ? party.is_favorited(current_user) : false
|
||||
end
|
||||
@count = Party.where(conditions).count
|
||||
else
|
||||
render_not_found_response
|
||||
|
|
@ -61,37 +66,36 @@ class Api::V1::UsersController < Api::V1::ApiController
|
|||
end
|
||||
|
||||
def check_email
|
||||
if params[:email].present?
|
||||
@available = User.where("email = ?", params[:email]).count == 0
|
||||
@available = if params[:email].present?
|
||||
User.where('email = ?', params[:email]).count.zero?
|
||||
else
|
||||
@available = false
|
||||
false
|
||||
end
|
||||
|
||||
render :available
|
||||
end
|
||||
|
||||
def check_username
|
||||
if params[:username].present?
|
||||
@available = User.where("username = ?", params[:username]).count == 0
|
||||
@available = if params[:username].present?
|
||||
User.where('username = ?', params[:username]).count.zero?
|
||||
else
|
||||
@available = false
|
||||
false
|
||||
end
|
||||
|
||||
render :available
|
||||
end
|
||||
|
||||
def destroy
|
||||
end
|
||||
def destroy; end
|
||||
|
||||
private
|
||||
|
||||
# Specify whitelisted properties that can be modified.
|
||||
def set
|
||||
@user = User.where("username = ?", params[:id]).first
|
||||
@user = User.where('username = ?', params[:id]).first
|
||||
end
|
||||
|
||||
def set_by_id
|
||||
@user = User.where("id = ?", params[:id]).first
|
||||
@user = User.where('id = ?', params[:id]).first
|
||||
end
|
||||
|
||||
def user_params
|
||||
|
|
@ -100,4 +104,6 @@ class Api::V1::UsersController < Api::V1::ApiController
|
|||
:granblue_id, :picture, :element, :language, :gender, :private
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,4 +1,8 @@
|
|||
class Api::V1::WeaponKeysController < Api::V1::ApiController
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class WeaponKeysController < Api::V1::ApiController
|
||||
def all
|
||||
conditions = {}
|
||||
conditions[:series] = request.params['series']
|
||||
|
|
@ -8,4 +12,6 @@ class Api::V1::WeaponKeysController < Api::V1::ApiController
|
|||
@keys = WeaponKey.where(conditions)
|
||||
render :all, status: :ok
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class ApplicationController < ActionController::API
|
||||
# Not usually required for Rails 5 in API mode, but
|
||||
# necessary here because we're using RABL.
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class TokensController < Doorkeeper::TokensController
|
||||
# Overriding create action
|
||||
# POST /oauth/token
|
||||
|
|
@ -7,7 +9,11 @@ class TokensController < Doorkeeper::TokensController
|
|||
|
||||
if response.status == :ok
|
||||
# User the resource_owner_id from token to identify the user
|
||||
user = User.find(response.token.resource_owner_id) rescue nil
|
||||
user = begin
|
||||
User.find(response.token.resource_owner_id)
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
|
||||
unless user.nil?
|
||||
### If you want to render user with template
|
||||
|
|
@ -25,12 +31,10 @@ class TokensController < Doorkeeper::TokensController
|
|||
end
|
||||
end
|
||||
|
||||
self.headers.merge! response.headers
|
||||
headers.merge! response.headers
|
||||
self.response_body = body.to_json
|
||||
self.status = response.status
|
||||
|
||||
rescue Doorkeeper::Errors::DoorkeeperError => e
|
||||
handle_token_exception e
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
module Api::V1
|
||||
class FavoriteAlreadyExistsError < GranblueError
|
||||
def http_status
|
||||
422
|
||||
end
|
||||
|
||||
def code
|
||||
"favorite_already_exists"
|
||||
end
|
||||
|
||||
def message
|
||||
"This user has favorited this party already"
|
||||
end
|
||||
|
||||
def to_hash
|
||||
{
|
||||
message: message,
|
||||
code: code
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
module Api::V1
|
||||
class IncompatibleSkillError < GranblueError
|
||||
def initialize(data)
|
||||
@data = data
|
||||
end
|
||||
|
||||
def http_status
|
||||
422
|
||||
end
|
||||
|
||||
def code
|
||||
'incompatible_skill'
|
||||
end
|
||||
|
||||
def message
|
||||
'The selected skill cannot be added to the current job'
|
||||
end
|
||||
|
||||
def to_hash
|
||||
ap @data
|
||||
{
|
||||
message: message,
|
||||
code: code,
|
||||
job: @data[:job],
|
||||
skill: @data[:skill]
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
module Api::V1
|
||||
class NoJobProvidedError < GranblueError
|
||||
def http_status
|
||||
422
|
||||
end
|
||||
|
||||
def code
|
||||
"no_job_provided"
|
||||
end
|
||||
|
||||
def message
|
||||
"A job ID must be provided"
|
||||
end
|
||||
|
||||
def to_hash
|
||||
{
|
||||
message: message,
|
||||
code: code
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
module Api::V1
|
||||
class NoJobSkillProvidedError < GranblueError
|
||||
def http_status
|
||||
422
|
||||
end
|
||||
|
||||
def code
|
||||
"no_job_skill_provided"
|
||||
end
|
||||
|
||||
def message
|
||||
"A job skill ID must be provided"
|
||||
end
|
||||
|
||||
def to_hash
|
||||
{
|
||||
message: message,
|
||||
code: code
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,22 +0,0 @@
|
|||
module Api::V1
|
||||
class UnauthorizedError < StandardError
|
||||
def http_status
|
||||
401
|
||||
end
|
||||
|
||||
def code
|
||||
"unauthorized"
|
||||
end
|
||||
|
||||
def message
|
||||
"User is not allowed to modify that resource"
|
||||
end
|
||||
|
||||
def to_hash
|
||||
{
|
||||
message: message,
|
||||
code: code
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
26
app/errors/api/v1/favorite_already_exists_error.rb
Normal file
26
app/errors/api/v1/favorite_already_exists_error.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class FavoriteAlreadyExistsError < GranblueError
|
||||
def http_status
|
||||
422
|
||||
end
|
||||
|
||||
def code
|
||||
'favorite_already_exists'
|
||||
end
|
||||
|
||||
def message
|
||||
'This user has favorited this party already'
|
||||
end
|
||||
|
||||
def to_hash
|
||||
{
|
||||
message: message,
|
||||
code: code
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
33
app/errors/api/v1/incompatible_skill_error.rb
Normal file
33
app/errors/api/v1/incompatible_skill_error.rb
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class IncompatibleSkillError < GranblueError
|
||||
def initialize(data)
|
||||
@data = data
|
||||
end
|
||||
|
||||
def http_status
|
||||
422
|
||||
end
|
||||
|
||||
def code
|
||||
'incompatible_skill'
|
||||
end
|
||||
|
||||
def message
|
||||
'The selected skill cannot be added to the current job'
|
||||
end
|
||||
|
||||
def to_hash
|
||||
ap @data
|
||||
{
|
||||
message: message,
|
||||
code: code,
|
||||
job: @data[:job],
|
||||
skill: @data[:skill]
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
26
app/errors/api/v1/no_job_provided_error.rb
Normal file
26
app/errors/api/v1/no_job_provided_error.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class NoJobProvidedError < GranblueError
|
||||
def http_status
|
||||
422
|
||||
end
|
||||
|
||||
def code
|
||||
'no_job_provided'
|
||||
end
|
||||
|
||||
def message
|
||||
'A job ID must be provided'
|
||||
end
|
||||
|
||||
def to_hash
|
||||
{
|
||||
message: message,
|
||||
code: code
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
26
app/errors/api/v1/no_job_skill_provided_error.rb
Normal file
26
app/errors/api/v1/no_job_skill_provided_error.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class NoJobSkillProvidedError < GranblueError
|
||||
def http_status
|
||||
422
|
||||
end
|
||||
|
||||
def code
|
||||
'no_job_skill_provided'
|
||||
end
|
||||
|
||||
def message
|
||||
'A job skill ID must be provided'
|
||||
end
|
||||
|
||||
def to_hash
|
||||
{
|
||||
message: message,
|
||||
code: code
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class SameFavoriteUserError < GranblueError
|
||||
26
app/errors/api/v1/unauthorized_error.rb
Normal file
26
app/errors/api/v1/unauthorized_error.rb
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Api
|
||||
module V1
|
||||
class UnauthorizedError < StandardError
|
||||
def http_status
|
||||
401
|
||||
end
|
||||
|
||||
def code
|
||||
'unauthorized'
|
||||
end
|
||||
|
||||
def message
|
||||
'User is not allowed to modify that resource'
|
||||
end
|
||||
|
||||
def to_hash
|
||||
{
|
||||
message: message,
|
||||
code: code
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class ValidationErrorSerializer
|
||||
def initialize(record, field, details)
|
||||
@record = record
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class ValidationErrorsSerializer
|
||||
attr_reader :record
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class ApplicationRecord < ActiveRecord::Base
|
||||
self.abstract_class = true
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Character < ApplicationRecord
|
||||
include PgSearch::Model
|
||||
|
||||
|
|
@ -14,7 +16,7 @@ class Character < ApplicationRecord
|
|||
using: {
|
||||
tsearch: {
|
||||
prefix: true,
|
||||
dictionary: "simple"
|
||||
dictionary: 'simple'
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -90,5 +92,4 @@ class Character < ApplicationRecord
|
|||
# Female: 2,
|
||||
# "Male/Female": 3
|
||||
# }
|
||||
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Favorite < ApplicationRecord
|
||||
belongs_to :user
|
||||
belongs_to :party
|
||||
|
||||
def party
|
||||
Party.find(self.party_id)
|
||||
Party.find(party_id)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class GridCharacter < ApplicationRecord
|
||||
belongs_to :party
|
||||
|
||||
def character
|
||||
Character.find(self.character_id)
|
||||
Character.find(character_id)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class GridSummon < ApplicationRecord
|
||||
belongs_to :party
|
||||
|
||||
def summon
|
||||
Summon.find(self.summon_id)
|
||||
Summon.find(summon_id)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class GridWeapon < ApplicationRecord
|
||||
belongs_to :party,
|
||||
counter_cache: :weapons_count
|
||||
|
|
@ -7,14 +9,14 @@ class GridWeapon < ApplicationRecord
|
|||
belongs_to :weapon_key3, class_name: 'WeaponKey', foreign_key: :weapon_key3_id, optional: true
|
||||
|
||||
def weapon
|
||||
Weapon.find(self.weapon_id)
|
||||
Weapon.find(weapon_id)
|
||||
end
|
||||
|
||||
def weapon_keys
|
||||
weapon_keys = []
|
||||
weapon_keys.push(self.weapon_key1) if self.weapon_key1 != nil
|
||||
weapon_keys.push(self.weapon_key2) if self.weapon_key2 != nil
|
||||
weapon_keys.push(self.weapon_key3) if self.weapon_key3 != nil
|
||||
weapon_keys.push(weapon_key1) unless weapon_key1.nil?
|
||||
weapon_keys.push(weapon_key2) unless weapon_key2.nil?
|
||||
weapon_keys.push(weapon_key3) unless weapon_key3.nil?
|
||||
|
||||
weapon_keys
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Job < ApplicationRecord
|
||||
belongs_to :party
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class JobSkill < ApplicationRecord
|
||||
alias eql? ==
|
||||
|
||||
|
|
@ -10,8 +12,8 @@ class JobSkill < ApplicationRecord
|
|||
using: {
|
||||
tsearch: {
|
||||
prefix: true,
|
||||
dictionary: "simple",
|
||||
},
|
||||
dictionary: 'simple'
|
||||
}
|
||||
}
|
||||
|
||||
pg_search_scope :jp_search,
|
||||
|
|
@ -19,15 +21,15 @@ class JobSkill < ApplicationRecord
|
|||
using: {
|
||||
tsearch: {
|
||||
prefix: true,
|
||||
dictionary: "simple",
|
||||
},
|
||||
dictionary: 'simple'
|
||||
}
|
||||
}
|
||||
|
||||
def display_resource(skill)
|
||||
skill.name_en
|
||||
end
|
||||
|
||||
def ==(o)
|
||||
self.class == o.class && id == o.id
|
||||
def ==(other)
|
||||
self.class == other.class && id == other.id
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Party < ApplicationRecord
|
||||
##### ActiveRecord Associations
|
||||
belongs_to :user, optional: true
|
||||
|
|
@ -5,38 +7,38 @@ class Party < ApplicationRecord
|
|||
belongs_to :job, optional: true
|
||||
|
||||
belongs_to :skill0,
|
||||
foreign_key: "skill0_id",
|
||||
class_name: "JobSkill",
|
||||
foreign_key: 'skill0_id',
|
||||
class_name: 'JobSkill',
|
||||
optional: true
|
||||
|
||||
belongs_to :skill1,
|
||||
foreign_key: "skill1_id",
|
||||
class_name: "JobSkill",
|
||||
foreign_key: 'skill1_id',
|
||||
class_name: 'JobSkill',
|
||||
optional: true
|
||||
|
||||
belongs_to :skill2,
|
||||
foreign_key: "skill2_id",
|
||||
class_name: "JobSkill",
|
||||
foreign_key: 'skill2_id',
|
||||
class_name: 'JobSkill',
|
||||
optional: true
|
||||
|
||||
belongs_to :skill3,
|
||||
foreign_key: "skill3_id",
|
||||
class_name: "JobSkill",
|
||||
foreign_key: 'skill3_id',
|
||||
class_name: 'JobSkill',
|
||||
optional: true
|
||||
|
||||
has_many :characters,
|
||||
foreign_key: "party_id",
|
||||
class_name: "GridCharacter",
|
||||
foreign_key: 'party_id',
|
||||
class_name: 'GridCharacter',
|
||||
dependent: :destroy
|
||||
|
||||
has_many :weapons,
|
||||
foreign_key: "party_id",
|
||||
class_name: "GridWeapon",
|
||||
foreign_key: 'party_id',
|
||||
class_name: 'GridWeapon',
|
||||
dependent: :destroy
|
||||
|
||||
has_many :summons,
|
||||
foreign_key: "party_id",
|
||||
class_name: "GridSummon",
|
||||
foreign_key: 'party_id',
|
||||
class_name: 'GridSummon',
|
||||
dependent: :destroy
|
||||
|
||||
has_many :favorites
|
||||
|
|
@ -55,18 +57,14 @@ class Party < ApplicationRecord
|
|||
def skills_are_unique
|
||||
skills = [skill0, skill1, skill2, skill3].compact
|
||||
|
||||
if skills.uniq.length != skills.length
|
||||
errors.add(:skill1, "must be unique") if skill0 == skill1
|
||||
return unless skills.uniq.length != skills.length
|
||||
|
||||
if skill0 == skill2 || skill1 == skill2
|
||||
errors.add(:skill2, "must be unique")
|
||||
end
|
||||
errors.add(:skill1, 'must be unique') if skill0 == skill1
|
||||
|
||||
if skill0 == skill3 || skill1 == skill3 || skill2 == skill3
|
||||
errors.add(:skill3, "must be unique")
|
||||
end
|
||||
errors.add(:skill2, 'must be unique') if skill0 == skill2 || skill1 == skill2
|
||||
|
||||
errors.add(:job_skills, "must be unique")
|
||||
end
|
||||
errors.add(:skill3, 'must be unique') if skill0 == skill3 || skill1 == skill3 || skill2 == skill3
|
||||
|
||||
errors.add(:job_skills, 'must be unique')
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Raid < ApplicationRecord
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Summon < ApplicationRecord
|
||||
include PgSearch::Model
|
||||
|
||||
|
|
@ -14,7 +16,7 @@ class Summon < ApplicationRecord
|
|||
using: {
|
||||
tsearch: {
|
||||
prefix: true,
|
||||
dictionary: "simple"
|
||||
dictionary: 'simple'
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class User < ApplicationRecord
|
||||
before_save { self.email = email.downcase }
|
||||
|
||||
|
|
@ -34,10 +36,10 @@ class User < ApplicationRecord
|
|||
on: :update,
|
||||
if: :password_digest_changed?
|
||||
|
||||
##### ActiveModel Security
|
||||
##### ActiveModel Security
|
||||
has_secure_password
|
||||
|
||||
def favorite_parties
|
||||
self.favorites.map { |favorite| favorite.party }
|
||||
favorites.map(&:party)
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,3 +1,5 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class Weapon < ApplicationRecord
|
||||
include PgSearch::Model
|
||||
|
||||
|
|
@ -14,7 +16,7 @@ class Weapon < ApplicationRecord
|
|||
using: {
|
||||
tsearch: {
|
||||
prefix: true,
|
||||
dictionary: "simple"
|
||||
dictionary: 'simple'
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,2 +1,4 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class WeaponKey < ApplicationRecord
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,4 +1,9 @@
|
|||
Rails.application.configure do
|
||||
config.after_initialize do
|
||||
ActiveRecord::Base.logger = Rails.logger.clone
|
||||
ActiveRecord::Base.logger.level = Logger::INFO
|
||||
end
|
||||
|
||||
# Settings specified here will take precedence over those in config/application.rb.
|
||||
config.hosts << "grid-api.ngrok.io"
|
||||
|
||||
|
|
|
|||
7
spec/requests/job_skills_spec.rb
Normal file
7
spec/requests/job_skills_spec.rb
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
require 'rails_helper'
|
||||
|
||||
RSpec.describe "JobSkills", type: :request do
|
||||
describe "GET /index" do
|
||||
pending "add some examples (or delete) #{__FILE__}"
|
||||
end
|
||||
end
|
||||
Loading…
Reference in a new issue