From 99bfe6ed704457813afb690fae5e6a776d4096d4 Mon Sep 17 00:00:00 2001 From: Thor77 Date: Mon, 13 May 2019 14:19:15 +0200 Subject: [PATCH] Add StatisticsController --- app/controllers/statistics_controller.rb | 26 ++++++++++ .../controllers/statistics_controller_spec.rb | 50 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 app/controllers/statistics_controller.rb create mode 100644 spec/controllers/statistics_controller_spec.rb diff --git a/app/controllers/statistics_controller.rb b/app/controllers/statistics_controller.rb new file mode 100644 index 0000000..0b25abf --- /dev/null +++ b/app/controllers/statistics_controller.rb @@ -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 diff --git a/spec/controllers/statistics_controller_spec.rb b/spec/controllers/statistics_controller_spec.rb new file mode 100644 index 0000000..ff43d90 --- /dev/null +++ b/spec/controllers/statistics_controller_spec.rb @@ -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