Esempio n. 1
0
        public async Task <CommunityResponseModel> GetCommunities(CommunityCriteriaModel criteriaModel)
        {
            var criteria         = _modelFactory.CreateCommunityCriteria(criteriaModel);
            var communitiesCount = await _communityDao.CountCommunities(criteria).ConfigureAwait(false);

            var communityResponse = new CommunityResponse
            {
                Links = new List <Link>()
                        .AddFirstPage(communitiesCount)
                        .AddLastPage(communitiesCount, criteria.PageSize)
                        .AddNextPage(communitiesCount, criteria.PageSize, criteria.Page)
                        .AddPreviousPage(communitiesCount, criteria.Page)
            };

            if (communitiesCount > 0 && criteria.Page.IsPageInRange(communitiesCount, criteria.PageSize))
            {
                communityResponse.Results = (await _communityDao.GetCommunities(criteria).ConfigureAwait(false))
                                            .Paginate(criteria.Page, criteria.PageSize);
            }
            else
            {
                communityResponse.Results = new List <Community>();
            }


            return(_modelFactory.CreateCommunityResponseModel(communityResponse));
        }
Esempio n. 2
0
 public CommunityCriteria CreateCommunityCriteria(CommunityCriteriaModel criteriaModel)
 {
     return(new CommunityCriteria
     {
         Ids = criteriaModel.Ids?.Select(int.Parse),
         Name = criteriaModel.Name,
         Email = criteriaModel.Email,
         Page = criteriaModel.Page,
         PageSize = criteriaModel.PageSize
     });
 }
Esempio n. 3
0
        public void Should_not_return_Artifacts_When_page_do_not_exist()
        {
            _communityDao.CountCommunities(Arg.Any <CommunityCriteria>()).ReturnsForAnyArgs(50);
            _communityDao.GetCommunities(Arg.Any <CommunityCriteria>())
            .ReturnsForAnyArgs(_fixture.CreateMany <Community>(50));

            var criteria = new CommunityCriteriaModel
            {
                Page     = 10,
                PageSize = 20
            };

            var results = _communityRepo.GetCommunities(criteria).Result;

            results.Results.Should().BeEmpty();
        }
Esempio n. 4
0
        public void Should_return_5_communities_when_get_all_Communities()
        {
            var response = _fixture.Build <CommunityResponseModel>()
                           .With(x => x.Results, _fixture.Build <CommunityModel>()
                                 .CreateMany(5).ToList())
                           .Create();

            var criteria      = new CommunityCriteriaModel();
            var communityRepo = Substitute.For <ICommunityRepository>();

            communityRepo.GetCommunities(criteria).Returns(response);
            var communityService = new CommunityService(communityRepo);
            var responseModel    = communityService.GetCommunities(criteria).Result;

            responseModel.Results.FirstOrDefault().Should().BeOfType <CommunityModel>();
            responseModel.Results.Count().Should().Be(5);
        }
Esempio n. 5
0
        public void Should_not_have_validation_error_when_no_criteria()
        {
            var criteria = new CommunityCriteriaModel
            {
                Ids      = null,
                Email    = null,
                Name     = null,
                PageSize = 20,
                Page     = 1
            };

            _validator.ShouldNotHaveValidationErrorFor(x => x.Ids, criteria);
            _validator.ShouldNotHaveValidationErrorFor(x => x.Email, criteria);
            _validator.ShouldNotHaveValidationErrorFor(x => x.Name, criteria);
            _validator.ShouldNotHaveValidationErrorFor(x => x.PageSize, criteria);
            _validator.ShouldNotHaveValidationErrorFor(x => x.Page, criteria);
        }
Esempio n. 6
0
        public async Task <IHttpActionResult> GetCommunities(string ids = null, string email = null, string name = null, int page = 1, int pageSize = 50)
        {
            var criteria = new CommunityCriteriaModel
            {
                Ids      = ids?.SafeSplit(),
                Email    = email,
                Name     = name,
                Page     = page,
                PageSize = pageSize
            };

            var validationResults = _criteriaValidator.Validate(criteria);

            if (validationResults.IsValid)
            {
                return(Ok(await _communityService.GetCommunities(criteria).ConfigureAwait(false)));
            }
            validationResults.AddToModelState(ModelState, null);
            return(BadRequest(ModelState));
        }
Esempio n. 7
0
 public async Task <CommunityResponseModel> GetCommunities(CommunityCriteriaModel criteriaModel)
 {
     return(await _communityRepo.GetCommunities(criteriaModel).ConfigureAwait(false));
 }