public async Task <IHttpActionResult> GetSites(
            int customerId,
            [FromUri] SiteSearchDto request
            )
        {
            var responseModel = await sitesHelper.GetSites(customerId, request);

            return(Ok(responseModel));
        }
        public async Task GetSites_GetSitesForOrganization_ReturnsCorrectSitesList()
        {
            // Arrange
            const int testCustomerId     = 3001;
            const int expectedSitesCount = 2;

            var testOrganization = new Organization
            {
                Id                 = Guid.NewGuid(),
                CustomerId         = testCustomerId,
                ChildOrganizations = new List <Organization>(),
                Sites              = new List <Site>()
            };
            var site1 = new Site
            {
                Id         = Guid.NewGuid(),
                CustomerId = 3000
            };
            var site2 = new Site
            {
                Id                   = Guid.NewGuid(),
                CustomerId           = testCustomerId,
                ParentOrganizationId = testOrganization.Id
            };
            var site3 = new Site
            {
                Id                   = Guid.NewGuid(),
                CustomerId           = testCustomerId,
                ParentOrganizationId = testOrganization.Id
            };
            var searchDto = new SiteSearchDto
            {
                OrganizationId = testOrganization.Id
            };

            testOrganization.Sites.Add(site2);
            testOrganization.Sites.Add(site3);

            this.siteRepository.Refresh(new List <Site> {
                site1, site2, site3
            });
            this.organizationRepository.Refresh(new List <Organization> {
                testOrganization
            });

            // Act
            var actual = await this.sut.GetSites(testCustomerId, searchDto);

            // Assert
            Assert.AreEqual(expectedSitesCount, actual.Results.Count);
            Assert.IsTrue(actual.Results.All(s => s.Id == site2.Id || s.Id == site3.Id));
        }
        /// <summary>
        /// Gets the sites.
        /// </summary>
        /// <param name="customerId">The customer identifier.</param>
        /// <param name="request">The request.</param>
        /// <param name="bearerToken">The bearer token.</param>
        /// <returns></returns>
        public async Task <IList <SiteResponseDto> > GetSites(int customerId, SiteSearchDto request, string bearerToken)
        {
            var requestUrl = string.Format("api/{0}/sites", customerId);

            var pagedResult = await _apiClient
                              .SendRequestAsync <PagedResult <SiteResponseDto> >(
                requestUrl,
                request,
                Method.GET,
                null,
                bearerToken
                );

            return(pagedResult.Results);
        }
Beispiel #4
0
        /// <summary>
        /// Gets all sites.
        /// </summary>
        /// <param name="customerId">The customer identifier.</param>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <PagedResultDto <SiteResponseDto> > GetSites(int customerId, SiteSearchDto request = null)
        {
            var result = await sitesService.GetSites(customerId, request);

            return(Mapper.Map <PagedResultDto <SiteResponseDto> >(result));
        }
        public async Task GetSites_GetSitesForOrganisationWithSubOrganizations_ReturnsCorrectSitesList()
        {
            // Arrange
            const int testCustomerId     = 3001;
            const int expectedSitesCount = 2;

            var testSubOrganuzation = new Organization
            {
                Id                 = Guid.NewGuid(),
                CustomerId         = testCustomerId,
                ChildOrganizations = new List <Organization>(),
                Sites              = new List <Site>()
            };
            var testOrganization = new Organization
            {
                Id                 = Guid.NewGuid(),
                CustomerId         = testCustomerId,
                ChildOrganizations = new List <Organization>
                {
                    testSubOrganuzation
                },
                Sites = new List <Site>()
            };
            var testSite = new Site
            {
                Id                   = Guid.NewGuid(),
                CustomerId           = testCustomerId,
                ParentOrganizationId = Guid.NewGuid()
            };
            var orgSite = new Site
            {
                Id                   = Guid.NewGuid(),
                CustomerId           = testCustomerId,
                ParentOrganizationId = testOrganization.Id
            };
            var subOrgSite = new Site
            {
                Id                   = Guid.NewGuid(),
                CustomerId           = testCustomerId,
                ParentOrganizationId = testOrganization.Id
            };
            var searchDto = new SiteSearchDto
            {
                OrganizationId = testOrganization.Id
            };

            testOrganization.Sites.Add(orgSite);
            testSubOrganuzation.Sites.Add(subOrgSite);

            this.siteRepository.Refresh(new List <Site> {
                orgSite, subOrgSite, testSite
            });
            this.organizationRepository.Refresh(new List <Organization> {
                testOrganization
            });

            // Act
            var actual = await this.sut.GetSites(testCustomerId, searchDto);

            // Assert
            Assert.AreEqual(expectedSitesCount, actual.Results.Count);
            Assert.IsTrue(actual.Results.Any(s => s.Id == subOrgSite.Id));
        }
 /// <summary>
 /// Gets the sites.
 /// </summary>
 /// <param name="customerId">The customer identifier.</param>
 /// <param name="request">The request.</param>
 /// <param name="bearerToken">The bearer token.</param>
 /// <returns></returns>
 public Task <IList <SiteResponseDto> > GetSites(int customerId, SiteSearchDto request, string bearerToken)
 {
     return(customersDataProvider.GetSites(customerId, request, bearerToken));
 }
