Controllers should inherit from ApiController

This commit is contained in:
Justin Edmund 2020-09-19 12:46:24 -07:00
parent c6886a62e7
commit 071f6b4664
6 changed files with 39 additions and 41 deletions

View file

@ -1,36 +0,0 @@
module Api
class ApplicationController < ActionController::API
include Doorkeeper::Rails::Helpers
rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response
rescue_from ActiveRecord::RecordNotDestroyed, with: :render_unprocessable_entity_response
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response
rescue_from ActiveRecord::RecordNotSaved, with: :render_unprocessable_entity_response
rescue_from ActiveRecord::RecordNotUnique, with: :render_unprocessable_entity_response
rescue_from ActionController::ParameterMissing, with: :render_unprocessable_entity_response
before_action :current_user
# Assign the current user if the Doorkeeper token isn't nil
def current_user
@current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
return @current_user
end
### Error response methods
def render_unprocessable_entity_response(exception)
@exception = exception
render action: 'errors', status: :unprocessable_entity
end
def render_not_found_response(exception)
response = { errors: [{ message: "Record could not be found.", code: "not_found" }]}
render 'not_found', status: :not_found
end
def render_unauthorized_response(exception)
render action: 'errors', status: :unauthorized
end
end
end

View file

@ -0,0 +1,34 @@
class Api::V1::ApiController < ActionController::API
include Doorkeeper::Rails::Helpers
rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response
rescue_from ActiveRecord::RecordNotDestroyed, with: :render_unprocessable_entity_response
rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response
rescue_from ActiveRecord::RecordNotSaved, with: :render_unprocessable_entity_response
rescue_from ActiveRecord::RecordNotUnique, with: :render_unprocessable_entity_response
rescue_from ActionController::ParameterMissing, with: :render_unprocessable_entity_response
before_action :current_user
# Assign the current user if the Doorkeeper token isn't nil
def current_user
@current_user ||= User.find(doorkeeper_token.resource_owner_id) if doorkeeper_token
return @current_user
end
### Error response methods
def render_unprocessable_entity_response(exception)
@exception = exception
render action: 'errors', status: :unprocessable_entity
end
def render_not_found_response(exception)
response = { errors: [{ message: "Record could not be found.", code: "not_found" }]}
render 'not_found', status: :not_found
end
def render_unauthorized_response(exception)
render action: 'errors', status: :unauthorized
end
end

View file

@ -1,4 +1,4 @@
class Api::V1::GridWeaponsController < ActionController::API
class Api::V1::GridWeaponsController < Api::V1::ApiController
def create
party = Party.find(weapon_params[:party_id])
canonical_weapon = Weapon.find(weapon_params[:weapon_id])

View file

@ -1,4 +1,4 @@
class Api::V1::PartiesController < ActionController::API
class Api::V1::PartiesController < Api::V1::ApiController
before_action :set, except: ['create']
def index
@ -28,6 +28,6 @@ class Api::V1::PartiesController < ActionController::API
end
def set
@party = Party.where(shortcode: params[:id]).first
@party = Party.find("abc")
end
end

View file

@ -1,4 +1,4 @@
class Api::V1::SearchController < ApplicationController
class Api::V1::SearchController < Api::V1::ApiController
def index
logger.debug params
if params[:query].present?

View file

@ -1,4 +1,4 @@
class Api::V1::UsersController < ActionController::API
class Api::V1::UsersController < Api::V1::ApiController
def create
@user = User.new(user_params)
render :create, status: :created if @user.save!