Пример #1
0
        public async Task <IActionResult> FindAll([FromQuery] ArchetypeBrowserQueryParams queryParams)
        {
            if (!_queryParamsValidator.IsValid(queryParams))
            {
                return(BadRequest("Invalid params. Please report it to administrator."));
            }

            DateTime?statisticsFrom = null;
            DateTime?statisticsTo   = null;

            if (!string.IsNullOrEmpty(queryParams.StatisticsFromDate))
            {
                statisticsFrom = DateTime
                                 .ParseExact(
                    queryParams.StatisticsFromDate,
                    DateFormat.yyyy_MM_dd,
                    CultureInfo.InvariantCulture);
            }

            if (!string.IsNullOrEmpty(queryParams.StatisticsToDate))
            {
                statisticsTo = DateTime
                               .ParseExact(
                    queryParams.StatisticsToDate,
                    DateFormat.yyyy_MM_dd,
                    CultureInfo.InvariantCulture);
            }

            var archetypes = await _archetypeService.FindAllQuery(
                minNumberOfGames : queryParams.MinNumberOfGames,
                archetypeName : queryParams.ArchetypeName,
                statisticsFromDate : statisticsFrom,
                statisticsToDate : statisticsTo,
                includeCards : false,
                includeDecks : false,
                OrderByDescendingByNumberOfGames : queryParams.OrderByDescendingByNumberOfGames
                );

            int numberOfResultsPerPage = _numberOfResultsHelper.GetNumberOfResultsPerPage(queryParams.NumberOfResults);
            var numberOfPages          = Convert.ToInt32(
                Math.Ceiling(
                    ((double)(archetypes.Count()) / (double)(numberOfResultsPerPage))
                    )
                );

            var archetypesToActualPage = archetypes
                                         .Skip(numberOfResultsPerPage * (queryParams.PageNumber - 1))
                                         .Take(numberOfResultsPerPage)
                                         .ToList();

            var dtos = _archetypeToDtoConverter.Convert(archetypesToActualPage, statisticsFrom, statisticsTo);

            return(Ok(new ArchetypeBrowserResultsDTO(numberOfPages, dtos)));
        }