public void TournamentAggregate_CanBeCreated_EmptyAggregateId_ErrorThrown()
 {
     Should.Throw <ArgumentNullException>(() =>
     {
         TournamentAggregate aggregate = TournamentAggregate.Create(Guid.Empty);
     });
 }
        public void TournamentAggregate_CanBeCreated_IsCreated()
        {
            TournamentAggregate aggregate = TournamentAggregate.Create(TournamentTestData.AggregateId);

            aggregate.ShouldNotBeNull();
            aggregate.AggregateId.ShouldBe(TournamentTestData.AggregateId);
        }
        /// <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 void TournamentAggregate_ProduceResult_ResultIsProduced()
        {
            TournamentAggregate tournamentAggregate = TournamentTestData.GetCompletedTournamentAggregateWithCSSCalculatedAggregate(20, 15, 23, 16, 0, 5);

            tournamentAggregate.ProduceResult();

            tournamentAggregate.HasResultBeenProduced.ShouldBeTrue();

            List <PlayerScoreRecordDataTransferObject> scores = tournamentAggregate.GetScores();

            scores.Any(s => s.Last9HolesScore == 0).ShouldBeFalse();
            scores.Any(s => s.Last6HolesScore == 0).ShouldBeFalse();
            scores.Any(s => s.Last3HolesScore == 0).ShouldBeFalse();
            scores.Any(s => s.TournamentDivision == 0).ShouldBeFalse();
            scores.Any(s => s.Position == 0).ShouldBeFalse();
            scores.Any(s => s.MeasuredCourseId == Guid.Empty).ShouldBeFalse();
            scores.Any(s => s.PlayerId == Guid.Empty).ShouldBeFalse();
            scores.Any(s => s.NetScore == 0).ShouldBeFalse();
            scores.Any(s => s.GrossScore == 0).ShouldBeFalse();
            scores.Any(s => s.PlayingHandicap < 0).ShouldBeFalse();
            scores.Any(s => s.PlayingHandicap > 36).ShouldBeFalse();
            scores.Any(s => s.TournamentId == Guid.Empty).ShouldBeFalse();
            scores.Any(s => s.ScoreDate == DateTime.MinValue).ShouldBeFalse();
            scores.Any(s => s.GolfClubId == Guid.Empty).ShouldBeFalse();
            scores.Any(s => s.HandicapCategory == 0).ShouldBeFalse();
            scores.Any(s => s.HoleScores.Count() != 18).ShouldBeFalse();
            scores.Any(s => s.CSS == 0).ShouldBeFalse();
        }
Exemple #5
0
 private TournamentAggregate GetNewTournament()
 {
     return(TournamentAggregate.CreateNew(
                _tournamentId,
                _tournamentName,
                _tournamentDescription));
 }
Exemple #6
0
        public void publish_tournament_without_steps_should_throw()
        {
            var tournament = TournamentAggregate.Restore(_tournamentId, _tournamentName, _tournamentDescription, false,
                                                         DateTime.Now, DateTime.Now.AddDays(1), new List <StepEntity>());

            Assert.Throws <DomainException>(() => tournament.Publish());
        }
        public async Task SignUpPlayerForTournament(Guid tournamentId,
                                                    Guid playerId, CancellationToken cancellationToken)
        {
            // Validate the tournament Id
            TournamentAggregate tournament = await this.TournamentRepository.GetLatestVersion(tournamentId, cancellationToken);

            PlayerAggregate player = await this.PlayerRepository.GetLatestVersion(playerId, cancellationToken);

            if (!player.HasBeenRegistered)
            {
                throw new InvalidOperationException("A player must be registered to sign up for a club tournament");
            }

            try
            {
                GolfClubMembershipAggregate golfClubMembership = await this.ClubMembershipRepository.GetLatestVersion(tournament.GolfClubId, cancellationToken);

                golfClubMembership.GetMembership(player.AggregateId, player.DateOfBirth, player.Gender);
            }
            catch (NotFoundException nex)
            {
                throw new InvalidOperationException("A player must be a member of the club to sign up for a club tournament");
            }

            tournament.SignUpForTournament(playerId);

            await this.TournamentRepository.SaveChanges(tournament, cancellationToken);
        }
Exemple #8
0
        private async Task <Tournament> ToModel(TournamentAggregate aggregate)
        {
            var tournament = await GetTournament(aggregate.Id.Value);

            var removedSteps = GetRemovedSteps(aggregate, tournament);
            var currentSteps = GetCurrentSteps(tournament)
                               .Where(step => !removedSteps.Contains(step))
                               .ToList();
            var newSteps = GetNewSteps(aggregate, currentSteps);

            currentSteps.ForEach(currentStep =>
            {
                var step               = aggregate.Steps.First(m => m.Id.Value == currentStep.StepId);
                currentStep.Order      = step.Order;
                currentStep.IsOptional = step.IsOptional;
            });
            newSteps.ForEach(tS => tournament.TournamentSteps.Add(tS));
            _context.TournamentSteps.RemoveRange(removedSteps);
            tournament.Name        = aggregate.Name;
            tournament.Description = aggregate.Description;
            tournament.EndDate     = aggregate.EndDate;
            tournament.StartDate   = aggregate.StartDate;
            tournament.IsPublished = aggregate.IsPublished;
            return(tournament);
        }
        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 void TournamentAggregate_CalculateCSS_CSSAlreadyCalculated_ErrorThrown()
        {
            TournamentAggregate aggregate = TournamentTestData.GetCompletedTournamentAggregate(1, 2, 7, 20, 5, 5);

            aggregate.CalculateCSS();

            Should.Throw <InvalidOperationException>(() => { aggregate.CalculateCSS(); });
        }
        public void TournamentAggregate_CompleteTournament_InvalidData_ErrorThrown(Boolean validCompleteDate)
        {
            TournamentAggregate aggregate = TournamentTestData.GetCreatedTournamentWithScoresRecordedAggregate();

            DateTime completeDateTime = validCompleteDate ? TournamentTestData.CompletedDateTime : DateTime.MinValue;

            Should.Throw <ArgumentNullException>(() => { aggregate.CompleteTournament(completeDateTime); });
        }
        public async Task <string> Handle(CreateTournamentCommand request, CancellationToken cancellationToken)
        {
            var tournament = TournamentAggregate.CreateNew(await _tournamentRepository.NextIdAsync(), request.Name,
                                                           request.Description);
            await _tournamentRepository.SetAsync(tournament);

            return(tournament.Id.ToString());
        }
        public void TournamentAggregate_GetScores_ScoresReturned()
        {
            TournamentAggregate aggregate = TournamentTestData.GetCreatedTournamentWithScoresRecordedAggregate();

            List <PlayerScoreRecordDataTransferObject> scores = aggregate.GetScores();

            scores.ShouldNotBeNull();
            scores.Count.ShouldBe(1);
        }
        public void TournamentAggregate_CompleteTournament_TournamentComplete()
        {
            TournamentAggregate aggregate = TournamentTestData.GetCreatedTournamentWithScoresRecordedAggregate();

            aggregate.CompleteTournament(TournamentTestData.CompletedDateTime);

            aggregate.HasBeenCompleted.ShouldBeTrue();
            aggregate.CompletedDateTime.ShouldBe(TournamentTestData.CompletedDateTime);
        }
Exemple #15
0
        public async Task <TournamentId> SetAsync(TournamentAggregate aggregate)
        {
            var tournament = await ToModel(aggregate);

            _context.Tournaments.Upsert(tournament);
            await _context.SaveChangesAsync();

            return(new TournamentId(tournament.Id));
        }
        public void TournamentAggregate_CancelTournament_InvalidData_ErrorThrown(Boolean validCancellationDate,
                                                                                 String cancellationReason)
        {
            TournamentAggregate aggregate = TournamentTestData.GetCreatedTournamentWithScoresRecordedAggregate();

            DateTime cancellationDateTime = validCancellationDate ? TournamentTestData.CancelledDateTime : DateTime.MinValue;

            Should.Throw <ArgumentNullException>(() => { aggregate.CancelTournament(cancellationDateTime, cancellationReason); });
        }
Exemple #17
0
        public void set_start_date_equals_to_end_date_should_throw()
        {
            var endDate    = DateTime.Now;
            var tournament = TournamentAggregate.Restore(
                _tournamentId, _tournamentName, _tournamentDescription,
                false, endDate.AddDays(-1), endDate, new List <StepEntity>());

            Assert.Throws <DomainException>(() => tournament.SetStartDate(endDate));
        }
Exemple #18
0
 private static List <TournamentStep> GetNewSteps(TournamentAggregate aggregate,
                                                  List <TournamentStep> currentSteps)
 {
     return(aggregate.Steps
            .Where(s => currentSteps.All(tS => tS.StepId != s.Id.Value))
            .Select(s => new TournamentStep {
         Order = s.Order, IsOptional = s.IsOptional, StepId = s.Id.Value
     })
            .ToList());
 }
        public void TournamentAggregate_CancelTournament_TournamentComplete()
        {
            TournamentAggregate aggregate = TournamentTestData.GetCreatedTournamentWithScoresRecordedAggregate();

            aggregate.CancelTournament(TournamentTestData.CancelledDateTime, TournamentTestData.CancellationReason);

            aggregate.HasBeenCancelled.ShouldBeTrue();
            aggregate.CancelledDateTime.ShouldBe(TournamentTestData.CancelledDateTime);
            aggregate.CancelledReason.ShouldBe(TournamentTestData.CancellationReason);
        }
