public PagedResponse <Game> GetGames(PaginationQuery paginationQuery, GamesQueryParams gamesQueryParams) { IMongoCollection <Game> _games = _gamesRepository.GetGamesCollection(); FilterDefinition <Game> filter = Builders <Game> .Filter.Where(game => true); SortDefinition <Game> sort = Builders <Game> .Sort.Ascending("Name"); if (gamesQueryParams != null) { filter = Builders <Game> .Filter.Where(game => game.IsActive == gamesQueryParams.IsActive); if (gamesQueryParams.NumberOfPeople != 0) { int number = gamesQueryParams.NumberOfPeople; if (number < 0) { throw new InvalidGamesQueryParamsException("Query parameter NumberOfPeople should be a positive integer"); } filter = Builders <Game> .Filter.And(filter, Builders <Game> .Filter.Where(game => game.NumberOfPlayers >= number)); } if (gamesQueryParams.SearchByName != null) { var queryString = gamesQueryParams.SearchByName.ToLower(); filter = Builders <Game> .Filter.And(filter, Builders <Game> .Filter.Where(game => game.Name.ToLower().Contains(queryString))); } } if (gamesQueryParams != null && gamesQueryParams.OrderBy != null) { //doesn't cause error if value doesnt match any property sort = Builders <Game> .Sort.Ascending(gamesQueryParams.OrderBy); } if (paginationQuery != null) { //TODO: If page size smaller than 0 or page number smaller than 0 (if they are not integers fluent validator catches) var take = paginationQuery.PageSize > 100? 100:paginationQuery.PageSize; var skip = (paginationQuery.PageNumber - 1) * take; if (take < 0 || skip < 0) { throw new InvalidGamesQueryParamsException("Query parameters PageSize and PageNumber should be a positive integer"); } int numOfGames = (int)_games.Find(filter).CountDocuments(); List <Game> gamesList = _games.Find(filter).Sort(sort).Skip(skip).Limit(take).ToList(); return(new PagedResponse <Game>(gamesList, paginationQuery, numOfGames)); } int numberOfGames = (int)_games.Find(filter).CountDocuments(); List <Game> games = _games.Find(filter).Sort(sort).ToList(); return(new PagedResponse <Game>(games, numberOfGames)); }
public IActionResult GetFiltered([FromQuery] PaginationQuery paginationQuery, [FromQuery] GamesQueryParams gamesQueryParams) { try { PagedResponse <Game> response = _gamesServices.GetGames(paginationQuery, gamesQueryParams); List <Game> games = (List <Game>)response.Data; List <GameDto> result = new List <GameDto>(); foreach (Game game in games) { string imagePath = String.Format("{0}://{1}{2}/Images/{3}", Request.Scheme, Request.Host, Request.PathBase, game.ImageName); GameDto dto = _mapper.Map <GameDto>(game, opt => { opt.Items["ImagePath"] = imagePath; }); result.Add(dto); } PagedResponse <GameDto> res = new PagedResponse <GameDto>(response.PageNumber, response.PageSize, response.NextPage, response.PreviousPage, response.NumberOfPages); res.Data = result; return(Ok(res)); } catch (Exception e) { if (e.GetType().IsAssignableFrom(typeof(InvalidGamesQueryParamsException))) { return(BadRequest(e.Message)); } return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } }