/// <summary>
        /// Handles the command.
        /// </summary>
        /// <param name="command">The command.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        private async Task HandleCommand(CreateTournamentCommand command,
                                         CancellationToken cancellationToken)
        {
            // Rehydrate the aggregate
            TournamentAggregate tournament = await this.TournamentRepository.GetLatestVersion(command.TournamentId, cancellationToken);

            // Get the club to validate the input
            GolfClubAggregate club = await this.GolfClubRepository.GetLatestVersion(command.GolfClubId, cancellationToken);

            // bug #29 fixes (throw exception if club not created)
            if (!club.HasBeenCreated)
            {
                throw new NotFoundException($"No created golf club found with Id {command.GolfClubId}");
            }

            // Club is valid, now check the measured course, this will throw exception if not found
            MeasuredCourseDataTransferObject measuredCourse = club.GetMeasuredCourse(command.CreateTournamentRequest.MeasuredCourseId);

            tournament.CreateTournament(command.CreateTournamentRequest.TournamentDate,
                                        command.GolfClubId,
                                        command.CreateTournamentRequest.MeasuredCourseId,
                                        measuredCourse.StandardScratchScore,
                                        command.CreateTournamentRequest.Name,
                                        (PlayerCategory)command.CreateTournamentRequest.MemberCategory,
                                        (TournamentFormat)command.CreateTournamentRequest.Format);

            // Save the changes
            await this.TournamentRepository.SaveChanges(tournament, cancellationToken);

            // Setup the response
            command.Response = new CreateTournamentResponse
            {
                TournamentId = command.TournamentId
            };
        }
        public static TournamentAggregate GetCreatedTournamentAggregate()
        {
            TournamentAggregate aggregate = TournamentAggregate.Create(TournamentTestData.AggregateId);

            aggregate.CreateTournament(TournamentTestData.TournamentDate, TournamentTestData.GolfClubId, TournamentTestData.MeasuredCourseId, TournamentTestData.MeasuredCourseSSS, TournamentTestData.Name, TournamentTestData.PlayerCategoryEnum, TournamentTestData.TournamentFormatEnum);

            return(aggregate);
        }
        public static TournamentAggregate GetCreatedTournamentWithScoresRecordedAggregate()
        {
            TournamentAggregate aggregate = TournamentAggregate.Create(TournamentTestData.AggregateId);

            aggregate.CreateTournament(TournamentTestData.TournamentDate, TournamentTestData.GolfClubId, TournamentTestData.MeasuredCourseId, TournamentTestData.MeasuredCourseSSS, TournamentTestData.Name, TournamentTestData.PlayerCategoryEnum, TournamentTestData.TournamentFormatEnum);

            aggregate.SignUpForTournament(TournamentTestData.PlayerId);

            aggregate.RecordPlayerScore(TournamentTestData.PlayerId, TournamentTestData.PlayingHandicap, TournamentTestData.HoleScores);

            return(aggregate);
        }
        public void TournamentAggregate_CreateTournament_TournamentAlreadyCreated_ErrorThrown()
        {
            TournamentAggregate aggregate = TournamentTestData.GetCreatedTournamentAggregate();

            Should.Throw <InvalidOperationException>(() =>
            {
                aggregate.CreateTournament(TournamentTestData.TournamentDate,
                                           TournamentTestData.GolfClubId,
                                           TournamentTestData.MeasuredCourseId,
                                           TournamentTestData.MeasuredCourseSSS,
                                           TournamentTestData.Name,
                                           TournamentTestData.PlayerCategoryEnum,
                                           TournamentTestData.TournamentFormatEnum);
            });
        }
        public void TournamentAggregate_CreateTournament_InvalidData_ErrorThrown(Boolean validTournamentDate,
                                                                                 Boolean validGolfClubId,
                                                                                 Boolean validMeasuredCourseId,
                                                                                 Int32 measuredCourseSSS,
                                                                                 String name,
                                                                                 PlayerCategory memberCategory,
                                                                                 TournamentFormat tournamentFormat,
                                                                                 Type exceptionType)
        {
            TournamentAggregate aggregate = TournamentTestData.GetEmptyTournamentAggregate();

            DateTime tournamentDate   = validTournamentDate ? TournamentTestData.TournamentDate : DateTime.MinValue;
            Guid     golfClubId       = validGolfClubId ? TournamentTestData.GolfClubId : Guid.Empty;
            Guid     measuredCourseId = validMeasuredCourseId ? TournamentTestData.MeasuredCourseId : Guid.Empty;

            Should.Throw(() => { aggregate.CreateTournament(tournamentDate, golfClubId, measuredCourseId, measuredCourseSSS, name, memberCategory, tournamentFormat); },
                         exceptionType);
        }
        public static TournamentAggregate GetCompletedTournamentAggregate(Int32 category1Scores = 1, Int32 category2Scores  = 2, Int32 category3Scores = 7,
                                                                          Int32 category4Scores = 20, Int32 category5Scores = 5, Int32 bufferorbetter  = 5)
        {
            TournamentAggregate aggregate = TournamentAggregate.Create(TournamentTestData.AggregateId);

            aggregate.CreateTournament(TournamentTestData.TournamentDate, TournamentTestData.GolfClubId, TournamentTestData.MeasuredCourseId, TournamentTestData.MeasuredCourseSSS, TournamentTestData.Name, TournamentTestData.PlayerCategoryEnum, TournamentTestData.TournamentFormatEnum);

            List <GeneratedPlayerScore> scoresToRecord = TournamentTestData.GenerateScores(category1Scores, category2Scores, category3Scores, category4Scores, category5Scores, bufferorbetter);

            foreach (GeneratedPlayerScore playerScoreForTest in scoresToRecord)
            {
                aggregate.SignUpForTournament(playerScoreForTest.PlayerId);
                aggregate.RecordPlayerScore(playerScoreForTest.PlayerId, playerScoreForTest.Handicap, playerScoreForTest.HoleScores);
            }

            aggregate.CompleteTournament(TournamentTestData.CompletedDateTime);

            return(aggregate);
        }
        public void TournamentAggregate_CreateTournament_TournamentCreated()
        {
            TournamentAggregate aggregate = TournamentTestData.GetEmptyTournamentAggregate();

            aggregate.CreateTournament(TournamentTestData.TournamentDate,
                                       TournamentTestData.GolfClubId,
                                       TournamentTestData.MeasuredCourseId,
                                       TournamentTestData.MeasuredCourseSSS,
                                       TournamentTestData.Name,
                                       TournamentTestData.PlayerCategoryEnum,
                                       TournamentTestData.TournamentFormatEnum);

            aggregate.TournamentDate.ShouldBe(TournamentTestData.TournamentDate);
            aggregate.GolfClubId.ShouldBe(TournamentTestData.GolfClubId);
            aggregate.MeasuredCourseId.ShouldBe(TournamentTestData.MeasuredCourseId);
            aggregate.MeasuredCourseSSS.ShouldBe(TournamentTestData.MeasuredCourseSSS);
            aggregate.Name.ShouldBe(TournamentTestData.Name);
            aggregate.PlayerCategory.ShouldBe(TournamentTestData.PlayerCategoryEnum);
            aggregate.Format.ShouldBe(TournamentTestData.TournamentFormatEnum);
            aggregate.HasBeenCreated.ShouldBeTrue();
        }