Beispiel #7
0
        /// <summary>
        /// Gets all sites.
        /// </summary>
        /// <param name="customerId"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <PagedResult <Site> > GetSites(int customerId, SiteSearchDto request = null)
        {
            Expression <Func <Site, bool> > expression = s => s.CustomerId == customerId;

            if (request != null)
            {
                if (request.OrganizationId.HasValue)
                {
                    // Retrieving all customer's organizations to skip additional calls
                    // to the database during building tree
                    var customerOrganizations = await organizationRepository
                                                .FindAsync(
                        c => c.CustomerId == customerId,
                        null,
                        new List <Expression <Func <Organization, object> > >
                    {
                        o => o.Sites,
                        o => o.ParentOrganization,
                        o => o.ChildOrganizations
                    }
                        );

                    var  result = new List <Site>();
                    long total  = 0;

                    var targetOrganization =
                        customerOrganizations.FirstOrDefault(o => o.Id == request.OrganizationId.Value);

                    if (targetOrganization != null)
                    {
                        var allOrganizations = GetAllSubOrganizations(targetOrganization);

                        foreach (var organization in allOrganizations)
                        {
                            result.AddRange(organization.Sites);
                        }

                        if (!string.IsNullOrEmpty(request.Q))
                        {
                            var terms = request.Q.Split(' ').Where(r => !string.IsNullOrWhiteSpace(r));

                            foreach (var term in terms)
                            {
                                expression = expression.And(s => s.Name.Contains(term));
                            }
                        }

                        if (!request.IncludeArchived)
                        {
                            expression = expression.And(s => !s.IsDeleted);
                        }

                        result = result
                                 .Where(expression.Compile())
                                 .ToList();

                        total = result.LongCount();

                        result = result
                                 .OrderBy(s => s.Name)
                                 .Skip(request.Skip)
                                 .Take(request.Take)
                                 .ToList();
                    }

                    return(new PagedResult <Site>()
                    {
                        Results = result,
                        Total = total
                    });
                }

                if (!string.IsNullOrEmpty(request.Q))
                {
                    var terms = request.Q.Split(' ').Where(r => !string.IsNullOrWhiteSpace(r));

                    foreach (var term in terms)
                    {
                        expression = expression.And(s => s.Name.Contains(term));
                    }
                }

                if (!request.IncludeArchived)
                {
                    expression = expression.And(s => !s.IsDeleted);
                }
            }

            return(await siteRepository
                   .FindPagedAsync(
                       expression,
                       o => o.OrderBy(e => e.Name),
                       SiteIncludes,
                       request != null?request.Skip : (int?)null,
                       request != null?request.Take : (int?)null
                       ));
        }