Ejemplo n.º 1
0
        public IActionResult GetPosts(int pageNumber = 1, int pageSize = 5)
        {
            pageSize = pageSize > maxPageSize ? maxPageSize : pageSize;

            var data = _dataService.GetPosts(pageNumber, pageSize);

            var result = Mapper.Map <IEnumerable <PostListModel> >(data);

            foreach (PostListModel p in result)
            {
                p.Url = Url.Link(nameof(GetPost), new{ p.Id });
            }

            var prevlink = pageNumber > 1
                ? Url.Link(nameof(GetPosts), new { pageNumber = pageNumber - 1, pageSize })
                : null;

            var total = _dataService.GetNumberOfPost();

            var totalPages = (int)System.Math.Ceiling(total / (double)pageSize);

            var nextlink = pageNumber < totalPages
                ? Url.Link(nameof(GetPosts), new { pageNumber = pageNumber + 1, pageSize })
                : null;

            var curlink = Url.Link(nameof(GetPosts), new { pageNumber, pageSize });

            var linkedResult = new
            {
                Result = result,
                Links  = new List <object>
                {
                    new { name = "prev", url = prevlink },
                    new { name = "next", url = nextlink },
                    new { name = "cur", url = curlink }
                },
                TotalPosts = total
            };

            return(Ok(linkedResult));
        }
 public IEnumerable <Post> Posts(Pagging pagging)
 {
     return(_postDataService.GetPosts(pagging));
 }