Exemplo n.º 1
0
        public PagedList <Player> GetPlayersFromTrueskillHistory(PlayersResourceParameters playersResourceParameters, int gameNum)
        {
            //var gameTournament1 = _context.Tournaments
            //var playersTrueSkillHistory = _context.TrueskillHistories.Where(tsh => tsh.PlayerId == playerId);


            //var gameTournament = _context.Tournaments
            //    .Where(t => t.GameId == Guid.Parse("1F52BB15-DFEF-4FD3-9C0A-E3F8260F9A1C"));


            //Guid gameId = Guid.Parse("1F52BB15-DFEF-4FD3-9C0A-E3F8260F9A1C"); //s4
            Guid gameId = Guid.Parse("8FA6C1F8-B06F-4020-A154-3A88260515A4"); //melee

            //playersTrueSkillHistory.Where(p => p.TournamentId == )

            var allPlayersForGame = (from history in _context.TrueskillHistories
                                     join tourney in _context.Tournaments on history.TournamentId equals tourney.Id
                                     join players in _context.Players on history.PlayerId equals players.Id
                                     join games in _context.Games on tourney.GameId equals games.Id
                                     where games.Enum == gameNum

                                     select new Player()
            {
                Trueskill = history.Trueskill,
                Id = players.Id,
                Tag = players.Tag,
                State = players.State,
                FirstName = players.FirstName,
                LastName = players.LastName,
                SggPlayerId = players.SggPlayerId,
                LastActive = tourney.Date
            }
                                     )
                                    .OrderByDescending(t => t.LastActive)
                                    .ThenBy(p => p.Trueskill)
                                    .GroupBy(p => p.Id)
                                    .Select(p => p.First());
            //TODO look at group by, look at max with max of tournament date.
            //.ToList();
            //return allPlayersForGame.OrderByDescending(p => p.Trueskill);

            IQueryable <Player> collectionBeforePaging = allPlayersForGame.ApplySort(playersResourceParameters.OrderBy,
                                                                                     _propertyMappingService.GetPropertyMapping <PlayerDto, Player>());

            if (!string.IsNullOrEmpty(playersResourceParameters.State))
            {
                // trim & ignore casing
                var stateForWhereClause = playersResourceParameters.State
                                          .Trim().ToLowerInvariant();
                collectionBeforePaging = collectionBeforePaging
                                         .Where(a => a.State.ToLowerInvariant() == stateForWhereClause);
            }

            if (!string.IsNullOrEmpty(playersResourceParameters.SearchQuery))
            {
                // trim & ignore casing
                var searchQueryForWhereClause = playersResourceParameters.SearchQuery
                                                .Trim().ToLowerInvariant();

                collectionBeforePaging = collectionBeforePaging
                                         .Where(a => a.State.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                                a.FirstName.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                                a.LastName.ToLowerInvariant().Contains(searchQueryForWhereClause) ||
                                                a.Tag.ToLowerInvariant().Contains(searchQueryForWhereClause));
            }

            return(PagedList <Player> .Create(collectionBeforePaging, playersResourceParameters.PageNumber, playersResourceParameters.PageSize));
        }
Exemplo n.º 2
0
        public IActionResult GetPlayersFromTrueskillHistory(PlayersResourceParameters playersResourceParameters, string game)
        {
            if (!_propertyMappingService.ValidMappingExistsFor <PlayerDto, Player>(playersResourceParameters.OrderBy))
            {
                return(BadRequest());
            }

            if (!_typeHelperService.TypeHasProperties <PlayerDto>(playersResourceParameters.Fields))
            {
                return(BadRequest());
            }

            int gameNum = GetGameFromUrl(game);

            if (gameNum == -1)
            {
                return(BadRequest());
            }


            //TODO: Add gameNum to this request!
            var playersFromRepo = _hartPRRepository.GetPlayersFromTrueskillHistory(playersResourceParameters, gameNum);

            if (playersFromRepo == null)
            {
                return(NotFound());
            }

            var players = Mapper.Map <IEnumerable <PlayerDto> >(playersFromRepo);

            //return Ok(players);

            var paginationMetadata = new
            {
                totalCount  = playersFromRepo.TotalCount,
                pageSize    = playersFromRepo.PageSize,
                currentPage = playersFromRepo.CurrentPage,
                totalPages  = playersFromRepo.TotalPages,
            };

            //TODO: Figure out exactly waht this is doing, check the pluralsight course
            //Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(paginationMetadata));

            var links = CreateLinksForPlayers(playersResourceParameters, playersFromRepo.HasNext, playersFromRepo.HasPrevious);


            var shapedPlayers = players.ShapeData(playersResourceParameters.Fields);

            var shapedPlayersWithLinks = shapedPlayers.Select(player =>
            {
                var playerAsDictionary = player as IDictionary <string, object>;
                var playerLinks        = CreateLinksForPlayer((Guid)playerAsDictionary["Id"], gameNum, playersResourceParameters.Fields);

                playerAsDictionary.Add("links", playerLinks);

                return(playerAsDictionary);
            });

            var linkedCollectionResource = new
            {
                value = shapedPlayersWithLinks,
                links = links
            };

            return(Ok(linkedCollectionResource));
        }