public async Task <RepositoryResult <SearchProjectsD365PageModel> > SearchProject(string search,
                                                                                          ProjectStatusEnum status,
                                                                                          bool isAscending = true,
                                                                                          uint pageSize    = 10,
                                                                                          uint pageNumber  = 1)
        {
            var sanitizedSearch = _fetchXmlSanitizer.Sanitize(search);

            var fetchXml        = BuildFetchXMLQuery(sanitizedSearch, status, isAscending);
            var encodedFetchXml = WebUtility.UrlEncode(fetchXml);

            var url = $"{_route}?fetchXml={encodedFetchXml}";

            var response = await _client.GetAsync(url);

            var responseContent = await response.Content?.ReadAsStringAsync();

            var responseStatusCode = response.StatusCode;

            if (response.IsSuccessStatusCode)
            {
                var results = await response.Content.ReadAsStringAsync();

                var castedResults = JsonConvert.DeserializeObject <ResultSet <SearchProjectsD365Model> >(results);

                var distinctResults = castedResults.Items.Distinct().ToList();
                var totalPages      = distinctResults.Count == 0 ? 0 : (distinctResults.Count / (int)pageSize) + 1;
                var pageResults     = distinctResults
                                      .Skip(((int)pageNumber - 1) * (int)pageSize)
                                      .Take((int)pageSize)
                                      .ToList();

                var pageResult = new SearchProjectsD365PageModel
                {
                    CurrentPage = (int)pageNumber,
                    TotalPages  = totalPages,
                    Projects    = pageResults
                };

                return(new RepositoryResult <SearchProjectsD365PageModel> {
                    Result = pageResult
                });
            }

            //At this point, log the error and configure the repository result to inform the caller that the repo failed
            _logger.LogError(RepositoryErrorMessages.RepositoryErrorLogFormat, responseStatusCode, responseContent);

            return(new RepositoryResult <SearchProjectsD365PageModel>
            {
                Error = new RepositoryResultBase.RepositoryError
                {
                    StatusCode = responseStatusCode,
                    ErrorMessage = responseContent
                }
            });
        }
Ejemplo n.º 2
0
        public void CurrentPage_MapTest(int input, int expected)
        {
            var inputPageModel = new SearchProjectsD365PageModel
            {
                CurrentPage = input
            };

            var result = _mapper.Map(inputPageModel);

            Assert.Equal(expected, result.CurrentPage);
        }
Ejemplo n.º 3
0
        public void TotalPages_MapTest(int input, int expected)
        {
            var inputPageModel = new SearchProjectsD365PageModel
            {
                TotalPages = input
            };

            var result = _mapper.Map(inputPageModel);

            Assert.Equal(expected, result.TotalPages);
        }
Ejemplo n.º 4
0
        public void ItemMapper_CallTest()
        {
            var itemMapper = new Mock <IMapper <SearchProjectsD365Model, SearchProjectsModel> >();

            itemMapper.Setup(m => m.Map(It.IsAny <SearchProjectsD365Model>()))
            .Verifiable();

            var pageMapper = new SearchProjectsPageResponseMapper(itemMapper.Object);

            var inputModel = new SearchProjectsD365PageModel
            {
                Projects = new List <SearchProjectsD365Model>
                {
                    new SearchProjectsD365Model(),
                    new SearchProjectsD365Model(),
                    new SearchProjectsD365Model(),
                    new SearchProjectsD365Model()
                }
            };

            var result = pageMapper.Map(inputModel);

            itemMapper.Verify(m => m.Map(It.IsAny <SearchProjectsD365Model>()), Times.Exactly(4));
        }
Ejemplo n.º 5
0
        public async Task <RepositoryResult <SearchProjectsPageModel> > SearchProject(string search,
                                                                                      Models.Upstream.Enums.ProjectStatusEnum?status,
                                                                                      bool isAscending = true,
                                                                                      uint pageSize    = 10,
                                                                                      uint pageNumber  = 1)
        {
            var sanitizedSearch = _fetchXmlSanitizer.Sanitize(search);

            ProjectStatusEnum projectStatus = default;

            if (status.HasValue)
            {
                if (MappingDictionaries.ProjecStatusEnumMap.TryGetValue(status.Value, out var internalStatus))
                {
                    projectStatus = internalStatus;
                }
                else
                {
                    //If project status cannot be mapped to D365 Project Status, return an error
                    return(new RepositoryResult <SearchProjectsPageModel>()
                    {
                        Error = new RepositoryResultBase.RepositoryError()
                        {
                            ErrorMessage = "Project Status not recognised",
                            StatusCode = HttpStatusCode.BadRequest
                        }
                    });
                }
            }

            var fetchXml        = BuildFetchXMLQuery(sanitizedSearch, projectStatus, isAscending);
            var encodedFetchXml = WebUtility.UrlEncode(fetchXml);

            var url = $"{Route}?fetchXml={encodedFetchXml}";

            var response = await _client.GetAsync(url);

            var responseContent = await response.Content?.ReadAsStringAsync();

            var responseStatusCode = response.StatusCode;

            if (response.IsSuccessStatusCode)
            {
                var results = await response.Content.ReadAsStringAsync();

                var castedResults = JsonConvert.DeserializeObject <ResultSet <SearchProjectsD365Model> >(results);

                var distinctResults = castedResults.Items.Distinct().ToList();
                var totalPages      = distinctResults.Count == 0 ? 0 : (distinctResults.Count / (int)pageSize) + 1;
                var pageResults     = distinctResults
                                      .Skip(((int)pageNumber - 1) * (int)pageSize)
                                      .Take((int)pageSize)
                                      .ToList();

                var pageResult = new SearchProjectsD365PageModel
                {
                    CurrentPage = (int)pageNumber,
                    TotalPages  = totalPages,
                    Projects    = pageResults
                };

                var mappedPageResult = _searchProjectsMapper.Map(pageResult);
                return(new RepositoryResult <SearchProjectsPageModel> {
                    Result = mappedPageResult
                });
            }

            //At this point, log the error and configure the repository result to inform the caller that the repo failed
            _logger.LogError(RepositoryErrorMessages.RepositoryErrorLogFormat, responseStatusCode, responseContent);

            return(new RepositoryResult <SearchProjectsPageModel>
            {
                Error = new RepositoryResultBase.RepositoryError
                {
                    StatusCode = responseStatusCode,
                    ErrorMessage = responseContent
                }
            });
        }