public async Task <PagedList <Blogs> > GetBlogsAsync(DtoBlogParameter parameter, bool eager)
        {
            IQueryable <Blogs> queryExpression;

            //是否预加载
            if (eager)
            {
                queryExpression = _context.Set <Blogs>().Include(c => c.Menu).Include(d => d.User);
            }
            else
            {
                queryExpression = _context.Set <Blogs>();
            }

            if (!string.IsNullOrWhiteSpace(parameter.Title))
            {
                queryExpression = queryExpression.Where(c => c.Title.Contains(parameter.Title));
            }
            if (!string.IsNullOrWhiteSpace(parameter.Abstract))
            {
                queryExpression = queryExpression.Where(c => c.Abstract.Contains(parameter.Abstract));
            }
            if (parameter.UserId != 0)
            {
                queryExpression = queryExpression.Where(c => c.UserId == parameter.UserId);
            }
            if (parameter.MenuId != 0)
            {
                queryExpression = queryExpression.Where(c => c.MenuId == parameter.MenuId);
            }
            if (parameter.StartTime != null && parameter.EndTime != null)
            {
                queryExpression = queryExpression.Where(c => c.CreateTime >= parameter.StartTime && c.CreateTime <= parameter.EndTime);
            }


            var mappingDictionary = _propertyMappingService.GetPropertyMapping <BlogDto, Blogs>();

            queryExpression = queryExpression.ApplySort(parameter.OrderBy, mappingDictionary);
            return(await PagedList <Blogs> .CreateAsync(queryExpression, parameter.PageNumber, parameter.PageSize));
        }
Beispiel #2
0
        private string CreateBlogsResourceUri(DtoBlogParameter parameters,
                                              ResourceUriType type)
        {
            switch (type)
            {
            case ResourceUriType.PreviousPage:
                return(Url.Link(nameof(GetBlogs), new
                {
                    fields = parameters.Fields,
                    pageNumber = parameters.PageNumber - 1,
                    pageSize = parameters.PageSize,
                    title = parameters.Title,
                    summary = parameters.Abstract,
                    orderBy = parameters.OrderBy
                }));

            case ResourceUriType.NextPage:

                return(Url.Link(nameof(GetBlogs), new
                {
                    fields = parameters.Fields,
                    pageNumber = parameters.PageNumber + 1,
                    pageSize = parameters.PageSize,
                    title = parameters.Title,
                    summary = parameters.Abstract,
                    orderBy = parameters.OrderBy
                }));

            case ResourceUriType.CurrentPage:
            default:
                return(Url.Link(nameof(GetBlogs), new
                {
                    fields = parameters.Fields,
                    pageNumber = parameters.PageNumber,
                    pageSize = parameters.PageSize,
                    title = parameters.Title,
                    summary = parameters.Abstract,
                    orderBy = parameters.OrderBy
                }));
            }
        }
Beispiel #3
0
        private IEnumerable <LinkDto> CreateLinksForMenus(DtoBlogParameter parameters, bool hasPrevious, bool hasNext)
        {
            var links = new List <LinkDto>();


            links.Add(new LinkDto(CreateBlogsResourceUri(parameters, ResourceUriType.CurrentPage),
                                  "self", "GET"));

            if (hasPrevious)
            {
                links.Add(new LinkDto(CreateBlogsResourceUri(parameters, ResourceUriType.PreviousPage),
                                      "previous_page", "GET"));
            }

            if (hasNext)
            {
                links.Add(new LinkDto(CreateBlogsResourceUri(parameters, ResourceUriType.NextPage),
                                      "next_page", "GET"));
            }

            return(links);
        }
Beispiel #4
0
        public async Task <IActionResult> GetBlogs([FromQuery] DtoBlogParameter parameter, [FromHeader(Name = "Accept")] string mediaType)
        {
            if (!MediaTypeHeaderValue.TryParse(mediaType, out MediaTypeHeaderValue parsedMediaType))
            {
                return(BadRequest());
            }
            if (!_propertyCheckerService.TypeHasProperties <BlogDto>(parameter.Fields))
            {
                return(BadRequest());
            }
            if (!_propertyMappingService.ValidMappingExistsFor <BlogDto, Blogs>(parameter.OrderBy))
            {
                return(BadRequest());
            }
            var includeLinks = parsedMediaType.SubTypeWithoutSuffix.
                               EndsWith("hateoas", System.StringComparison.InvariantCultureIgnoreCase);
            IEnumerable <LinkDto> myLinks = new List <LinkDto>();
            var primaryMediaType          = includeLinks
                ? parsedMediaType.SubTypeWithoutSuffix.Substring(0, parsedMediaType.SubTypeWithoutSuffix.Length - 8)
                : parsedMediaType.SubTypeWithoutSuffix;


            if (primaryMediaType == "vnd.wuther.blogs.full")
            {
                var fullBlogs = await _blogsRepository.GetBlogsAsync(parameter, true);

                var fullPaginationMetadata = new
                {
                    totalCount  = fullBlogs.TotalCount,
                    pageSize    = fullBlogs.PageSize,
                    currentPage = fullBlogs.CurrentPage,
                    totalPages  = fullBlogs.TotalPages,
                };
                Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(fullPaginationMetadata, new JsonSerializerOptions
                {
                    Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
                }));
                var blogFullDtos   = _mapper.Map <IList <BlogFullDto> >(fullBlogs);
                var shapedFullData = blogFullDtos.shapeData(parameter.Fields);
                var fullLinks      = CreateLinksForMenus(parameter, fullBlogs.HasPrevious, fullBlogs.HasNext);
                var fullLinkedCollectionResource = new
                {
                    value = shapedFullData,
                    fullLinks
                };

                return(Ok(fullLinkedCollectionResource));
            }
            var blogs = await _blogsRepository.GetBlogsAsync(parameter, false);

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

            Response.Headers.Add("X-Pagination", JsonSerializer.Serialize(paginationMetadata, new JsonSerializerOptions
            {
                Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
            }));

            var blogDtos   = _mapper.Map <IList <BlogDto> >(blogs);
            var shapedData = blogDtos.shapeData(parameter.Fields);
            var links      = CreateLinksForMenus(parameter, blogs.HasPrevious, blogs.HasNext);

            //var shapedMenusWithLinks = shapedData.Select(c =>
            //{
            //    var menuDict = c as IDictionary<string, object>;
            //    var menusLinks = CreateLinksForMenus((int)menuDict["Id"], null);
            //    menuDict.Add("links", menusLinks);
            //    return menuDict;
            //});

            var linkedCollectionResource = new
            {
                value = shapedData,
                links
            };

            return(Ok(linkedCollectionResource));
        }