mirror of
https://github.com/ryankazokas/turbovault-app.git
synced 2026-04-17 04:02:53 +00:00
Moving to github
This commit is contained in:
38
app/controllers/api/v1/base_controller.rb
Normal file
38
app/controllers/api/v1/base_controller.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
module Api
|
||||
module V1
|
||||
class BaseController < ApplicationController
|
||||
skip_before_action :verify_authenticity_token
|
||||
before_action :authenticate_api_token
|
||||
|
||||
rescue_from ActiveRecord::RecordNotFound, with: :not_found
|
||||
rescue_from ActiveRecord::RecordInvalid, with: :unprocessable_entity
|
||||
|
||||
private
|
||||
|
||||
def authenticate_api_token
|
||||
token = request.headers["Authorization"]&.split(" ")&.last
|
||||
@api_token = ApiToken.active.find_by(token: token)
|
||||
|
||||
if @api_token
|
||||
@api_token.touch_last_used!
|
||||
@current_user = @api_token.user
|
||||
set_rls_user_id(@current_user.id)
|
||||
else
|
||||
render json: { error: "Invalid or missing API token" }, status: :unauthorized
|
||||
end
|
||||
end
|
||||
|
||||
def current_user
|
||||
@current_user
|
||||
end
|
||||
|
||||
def not_found(exception)
|
||||
render json: { error: exception.message }, status: :not_found
|
||||
end
|
||||
|
||||
def unprocessable_entity(exception)
|
||||
render json: { errors: exception.record.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
49
app/controllers/api/v1/collections_controller.rb
Normal file
49
app/controllers/api/v1/collections_controller.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
module Api
|
||||
module V1
|
||||
class CollectionsController < BaseController
|
||||
before_action :set_collection, only: [ :show, :update, :destroy ]
|
||||
|
||||
def index
|
||||
@collections = current_user.collections.includes(:games).order(:name)
|
||||
render json: @collections, include: :games
|
||||
end
|
||||
|
||||
def show
|
||||
render json: @collection, include: :games
|
||||
end
|
||||
|
||||
def create
|
||||
@collection = current_user.collections.build(collection_params)
|
||||
|
||||
if @collection.save
|
||||
render json: @collection, status: :created
|
||||
else
|
||||
render json: { errors: @collection.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
if @collection.update(collection_params)
|
||||
render json: @collection
|
||||
else
|
||||
render json: { errors: @collection.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@collection.destroy
|
||||
head :no_content
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_collection
|
||||
@collection = current_user.collections.find(params[:id])
|
||||
end
|
||||
|
||||
def collection_params
|
||||
params.require(:collection).permit(:name, :description, :parent_collection_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
95
app/controllers/api/v1/games_controller.rb
Normal file
95
app/controllers/api/v1/games_controller.rb
Normal file
@@ -0,0 +1,95 @@
|
||||
module Api
|
||||
module V1
|
||||
class GamesController < BaseController
|
||||
before_action :set_game, only: [ :show, :update, :destroy ]
|
||||
|
||||
def index
|
||||
@games = current_user.games.includes(:platform, :genres, :collections)
|
||||
|
||||
# Filtering
|
||||
@games = @games.by_platform(params[:platform_id]) if params[:platform_id].present?
|
||||
@games = @games.by_genre(params[:genre_id]) if params[:genre_id].present?
|
||||
@games = @games.where(format: params[:format]) if params[:format].present?
|
||||
@games = @games.where(completion_status: params[:completion_status]) if params[:completion_status].present?
|
||||
@games = @games.search(params[:search]) if params[:search].present?
|
||||
|
||||
# Sorting
|
||||
@games = case params[:sort]
|
||||
when "alphabetical" then @games.alphabetical
|
||||
when "recent" then @games.recent
|
||||
when "rated" then @games.rated
|
||||
else @games.recent
|
||||
end
|
||||
|
||||
# Pagination
|
||||
page = params[:page] || 1
|
||||
per_page = params[:per_page] || 25
|
||||
|
||||
@games = @games.page(page).per(per_page)
|
||||
|
||||
render json: @games, include: [ :platform, :genres, :collections ]
|
||||
end
|
||||
|
||||
def show
|
||||
render json: @game, include: [ :platform, :genres, :collections ]
|
||||
end
|
||||
|
||||
def create
|
||||
@game = current_user.games.build(game_params)
|
||||
|
||||
if @game.save
|
||||
render json: @game, status: :created, include: [ :platform, :genres ]
|
||||
else
|
||||
render json: { errors: @game.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def update
|
||||
if @game.update(game_params)
|
||||
render json: @game, include: [ :platform, :genres, :collections ]
|
||||
else
|
||||
render json: { errors: @game.errors.full_messages }, status: :unprocessable_entity
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@game.destroy
|
||||
head :no_content
|
||||
end
|
||||
|
||||
def bulk
|
||||
results = { created: [], failed: [] }
|
||||
games_data = params[:games] || []
|
||||
|
||||
games_data.each do |game_data|
|
||||
game = current_user.games.build(game_data.permit!)
|
||||
if game.save
|
||||
results[:created] << game
|
||||
else
|
||||
results[:failed] << { data: game_data, errors: game.errors.full_messages }
|
||||
end
|
||||
end
|
||||
|
||||
render json: {
|
||||
created: results[:created].count,
|
||||
failed: results[:failed].count,
|
||||
details: results
|
||||
}, status: :created
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_game
|
||||
@game = current_user.games.find(params[:id])
|
||||
end
|
||||
|
||||
def game_params
|
||||
params.require(:game).permit(
|
||||
:title, :platform_id, :format, :date_added, :completion_status,
|
||||
:user_rating, :notes, :condition, :price_paid, :location,
|
||||
:digital_store, :custom_entry, :igdb_id, genre_ids: []
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
17
app/controllers/api/v1/genres_controller.rb
Normal file
17
app/controllers/api/v1/genres_controller.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
module Api
|
||||
module V1
|
||||
class GenresController < BaseController
|
||||
skip_before_action :authenticate_api_token, only: [ :index, :show ]
|
||||
|
||||
def index
|
||||
@genres = Genre.order(:name)
|
||||
render json: @genres
|
||||
end
|
||||
|
||||
def show
|
||||
@genre = Genre.find(params[:id])
|
||||
render json: @genre
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
17
app/controllers/api/v1/platforms_controller.rb
Normal file
17
app/controllers/api/v1/platforms_controller.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
module Api
|
||||
module V1
|
||||
class PlatformsController < BaseController
|
||||
skip_before_action :authenticate_api_token, only: [ :index, :show ]
|
||||
|
||||
def index
|
||||
@platforms = Platform.order(:name)
|
||||
render json: @platforms
|
||||
end
|
||||
|
||||
def show
|
||||
@platform = Platform.find(params[:id])
|
||||
render json: @platform
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user