Add StatisticsController
This commit is contained in:
parent
bdcfbe934b
commit
99bfe6ed70
|
|
@ -0,0 +1,26 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
class StatisticsController < ApplicationController
|
||||
before_action :set_tournament, only: %i[index]
|
||||
|
||||
# GET /tournaments/1/statistics
|
||||
def index
|
||||
group_stage = @tournament.stages.find_by(level: -1)
|
||||
if group_stage
|
||||
service = StatisticsService.new group_stage
|
||||
render json: {
|
||||
most_dominant_score: service.most_dominant_score,
|
||||
least_dominant_score: service.least_dominant_score,
|
||||
group_scores: service.group_scores
|
||||
}
|
||||
else
|
||||
render json: {}, status: :not_implemented
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_tournament
|
||||
@tournament = Tournament.find(params[:tournament_id])
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe StatisticsController, type: :controller do
|
||||
describe 'GET #index' do
|
||||
context 'tournament without a group stage' do
|
||||
it 'returns a not implemented response' do
|
||||
get :index, params: { tournament_id: create(:tournament).to_param }
|
||||
expect(response).to have_http_status(:not_implemented)
|
||||
end
|
||||
end
|
||||
|
||||
context 'tournament with a group stage' do
|
||||
before do
|
||||
@tournament = create(:group_stage_tournament)
|
||||
@group_stage = @tournament.stages.find_by(level: -1)
|
||||
@most_dominant_score = GroupScore.new team: @tournament.teams.first,
|
||||
group_points: 100,
|
||||
scored_points: 100, received_points: 0
|
||||
@least_dominant_score = GroupScore.new team: @tournament.teams.first,
|
||||
group_points: 0,
|
||||
scored_points: 0, received_points: 100
|
||||
@tournament.stages.first.groups.first.group_scores << @most_dominant_score
|
||||
@tournament.stages.first.groups.first.group_scores << @least_dominant_score
|
||||
@tournament.save!
|
||||
end
|
||||
|
||||
it 'returns a success response' do
|
||||
get :index, params: { tournament_id: @tournament.to_param }
|
||||
expect(response).to be_successful
|
||||
end
|
||||
|
||||
it 'returns a list containing all group scores' do
|
||||
get :index, params: { tournament_id: @tournament.to_param }
|
||||
expect(deserialize_response(response)[:group_scores].length).to eq(GroupScore.count)
|
||||
end
|
||||
|
||||
it 'returns a most dominant group score' do
|
||||
get :index, params: { tournament_id: @tournament.to_param }
|
||||
expect(deserialize_response(response)[:most_dominant_score][:id]).to eq(@most_dominant_score.id)
|
||||
end
|
||||
|
||||
it 'returns a least dominant group score' do
|
||||
get :index, params: { tournament_id: @tournament.to_param }
|
||||
expect(deserialize_response(response)[:least_dominant_score][:id]).to eq(@least_dominant_score.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue