public async Task HandleAsync(PlayerRegisterOnTour command, ICorrelationContext context)
        {
            await ValidateTourOrThrowAsync(command);

            DateTime.TryParse(command.RegistrationDate, out var registrationDate);

            if (string.IsNullOrWhiteSpace(command.PlayerExternalId))
            {
                if (command.PlayerInternalIds is null || command.PlayerInternalIds.Length == 0)
                {
                    throw new FliGenException(ErrorCodes.EmptyPlayersList, "Players id list is empty.");
                }
                var tourRegistrationRepo = _uow.GetRepositoryAsync <TourRegistration>();
                foreach (var playerInternalId in command.PlayerInternalIds)
                {
                    await RegisterPlayer(tourRegistrationRepo, command.TourId, playerInternalId, registrationDate, true);
                }
            }
            else
            {
                PlayerInternalIdDto playerInternalIdDto = await _playersService.GetInternalIdAsync(command.PlayerExternalId);
                await ValidatePlayerStatusOrThrowAsync(command, playerInternalIdDto);

                var tourRegistrationRepo = _uow.GetRepositoryAsync <TourRegistration>();
                await RegisterPlayer(tourRegistrationRepo, command.TourId, playerInternalIdDto.InternalId, registrationDate);
            }

            _uow.SaveChanges();
        }
예제 #2
0
        public async Task LeagueQueryWithExternalIdOnlyShouldReturnValidData()
        {
            var command = new LeaguesQuery
            {
                PlayerExternalId = Guid.NewGuid().ToString(),
            };

            var retDto = new PlayerInternalIdDto
            {
                InternalId = _testDbFixture.MockedDataInstance.Player1
            };

            var playersService = Substitute.For <IPlayersService>();

            playersService.GetInternalIdAsync(command.PlayerExternalId).ReturnsForAnyArgs(retDto);

            await using (var context = _testDbFixture.LeaguesContextFactory.Create())
            {
                var uow                 = new UnitOfWork <LeaguesContext>(context);
                var commandHandler      = new LeaguesQueryHandler(uow, _mapper, playersService);
                List <LeagueDto> result = (await commandHandler.Handle(command, CancellationToken.None)).Items.ToList();
                result.Count.Should().Be(context.Leagues.Count());

                List <PlayerWithLeagueStatusDto> playerWithLeagueStatuses = result.SelectMany(l => l.PlayersLeagueStatuses).ToList();

                const int player1ExistsInLeaguesCount = 2;
                playerWithLeagueStatuses.Count.Should().Be(player1ExistsInLeaguesCount);
            }
        }
        private async Task ValidatePlayerStatusOrThrowAsync(
            PlayerRegisterOnTour command,
            PlayerInternalIdDto playerInternalIdDto)
        {
            if (playerInternalIdDto is null)
            {
                throw new FliGenException(
                          ErrorCodes.NoPlayerWithSuchId,
                          $"There is no player with external id: {command.PlayerExternalId}");
            }

            var leagues = await _leaguesService.GetAsync(
                new LeaguesQuery
            {
                PlayerExternalId = command.PlayerExternalId,
                LeagueId         = new[] { command.LeagueId }
            });

            if (leagues is null ||
                leagues.IsEmpty)
            {
                throw new FliGenException(
                          ErrorCodes.NoLeagueWithSuchId,
                          $"There is no league with id: {command.LeagueId}");
            }

            var leaguesArr = leagues.Items.ToList();

            if (leaguesArr.Count == 0)
            {
                throw new FliGenException(
                          ErrorCodes.PlayerIsNotAMemberOfLeague,
                          $"Player with id {command.PlayerExternalId} is not a member of league: {command.LeagueId}");
            }

            if (leaguesArr.Count != 0)
            {
                var statuses = leaguesArr[0].PlayersLeagueStatuses.ToArray();
                if (statuses.Length == 0 || statuses[0].PlayerLeagueJoinStatus != PlayerLeagueJoinStatus.Joined)
                {
                    throw new FliGenException(
                              ErrorCodes.PlayerIsNotAMemberOfLeague,
                              $"Player with id {command.PlayerExternalId} is not a member of league: {command.LeagueId}");
                }
            }
        }
