Ejemplo n.º 1
0
        public async Task <IHttpActionResult> SearchContestants(int warId, Models.ContestantSearchRequest request)
        {
            if (request == null)
            {
                return(BadRequest());
            }

            if (!await IsValidWarId(warId))
            {
                return(NotFound());
            }

            var contestants = await _rankingService.GetRankings(warId);

            var orderedContestants   = request.OrderByScoreDesc ? contestants.OrderByDescending(c => c.Score) : contestants.OrderBy(c => c.Score);
            var paginatedContestants = orderedContestants.Skip(request.Skip).Take(request.Take);
            var contestantModels     = paginatedContestants.Select(c => _mapper.Map <ContestantWithScore, Models.ContestantWithScore>(c));
            var result = new Models.ContestantSearchResults
            {
                Content = contestantModels,
                Count   = orderedContestants.Count(),
                Take    = request.Take,
                Skip    = request.Skip
            };

            return(Ok(result));
        }
Ejemplo n.º 2
0
        public async Task GivenInvalidWarId_SearchContestants_ReturnsNotFound()
        {
            // Arrange
            var request = new Models.ContestantSearchRequest();

            // Act
            var result = await _controller.SearchContestants(InvalidWarId, request);

            // Assert
            result.Should().BeOfType <NotFoundResult>();
        }
Ejemplo n.º 3
0
        public async Task GivenValidWarId_SearchContestants_ReturnsRankings()
        {
            // Arrange
            var contestant1 = new ContestantWithScore {
                Score = 1
            };
            var contestant2 = new ContestantWithScore {
                Score = 2
            };
            var contestant3 = new ContestantWithScore {
                Score = 3
            };
            var contestant4 = new ContestantWithScore {
                Score = 4
            };
            var contestantRankingModel1 = new Models.ContestantWithScore();
            var contestantRankingModel2 = new Models.ContestantWithScore();
            var contestants             = new[] { contestant1, contestant2, contestant3, contestant4 };
            var contestantRankingModels = new List <Models.ContestantWithScore> {
                contestantRankingModel2, contestantRankingModel1
            };

            _stubRankingService.Setup(x => x.GetRankings(ValidWarId)).Returns(Task.FromResult((IEnumerable <ContestantWithScore>)contestants));
            _stubMapper.Setup(x => x.Map <ContestantWithScore, Models.ContestantWithScore>(contestant2)).Returns(contestantRankingModel1);
            _stubMapper.Setup(x => x.Map <ContestantWithScore, Models.ContestantWithScore>(contestant3)).Returns(contestantRankingModel2);
            var request = new Models.ContestantSearchRequest
            {
                OrderByScoreDesc = true,
                Take             = 2,
                Skip             = 1
            };

            // Act
            var result = await _controller.SearchContestants(ValidWarId, request);

            // Assert
            result.Should().BeOfType <OkNegotiatedContentResult <Models.ContestantSearchResults> >();
            var response = (OkNegotiatedContentResult <Models.ContestantSearchResults>)result;

            response.Content.Content.Should().ContainInOrder(contestantRankingModels);
            response.Content.Content.Should().OnlyContain(x => contestantRankingModels.Contains(x));
            response.Content.Count.Should().Be(contestants.Length);
            response.Content.Take.Should().Be(request.Take);
            response.Content.Skip.Should().Be(request.Skip);
        }