Ejemplo n.º 1
0
        public void IsValid_ParamsValidValid_ReturnsTrue(int pageNumber, int minNumberOfGames, int numberOfResults,
                                                         string statisticsFromDate, string statisticsToDate, string formatOrName)
        {
            _dateValidatorMock
            .Setup(x => x.IsValidFormat(It.IsAny <string>(), DateFormat.yyyy_MM_dd))
            .Returns(true);
            var dto = new BanlistBrowserQueryParams()
            {
                PageNumber         = pageNumber,
                MinNumberOfGames   = minNumberOfGames,
                NumberOfResults    = numberOfResults,
                StatisticsFromDate = statisticsFromDate,
                StatisticsToDate   = statisticsToDate,
                FormatOrName       = formatOrName
            };

            Assert.IsTrue(_validator.IsValid(dto));
        }
        public async Task <IActionResult> FindAll([FromQuery] BanlistBrowserQueryParams queryParams)
        {
            if (!_validator.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 banlists = await _banlistService
                           .FindAllQuery(
                minNumberOfGames : queryParams.MinNumberOfGames,
                formatOrName : queryParams.FormatOrName,
                statisticsFromDate : statisticsFrom,
                statisticsToDate : statisticsTo
                );

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

            var banlistsToActualPage = banlists
                                       .Skip(numberOfResultsPerPage * (queryParams.PageNumber - 1))
                                       .Take(numberOfResultsPerPage)
                                       .ToList();

            var dtos = _banlistToDtoConverter
                       .Convert(banlistsToActualPage, statisticsFrom, statisticsTo);

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