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();
        }
Exemple #2
0
        public async Task <PagedResult <LeagueDto> > Handle(LeaguesQuery request, CancellationToken cancellationToken)
        {
            var leagueRepo = _uow.GetRepositoryAsync <League>();

            Expression <Func <League, bool> > lPredicate = null;

            if (!(request.LeagueId is null) && request.LeagueId.Length != 0)
            {
                lPredicate = league => request.LeagueId.Contains(league.Id);
            }

            IPaginate <League> leagues =
                await leagueRepo.GetListAsync(
                    lPredicate,
                    size : request.Size,
                    index : request.Page,
                    include : q => q.Include(league => league.LeaguePlayerLinks),
                    cancellationToken : cancellationToken);

            List <LeagueDto> resultLeagues = leagues.Items
                                             .Select(x => _mapper.Map <LeagueDto>(x))
                                             .ToList();

            Func <LeaguePlayerLink, bool> lplPredicate;

            if (request.PlayerExternalId is null)
            {
                if (request.Pid is null || request.Pid.Length == 0)
                {
                    return(GetPagedResult(resultLeagues, leagues));
                }
                lplPredicate = l => request.Pid.Contains(l.PlayerId);
            }
            else
            {
                var playerInternalIdDto = await _playersService.GetInternalIdAsync(request.PlayerExternalId);

                if (playerInternalIdDto is null)
                {
                    return(null);
                }
                lplPredicate = l => l.PlayerId == playerInternalIdDto.InternalId;
            }

            foreach (var league in leagues.Items)
            {
                var distinctLinks = league.LeaguePlayerLinks
                                    .Where(lplPredicate)
                                    .OrderBy(p => p.CreationTime)
                                    .GroupBy(p => p.PlayerId)
                                    .Select(g => g.Last())
                                    .ToList();

                resultLeagues.First(x => x.Id == league.Id).PlayersLeagueStatuses = EnrichByPlayerInformation(distinctLinks);
            }

            return(GetPagedResult(resultLeagues, leagues));
        }
Exemple #3
0
        public async Task <PagedResult <Tour> > GetMyTours(
            [FromQuery] ToursQueryType queryType,
            [FromQuery] int?last,
            [FromQuery] int?size,
            [FromQuery] int?page)
        {
            var playerExternalId = _identityService.GetUserIdentity();
            var internalId       = (await _playersService.GetInternalIdAsync(playerExternalId)).InternalId;

            var query = new ToursQuery(internalId, last, queryType, Array.Empty <int>(), size, page);

            return(await _toursService.GetAsync(query));
        }
        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);
            }