diff --git a/app/controllers/match_scores_controller.rb b/app/controllers/match_scores_controller.rb index 0eb5ed2..c1bc65e 100644 --- a/app/controllers/match_scores_controller.rb +++ b/app/controllers/match_scores_controller.rb @@ -13,7 +13,10 @@ class MatchScoresController < ApplicationController # PATCH/PUT /scores/1 def update if @match_score.update(match_score_params) - GroupStageService.calculate_group_points(@match_score) + if @match_score.part_of_group_match? + match_score_group = @match_score.match.group + UpdateGroupsGroupScoresAndSave.call(group: match_score_group) + end render json: @match_score else render json: @match_score.errors, status: :unprocessable_entity diff --git a/app/interactors/update_groups_group_scores.rb b/app/interactors/update_groups_group_scores.rb new file mode 100644 index 0000000..dd5dc3e --- /dev/null +++ b/app/interactors/update_groups_group_scores.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +class UpdateGroupsGroupScores + include Interactor + + def call + group = context.group + begin + group_scores = GroupStageService.update_group_scores(group) + context.object_to_save = group_scores + rescue StandardError + context.fail! + end + end +end diff --git a/app/organizers/update_groups_group_scores_and_save.rb b/app/organizers/update_groups_group_scores_and_save.rb new file mode 100644 index 0000000..41d641f --- /dev/null +++ b/app/organizers/update_groups_group_scores_and_save.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class UpdateGroupsGroupScoresAndSave + include Interactor::Organizer + + organize UpdateGroupsGroupScores, SaveApplicationRecordObject +end diff --git a/spec/interactors/update_groups_group_scores_interactor_spec.rb b/spec/interactors/update_groups_group_scores_interactor_spec.rb new file mode 100644 index 0000000..83d1068 --- /dev/null +++ b/spec/interactors/update_groups_group_scores_interactor_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +RSpec.describe UpdateGroupsGroupScores do + before do + @group = create(:group) + @group_scores = create_list(:group_score, 2) + end + + context 'save succeeds' do + let(:context) do + UpdateGroupsGroupScores.call(group: @group) + end + + before do + allow(GroupStageService) + .to receive(:update_group_scores).with(@group) + .and_return(@group_scores) + end + + it 'succeeds' do + expect(context).to be_a_success + end + + it 'provides the objects to save' do + expect(context.object_to_save).to eq(@group_scores) + end + end + + context 'exception is thrown' do + let(:context) do + UpdateGroupsGroupScores.call(group: @group) + end + before do + allow(GroupStageService) + .to receive(:update_group_scores).with(@group) + .and_throw('This failed :(') + end + + it 'fails' do + test = context.failure? + expect(test).to eq(true) + end + end +end