Exemple #20
0
        public void publish_tournament_without_end_date_should_throw()
        {
            var steps = new List <StepEntity> {
                GetStep(0, false)
            };
            var tournament = TournamentAggregate.Restore(_tournamentId, _tournamentName, _tournamentDescription, false,
                                                         DateTime.Now, null, steps);

            Assert.Throws <DomainException>(() => tournament.Publish());
        }
        public async Task <IActionResult> GetTournament([FromRoute] Guid tournamentId,
                                                        [FromQuery] Boolean includeScores,
                                                        CancellationToken cancellationToken)
        {
            TournamentAggregate tournament = await this.TournamentRepository.GetLatestVersion(tournamentId, cancellationToken);

            if (!tournament.HasBeenCreated)
            {
                return(this.NotFound($"Tournament not found with Id {tournamentId}"));
            }

            Developer.DataTransferObjects.GetTournamentResponse response = new Developer.DataTransferObjects.GetTournamentResponse
            {
                HasBeenCreated       = tournament.HasBeenCreated,
                Name                 = tournament.Name,
                GolfClubId           = tournament.GolfClubId,
                Adjustment           = tournament.Adjustment,
                CSS                  = tournament.CSS,
                CSSHasBeenCalculated = tournament.CSSHasBeenCalculated,
                CancelledDateTime    = tournament.CancelledDateTime,
                CancelledReason      = tournament.CancelledReason,
                CompletedDateTime    = tournament.CompletedDateTime,
                Format               = tournament.Format.ConvertTo <TournamentFormat>(),
                HasBeenCancelled     = tournament.HasBeenCancelled,
                HasBeenCompleted     = tournament.HasBeenCompleted,
                MeasuredCourseId     = tournament.MeasuredCourseId,
                MeasuredCourseSSS    = tournament.MeasuredCourseSSS,
                MemberCategory       = tournament.PlayerCategory.ConvertTo <PlayerCategory>(),
                TournamentDate       = tournament.TournamentDate
            };

            if (includeScores)
            {
                List <PlayerScoreRecordDataTransferObject> scores = tournament.GetScores();

                if (scores.Count > 0)
                {
                    scores.ForEach(s =>
                    {
                        response.Scores.Add(new MemberScoreRecord
                        {
                            GrossScore       = s.GrossScore,
                            PlayingHandicap  = s.PlayingHandicap,
                            HandicapCategory = s.HandicapCategory,
                            HoleScores       = s.HoleScores,
                            MemberId         = s.PlayerId,
                            NetScore         = s.NetScore
                        });
                    });
                }
            }

            return(this.Ok(response));
        }
        public void TournamentAggregate_RecordPlayerScore_InvalidData_NegativeHoleScores_ErrorThrown(Int32 holeNumber)
        {
            TournamentAggregate aggregate = TournamentTestData.GetCreatedTournamentAggregateWithPlayerSignedUp();

            Should.Throw <InvalidDataException>(() =>
            {
                aggregate.RecordPlayerScore(TournamentTestData.PlayerId,
                                            TournamentTestData.PlayingHandicap,
                                            TournamentTestData.HoleScoresNegativeScore(holeNumber));
            });
        }
        public void TournamentAggregate_RecordPlayerScore_InvalidData_NotAllHoleScores_ErrorThrown()
        {
            TournamentAggregate aggregate = TournamentTestData.GetCreatedTournamentAggregateWithPlayerSignedUp();

            Should.Throw <InvalidDataException>(() =>
            {
                aggregate.RecordPlayerScore(TournamentTestData.PlayerId,
                                            TournamentTestData.PlayingHandicap,
                                            TournamentTestData.HoleScoresNotAllPresent);
            });
        }
        public void TournamentAggregate_RecordPlayerScore_TournamentNotCreated_ErrorThrown()
        {
            TournamentAggregate aggregate = TournamentTestData.GetEmptyTournamentAggregate();

            Should.Throw <InvalidOperationException>(() =>
            {
                aggregate.RecordPlayerScore(TournamentTestData.PlayerId,
                                            TournamentTestData.PlayingHandicap,
                                            TournamentTestData.HoleScores);
            });
        }
        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_RecordPlayerScore_InvalidData_ErrorThrown(Boolean validPlayerId,
                                                                                  Int32 playingHandicap,
                                                                                  Boolean validHoleScores,
                                                                                  Type exceptionType)
        {
            TournamentAggregate aggregate = TournamentTestData.GetEmptyTournamentAggregate();

            Guid playerId = validPlayerId ? TournamentTestData.PlayerId : Guid.Empty;
            Dictionary <Int32, Int32> holeScores = validHoleScores ? TournamentTestData.HoleScores : null;

            Should.Throw(() => { aggregate.RecordPlayerScore(playerId, playingHandicap, holeScores); }, exceptionType);
        }
Exemple #27
0
        public void publish_tournament_should_works()
        {
            var steps = new List <StepEntity> {
                GetStep(0, false)
            };
            var startDate  = DateTime.Now;
            var tournament = TournamentAggregate.Restore(_tournamentId, _tournamentName, _tournamentDescription, false,
                                                         startDate, startDate.AddDays(1), steps);

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

            // Produce the result
            tournament.ProduceResult();

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

            DateTime cancelledDateTime = DateTime.Now;

            tournament.CancelTournament(cancelledDateTime, command.CancelTournamentRequest.CancellationReason);

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

            tournament.RecordPlayerScore(command.PlayerId,
                                         command.RecordPlayerTournamentScoreRequest.PlayingHandicap,
                                         command.RecordPlayerTournamentScoreRequest.HoleScores);

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