Merge pull request #72 from turniere/ticket/TUR-2
Implement Matches endpoint for tournament with filter
This commit is contained in:
commit
5da941c002
|
|
@ -4,6 +4,19 @@ class MatchesController < ApplicationController
|
||||||
before_action :set_match, only: %i[show update]
|
before_action :set_match, only: %i[show update]
|
||||||
before_action :validate_params, only: %i[update]
|
before_action :validate_params, only: %i[update]
|
||||||
before_action -> { require_owner! @match.owner }, only: %i[update]
|
before_action -> { require_owner! @match.owner }, only: %i[update]
|
||||||
|
before_action :set_tournament, only: %i[index]
|
||||||
|
|
||||||
|
# GET/tournaments/1/matches
|
||||||
|
def index
|
||||||
|
matches = if match_params['state'].nil?
|
||||||
|
@tournament.matches
|
||||||
|
else
|
||||||
|
@tournament.matches.select do |m|
|
||||||
|
m.state == match_params['state']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
render json: matches, each_serializer: MatchSerializer
|
||||||
|
end
|
||||||
|
|
||||||
# GET /matches/1
|
# GET /matches/1
|
||||||
def show
|
def show
|
||||||
|
|
@ -61,6 +74,10 @@ class MatchesController < ApplicationController
|
||||||
@match = Match.find(params[:id])
|
@match = Match.find(params[:id])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_tournament
|
||||||
|
@tournament = Tournament.find(params[:tournament_id])
|
||||||
|
end
|
||||||
|
|
||||||
def match_params
|
def match_params
|
||||||
params.slice(:state).permit!
|
params.slice(:state).permit!
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,10 @@ class Tournament < ApplicationRecord
|
||||||
|
|
||||||
after_initialize :generate_code
|
after_initialize :generate_code
|
||||||
|
|
||||||
|
def matches
|
||||||
|
[stages.map(&:matches), stages.map { |s| s.groups.map(&:matches) }].flatten
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def generate_code
|
def generate_code
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ Rails.application.routes.draw do
|
||||||
resources :teams, only: %i[show update]
|
resources :teams, only: %i[show update]
|
||||||
resources :tournaments do
|
resources :tournaments do
|
||||||
resources :statistics, only: %i[index]
|
resources :statistics, only: %i[index]
|
||||||
|
resources :matches, only: %i[index]
|
||||||
end
|
end
|
||||||
resources :match_scores, only: %i[show update]
|
resources :match_scores, only: %i[show update]
|
||||||
resources :groups, only: %i[show]
|
resources :groups, only: %i[show]
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,13 @@
|
||||||
|
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
|
def test_get_index_with_filter(filter_state)
|
||||||
|
get :index, params: { state: filter_state, tournament_id: @tournament.to_param }
|
||||||
|
deserialize_response(response).each do |match|
|
||||||
|
expect(match[:state]).to eq(filter_state)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
RSpec.describe MatchesController, type: :controller do
|
RSpec.describe MatchesController, type: :controller do
|
||||||
before do
|
before do
|
||||||
@match = create(:match, state: :not_started)
|
@match = create(:match, state: :not_started)
|
||||||
|
|
@ -12,6 +19,32 @@ RSpec.describe MatchesController, type: :controller do
|
||||||
@match.match_scores = create_pair(:match_score)
|
@match.match_scores = create_pair(:match_score)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'GET #index' do
|
||||||
|
context 'on a running group stage' do
|
||||||
|
before do
|
||||||
|
@tournament = create(:group_stage_tournament, match_factory: :running_group_match)
|
||||||
|
@tournament.matches.each_with_index do |m, i|
|
||||||
|
m.state = :not_started if i.even?
|
||||||
|
m.save!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'filters running matches when told to do so' do
|
||||||
|
test_get_index_with_filter('running')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'filters not_started matches when told to do so' do
|
||||||
|
test_get_index_with_filter('not_started')
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'doesn\'t break if the filter contains rubbish' do
|
||||||
|
get :index, params: { state: 'saftladen', tournament_id: @tournament.to_param }
|
||||||
|
body = deserialize_response response
|
||||||
|
expect(body.empty?).to be true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'GET #show' do
|
describe 'GET #show' do
|
||||||
it 'should return success' do
|
it 'should return success' do
|
||||||
get :show, params: { id: @match.to_param }
|
get :show, params: { id: @match.to_param }
|
||||||
|
|
|
||||||
|
|
@ -30,4 +30,30 @@ RSpec.describe Tournament, type: :model do
|
||||||
it { should have_many :teams }
|
it { should have_many :teams }
|
||||||
it { should have_many :stages }
|
it { should have_many :stages }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '#matches' do
|
||||||
|
context 'group stage tournament' do
|
||||||
|
before do
|
||||||
|
@tournament = create(:group_stage_tournament)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns only matches' do
|
||||||
|
@tournament.matches.each do |m|
|
||||||
|
expect(m).to be_a Match
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'stage tournament' do
|
||||||
|
before do
|
||||||
|
@tournament = create(:stage_tournament)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns only matches' do
|
||||||
|
@tournament.matches.each do |m|
|
||||||
|
expect(m).to be_a Match
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue