diff --git a/app/controllers/matches_controller.rb b/app/controllers/matches_controller.rb index 778fca9..7c0c012 100644 --- a/app/controllers/matches_controller.rb +++ b/app/controllers/matches_controller.rb @@ -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 diff --git a/app/models/tournament.rb b/app/models/tournament.rb index ec71609..d91ec72 100644 --- a/app/models/tournament.rb +++ b/app/models/tournament.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 61901bd..143d90c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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] diff --git a/spec/controllers/matches_controller_spec.rb b/spec/controllers/matches_controller_spec.rb index 061c4e8..d36e9ba 100644 --- a/spec/controllers/matches_controller_spec.rb +++ b/spec/controllers/matches_controller_spec.rb @@ -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 } diff --git a/spec/models/tournament_spec.rb b/spec/models/tournament_spec.rb index 20e6f6b..a555abe 100644 --- a/spec/models/tournament_spec.rb +++ b/spec/models/tournament_spec.rb @@ -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