Merge pull request #72 from turniere/ticket/TUR-2

Implement Matches endpoint for tournament with filter
This commit is contained in:
Daniel Schädler 2019-10-26 01:52:11 +02:00 committed by GitHub
commit 5da941c002
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 0 deletions

View File

@ -4,6 +4,19 @@ class MatchesController < ApplicationController
before_action :set_match, only: %i[show update]
before_action :validate_params, 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
def show
@ -61,6 +74,10 @@ class MatchesController < ApplicationController
@match = Match.find(params[:id])
end
def set_tournament
@tournament = Tournament.find(params[:tournament_id])
end
def match_params
params.slice(:state).permit!
end

View File

@ -16,6 +16,10 @@ class Tournament < ApplicationRecord
after_initialize :generate_code
def matches
[stages.map(&:matches), stages.map { |s| s.groups.map(&:matches) }].flatten
end
private
def generate_code

View File

@ -13,6 +13,7 @@ Rails.application.routes.draw do
resources :teams, only: %i[show update]
resources :tournaments do
resources :statistics, only: %i[index]
resources :matches, only: %i[index]
end
resources :match_scores, only: %i[show update]
resources :groups, only: %i[show]

View File

@ -2,6 +2,13 @@
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
before do
@match = create(:match, state: :not_started)
@ -12,6 +19,32 @@ RSpec.describe MatchesController, type: :controller do
@match.match_scores = create_pair(:match_score)
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
it 'should return success' do
get :show, params: { id: @match.to_param }

View File

@ -30,4 +30,30 @@ RSpec.describe Tournament, type: :model do
it { should have_many :teams }
it { should have_many :stages }
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