예제 #4
0
        public async Task HandleAsync(JoinLeague command, ICorrelationContext context)
        {
            PlayerInternalIdDto playerIdDto = await _playersService.GetInternalIdAsync(command.PlayerExternalId);

            if (playerIdDto is null)
            {
                throw new FliGenException(ErrorCodes.NoPlayerWithSuchExternalId, $"There is no player with external id: {command.PlayerExternalId}");
            }

            int playerId = playerIdDto.InternalId;

            var leagueSettingsRepo = _uow.GetRepositoryAsync <Domain.Entities.LeagueSettings>();

            Domain.Entities.LeagueSettings leagueSettings =
                await leagueSettingsRepo.SingleAsync(x => x.LeagueId == command.LeagueId);

            if (leagueSettings is null)
            {
                throw new FliGenException(ErrorCodes.NoLeagueSettings, $"There is no league settings for league: {command.LeagueId}");
            }

            var lplinksRepo = _uow.GetRepositoryAsync <LeaguePlayerLink>();

            LeaguePlayerLink lastLink = await
                                        lplinksRepo.SingleAsync(x =>
                                                                x.PlayerId == playerId &&
                                                                x.LeagueId == command.LeagueId &&
                                                                x.Actual);

            if (lastLink is null || lastLink.InLeftStatus())
            {
                LeaguePlayerLink link = leagueSettings.RequireConfirmation
                            ? LeaguePlayerLink.CreateWaitingLink(command.LeagueId, playerId)
                            : LeaguePlayerLink.CreateJoinedLink(command.LeagueId, playerId);

                if (leagueSettings.RequireConfirmation)
                {
                    LeaguePlayerLink.CreateWaitingLink(command.LeagueId, playerId);
                }
                else
                {
                    LeaguePlayerLink.CreateJoinedLink(command.LeagueId, playerId);
                }

                await lplinksRepo.AddAsync(link);
            }
예제 #5
0
        public async Task JoinLeagueShouldChangePlayerStatus()
        {
            var fix      = new Fixture();
            int leagueId = fix.Create <int>();
            int playerId = fix.Create <int>();
            var command  = new JoinLeague()
            {
                LeagueId         = leagueId,
                PlayerExternalId = Guid.NewGuid().ToString()
            };

            var retDto = new PlayerInternalIdDto()
            {
                InternalId = playerId
            };

            var uow            = Substitute.For <IUnitOfWork>();
            var playersService = Substitute.For <IPlayersService>();

            playersService.GetInternalIdAsync(command.PlayerExternalId).ReturnsForAnyArgs(retDto);

            var leagueSettingsRepo    = Substitute.For <IRepositoryAsync <LeagueSettings> >();
            var leaguePlayerLinksRepo = Substitute.For <IRepositoryAsync <LeaguePlayerLink> >();

            var lsWithoutConfirm = LeagueSettings.Create(true, false, leagueId);
            var lsWithConfirm    = LeagueSettings.Create(true, true, leagueId);

            var leftLpl = LeaguePlayerLink.CreateJoinedLink(leagueId, playerId);

            leftLpl.UpdateToLeft();

            var hs = new HashSet <(LeagueSettings, LeaguePlayerLink, Action)>
            {
                (
                    lsWithoutConfirm,
                    null,
                    () => leaguePlayerLinksRepo.Received().AddAsync(Arg.Is <LeaguePlayerLink>(x => x.InJoinedStatus()))
                ),
                (
                    lsWithoutConfirm,
                    leftLpl,
                    () => leaguePlayerLinksRepo.Received().AddAsync(Arg.Is <LeaguePlayerLink>(x => x.InJoinedStatus()))
                ),
                (
                    lsWithoutConfirm,
                    LeaguePlayerLink.CreateWaitingLink(leagueId, playerId),
                    () => leaguePlayerLinksRepo.Received().UpdateAsync(Arg.Is <LeaguePlayerLink>(x => x.InJoinedStatus()))
                ),
                (
                    lsWithoutConfirm,
                    LeaguePlayerLink.CreateJoinedLink(leagueId, playerId),
                    () => leaguePlayerLinksRepo.Received().UpdateAsync(Arg.Is <LeaguePlayerLink>(x => x.InLeftStatus()))
                ),
                (
                    lsWithConfirm,
                    null,
                    () => leaguePlayerLinksRepo.Received().AddAsync(Arg.Is <LeaguePlayerLink>(x => x.InWaitingStatus()))
                ),
                (
                    lsWithConfirm,
                    leftLpl,
                    () => leaguePlayerLinksRepo.Received().AddAsync(Arg.Is <LeaguePlayerLink>(x => x.InJoinedStatus()))
                ),
                (
                    lsWithConfirm,
                    LeaguePlayerLink.CreateWaitingLink(leagueId, playerId),
                    () => leaguePlayerLinksRepo.Received().RemoveAsync(Arg.Is <LeaguePlayerLink>(x => x.InWaitingStatus()))
                ),
                (
                    lsWithConfirm,
                    LeaguePlayerLink.CreateJoinedLink(leagueId, playerId),
                    () => leaguePlayerLinksRepo.Received().UpdateAsync(Arg.Is <LeaguePlayerLink>(x => x.InLeftStatus()))
                ),
            };

            foreach (var(lsItem, lplItem, checkAction) in hs)
            {
                leagueSettingsRepo.SingleAsync().ReturnsForAnyArgs(lsItem);
                leaguePlayerLinksRepo.SingleAsync().ReturnsForAnyArgs(lplItem);

                uow.GetRepositoryAsync <LeagueSettings>().ReturnsForAnyArgs(leagueSettingsRepo);
                uow.GetRepositoryAsync <LeaguePlayerLink>().ReturnsForAnyArgs(leaguePlayerLinksRepo);

                var commandHandler = new JoinLeagueHandler(uow, playersService);
                await commandHandler.HandleAsync(command, new CorrelationContext());

                checkAction();
            }
        }