diff --git a/app/controllers/api/application_controller.rb b/app/controllers/api/application_controller.rb deleted file mode 100644 index e25ef0c..0000000 --- a/app/controllers/api/application_controller.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/app/controllers/api/v1/api_controller.rb b/app/controllers/api/v1/api_controller.rb new file mode 100644 index 0000000..81e5ee5 --- /dev/null +++ b/app/controllers/api/v1/api_controller.rb @@ -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 \ No newline at end of file diff --git a/app/controllers/api/v1/grid_weapons_controller.rb b/app/controllers/api/v1/grid_weapons_controller.rb index a5944a1..f92268c 100644 --- a/app/controllers/api/v1/grid_weapons_controller.rb +++ b/app/controllers/api/v1/grid_weapons_controller.rb @@ -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]) diff --git a/app/controllers/api/v1/parties_controller.rb b/app/controllers/api/v1/parties_controller.rb index 555bd5f..442f1b6 100644 --- a/app/controllers/api/v1/parties_controller.rb +++ b/app/controllers/api/v1/parties_controller.rb @@ -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 \ No newline at end of file diff --git a/app/controllers/api/v1/search_controller.rb b/app/controllers/api/v1/search_controller.rb index 20086cc..e41f609 100644 --- a/app/controllers/api/v1/search_controller.rb +++ b/app/controllers/api/v1/search_controller.rb @@ -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? diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 5825d9a..ea17b56 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -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!