Implement type parameter for tournaments index

This commit is contained in:
Thor77 2019-04-23 13:09:29 +02:00 committed by Malaber
parent 6d40d091a4
commit 2b72d0457e
1 changed files with 14 additions and 1 deletions

View File

@ -9,7 +9,16 @@ class TournamentsController < ApplicationController
# GET /tournaments
def index
tournaments = Tournament.where(public: true).or(Tournament.where(owner: current_user)).order(:created_at)
type = index_params.fetch(:type, 'public')
if type == 'public'
tournaments = Tournament.where(public: true).order(:created_at)
elsif type == 'private'
tournaments = Tournament.where(owner: current_user, public: false).order(:created_at)
else
# invalid type specified
render json: { error: 'invalid type' }, status: :bad_request
return
end
render json: tournaments, each_serializer: SimpleTournamentSerializer
end
@ -69,6 +78,10 @@ class TournamentsController < ApplicationController
@tournament = Tournament.find(params[:id])
end
def index_params
params.permit(:type)
end
def tournament_params
params.slice(:name, :description, :public, :teams).permit!
end