Exemple #1
0
 string GenerateTouristRouteResourceURL(
     TouristRouteResourceParameters parameters,
     PaginationResourceParameter paginationParameters,
     ResourceUriType type
     )
 {
     return(type switch
     {
         ResourceUriType.PreviousPage => _urlHelper.Link("GetTouristRoutes",
                                                         new
         {
             keyword = parameters.Keyword,
             rating = parameters.Rating,
             pageNumber = paginationParameters.PageNumber - 1,
             pageSize = paginationParameters.PageSize
         }),
         ResourceUriType.NextPage => _urlHelper.Link("GetTouristRoutes",
                                                     new
         {
             keyword = parameters.Keyword,
             rating = parameters.Rating,
             pageNumber = paginationParameters.PageNumber + 1,
             pageSize = paginationParameters.PageSize
         }),
         _ => _urlHelper.Link("GetTouristRoutes",
                              new
         {
             keyword = parameters.Keyword,
             rating = parameters.Rating,
             pageNumber = paginationParameters.PageNumber,
             pageSize = paginationParameters.PageSize
         })
     });
        private IEnumerable <LinkDto> CreateLinksForTouristRouteList(
            TouristRouteResourceParameters touristRouteResourceParameters,
            PaginationResourceParameters paginationResourceParameters)
        {
            var links = new List <LinkDto>();

            links.Add(
                new LinkDto(
                    GenerateTouristRouteResourceUrl(
                        touristRouteResourceParameters,
                        paginationResourceParameters,
                        ResourceUriType.CurrentPage),
                    "self",
                    "GET"
                    )
                );

            links.Add(new LinkDto(

                          Url.Link("CreateTouristRoute", null),
                          "create_tourist_route",
                          "POST"
                          ));


            return(links);
        }
Exemple #3
0
 private string GenerateTouristRouteResourceUrl(TouristRouteResourceParameters touristRouteResourceParameters,
                                                PaginationResourceParameters paginationResourceParameters, ResourceUrlType resourceUrlType)
 {
     return(resourceUrlType switch
     {
         ResourceUrlType.PreviousPage => _urlHelper.Link("GetTouristRoutes", new
         {
             orderBy = touristRouteResourceParameters.OrderBy,
             keyword = touristRouteResourceParameters.Keyword,
             rating = touristRouteResourceParameters.Rating,
             fields = touristRouteResourceParameters.Fields,
             pageNumber = paginationResourceParameters.PageNumber - 1,
             pageSize = paginationResourceParameters.PageSize
         }),
         ResourceUrlType.NextPage => _urlHelper.Link("GetTouristRoutes", new
         {
             orderBy = touristRouteResourceParameters.OrderBy,
             keyword = touristRouteResourceParameters.Keyword,
             rating = touristRouteResourceParameters.Rating,
             fields = touristRouteResourceParameters.Fields,
             pageNumber = paginationResourceParameters.PageNumber + 1,
             pageSize = paginationResourceParameters.PageSize
         }),
         _ => _urlHelper.Link("GetTouristRoutes", new
         {
             orderBy = touristRouteResourceParameters.OrderBy,
             keyword = touristRouteResourceParameters.Keyword,
             rating = touristRouteResourceParameters.Rating,
             fields = touristRouteResourceParameters.Fields,
             pageNumber = paginationResourceParameters.PageNumber,
             pageSize = paginationResourceParameters.PageSize
         })
     });
        public IActionResult GetTouristRoutes(
            [FromQuery] TouristRouteResourceParameters parameters
            //[FromQuery] string keyword,
            //string rating // 小于,大于,等于
            ) // FromQuery vs FromBody
        {
            /*
             * [ApiController] 里面有attribute, 所以上面函数参数里面的 [FromQuery]是可以省略的, ASP会自动帮我们绑定URL中的参数,
             * 但是为了让代码更有逻辑性,还是不要省略这个attribute。 而且FromQuery还有另一个用处,就是如果url的参数命名与action函数
             * 参数的名称不一致,一定要使用FromQuery的name属性匹配一下, [FromQuery(name="")]。
             * 在这个项目中,参数的名称是一致的,所以不需要做这一步。
             * 写上可以使得更清晰
             * **/

            var touristRoutesFromRepo = _touristRouteRepository.GetTouristRoutes(parameters.Keyword, parameters.RatingOperator, parameters.RatingValue);

            if (touristRoutesFromRepo == null || touristRoutesFromRepo.Count() <= 0)
            {
                return(NotFound("没有旅游路线"));
            }

            // 使用IEnumerable进行列表的映射
            var touristRouteDto = _mapper.Map <IEnumerable <TouristRouteDto> >(touristRoutesFromRepo);

            return(Ok(touristRouteDto));
        }
Exemple #5
0
        public async Task <IActionResult> GetTouristRoutesAsync([FromQuery] TouristRouteResourceParameters parameters)
        {
            var touristRoutesFromRepo = await _touristRouteRepository
                                        .GetTouristRoutesAsync(parameters.Keyword, parameters.OperatorType, parameters.RatingValue);

            if (touristRoutesFromRepo == null || touristRoutesFromRepo.Count() == 0)
            {
                return(NotFound("沒有旅遊路線"));
            }

            var touristRouteDto = _mapper.Map <IEnumerable <TouristRouteDto> >(touristRoutesFromRepo);

            return(Ok(touristRouteDto));
        }
Exemple #6
0
        public async Task <IActionResult> GetTouristRoutes(
            [FromQuery] TouristRouteResourceParameters parameters
            // [FromQuery] string keyword,
            // string rating //lessThan, largerThan, equalTo
            )
        {
            var touristRouteFromRepo = await _touristRouteRepository.GetTouristRoutesAsync(parameters.Keyword, parameters.RatingOperator, parameters.RatingValue);

            if (touristRouteFromRepo == null || touristRouteFromRepo.Count() <= 0)
            {
                return(NotFound("没有旅游路线"));
            }
            var touristRoutesDto = _mapper.Map <IEnumerable <TouristRouteDto> >(touristRouteFromRepo);

            return(Ok(touristRoutesDto));
        }
        public async Task <IActionResult> GetTouristRoutesAsync(
            [FromQuery] TouristRouteResourceParameters parameters
            //[FromQuery] string keyword,
            //string rating // 小于lessThan 大于largeThan 等于equalTo
            )
        {
            var touristRouteFromRepo = await touristRouteRepository.GetTouristRoutesAsync(parameters.keyword, parameters.RatingOperator, parameters.RatingValue);

            if (touristRouteFromRepo == null)
            {
                return(NotFound("目前暂停所有旅游路线"));
            }
            var touristRouteDto = mapper.Map <IEnumerable <TouristRouteDto> >(touristRouteFromRepo);

            return(Ok(touristRouteDto));
        }
Exemple #8
0
        public IActionResult GerTouristRoutes(
            [FromQuery] TouristRouteResourceParameters parameters
            //[FromQuery] string keyword,
            //string rating
            )
        {
            var touristRouteFromRep = _touristRouteRepository.GetTouristRoutes(parameters.Keyword, parameters.operatorType, parameters.raringVlaue);

            if (touristRouteFromRep == null || touristRouteFromRep.Count() < 0)
            {
                return(NotFound("没有旅游路线"));
            }

            var touristRoutesDto = _mapper.Map <IEnumerable <TouristRouteDto> >(touristRouteFromRep);

            return(Ok(touristRoutesDto));
        }  //GetTouristRoute()
Exemple #9
0
 private string GenerateTouristRouteResourceURL(
     TouristRouteResourceParameters parameters,
     PaginationResourceParameters parameters2,
     ResourceUriType type
     )
 {
     return(type switch
     {
         ResourceUriType.PreviousPage => urlHelper.Link("GetTouristRoutesAsync",
                                                        new
         {
             fields = parameters.Fields,
             orderBy = parameters.OrderBy,
             keyword = parameters.Key,
             rating = parameters.Rating,
             pageNumber = parameters2.PageNumber - 1,
             pageSize = parameters2.PageSize
         }),
         ResourceUriType.NextPage => urlHelper.Link("GetTouristRoutesAsync",
                                                    new
         {
             fields = parameters.Fields,
             orderBy = parameters.OrderBy,
             keyword = parameters.Key,
             rating = parameters.Rating,
             pageNumber = parameters2.PageNumber + 1,
             pageSize = parameters2.PageSize
         }),
         _ => urlHelper.Link("GetTouristRoutesAsync",
                             new
         {
             fields = parameters.Fields,
             orderBy = parameters.OrderBy,
             keyword = parameters.Key,
             rating = parameters.Rating,
             pageNumber = parameters2.PageNumber,
             pageSize = parameters2.PageSize
         })
     });
        [HttpHead]// only return header info not the body
        public async Task <IActionResult> GetRouristRoutes(
            [FromQuery] TouristRouteResourceParameters parameters,
            [FromQuery] PaginationResourceParameters paginationResourceParameters,
            [FromHeader(Name = "Accept")] string mediaType
            //[FromQuery] string keyword,
            //string rating
            )
        {
            if (!MediaTypeHeaderValue.TryParse(mediaType, out MediaTypeHeaderValue parsedMediaType))
            {
                return(BadRequest($"http header: accept 不正确"));
            }

            if (!_propertyMappingService
                .IsMappingExists <TouristRouteDto, TouristRoute>
                    (parameters.OrderBy))
            {
                return(BadRequest($"请输入正确的排序参数"));
            }

            if (!_propertyMappingService.IsPropertyExists <TouristRouteDto>(parameters.Fields))
            {
                return(BadRequest($"请输入正确的塑形参数"));
            }

            var touristRoutesFromRepo = await _touristRouteRepository
                                        .GetTouristRoutesAsync(
                parameters.Keyword,
                parameters.RatingOperator,
                parameters.RatingValue,
                paginationResourceParameters.PageSize,
                paginationResourceParameters.PageNumber,
                parameters.OrderBy);

            if (touristRoutesFromRepo == null ||
                touristRoutesFromRepo.Count() <= 0)
            {
                return(NotFound("没有旅游路线"));
            }

            var previousPageLink = touristRoutesFromRepo.HasPrevious ?
                                   GenerateTouristRouteResourceUrl(
                parameters,
                paginationResourceParameters,
                ResourceUriType.PreviousPage
                ) : null;

            var nextPageLink = touristRoutesFromRepo.HasNext ?
                               GenerateTouristRouteResourceUrl(
                parameters,
                paginationResourceParameters,
                ResourceUriType.NextPage
                ) : null;


            var paginationMetadata = new
            {
                previousPageLink,
                nextPageLink,
                totalCount  = touristRoutesFromRepo.TotalCount,
                pageSize    = touristRoutesFromRepo.PageSize,
                currentPage = touristRoutesFromRepo.CurrentPage,
                totalPages  = touristRoutesFromRepo.TotalPages
            };

            Response.Headers.Add("x-pagination", JsonConvert.SerializeObject(paginationMetadata));

            bool isHateoas = parsedMediaType.SubTypeWithoutSuffix.EndsWith("hateoas", StringComparison.InvariantCultureIgnoreCase);

            var primaryMediaType = isHateoas
                ? parsedMediaType.SubTypeWithoutSuffix.Substring(0, parsedMediaType.SubTypeWithoutSuffix.Length - 8)
                : parsedMediaType.SubTypeWithoutSuffix;
            //var touristRoutesDto = _mapper.Map<IEnumerable<TouristRouteDto>>(touristRoutesFromRepo);
            //var shapedDtoList = touristRoutesDto.ShapeData(parameters.Fields);

            IEnumerable <object>        touristRoutesDto;
            IEnumerable <ExpandoObject> shapedDtoList;

            if (primaryMediaType == "vnd.ivan.touristRoute.simplify")
            {
                touristRoutesDto = _mapper.Map <IEnumerable <TouristRouteSimplifyDto> >(touristRoutesFromRepo);

                shapedDtoList = ((IEnumerable <TouristRouteSimplifyDto>)touristRoutesDto).ShapeData(parameters.Fields);
            }
            else
            {
                touristRoutesDto = _mapper.Map <IEnumerable <TouristRouteDto> >(touristRoutesFromRepo);
                shapedDtoList    = ((IEnumerable <TouristRouteDto>)touristRoutesDto).ShapeData(parameters.Fields);
            }

            if (isHateoas)
            {
                var linkDto = CreateLinksForTouristRouteList(parameters, paginationResourceParameters);

                var shapedDtoWithLinkedList = shapedDtoList.Select(t =>
                {
                    var touristDictionary = t as IDictionary <string, object>;

                    var links = CreateLinkForTouristRoute(
                        (Guid)touristDictionary["Id"],
                        null);
                    touristDictionary.Add("links", links);
                    return(touristDictionary);
                });

                var result = new
                {
                    value = shapedDtoWithLinkedList,
                    links = linkDto
                };
                return(Ok(result));
            }

            return(Ok(shapedDtoList));
        }