예제 #1
0
        public Envelope <NewsItemDto> GetAllNewsItems(int pageSize, int pageNumber)
        {
            var newsItems = _newsItemRepository.GetAllNewsItems(pageSize, pageNumber);
            var newsList  = newsItems.ToList();

            var skip     = (pageNumber * pageSize) - pageSize;
            var items    = newsList.Skip(skip).Take(pageSize);
            var maxPages = (int)(Math.Ceiling(newsItems.Count() / (decimal)pageSize));

            foreach (NewsItemDto item in items)
            {
                int itemId     = item.Id;
                int authorId   = _newsItemRepository.GetLinkedAuthorId(item.Id);
                int categoryId = _newsItemRepository.GetLinkedCategoryId(item.Id);
                Dictionary <string, string> dict = new Dictionary <string, string>();
                dict.Add("href", $"api/{itemId}");
                Dictionary <string, string> authorDict = new Dictionary <string, string>();
                authorDict.Add("href", $"api/authors/{authorId}");
                Dictionary <string, string> categoryDict = new Dictionary <string, string>();
                categoryDict.Add("href", $"api/categories/{categoryId}");

                item.Links.AddReference("self", dict);
                item.Links.AddReference("edit", dict);
                item.Links.AddReference("delete", dict);
                item.Links.AddReference("authors", authorDict);
                item.Links.AddReference("categories", categoryDict);

                dict = null;
            }

            Envelope <NewsItemDto> envelope = new Envelope <NewsItemDto>();

            envelope.PageNumber = pageNumber;
            envelope.PageSize   = pageSize;
            envelope.Items      = items;
            envelope.maxPages   = maxPages;

            return(envelope);
        }
예제 #2
0
        public IEnumerable <NewsItemDto> GetNewsItemsByAuthorId(int id)
        {
            var items = _authorRepository.GetNewsItemsByAuthorId(id);

            foreach (NewsItemDto item in items)
            {
                int itemId     = item.Id;
                int authorId   = _newsItemRepository.GetLinkedAuthorId(item.Id);
                int categoryId = _newsItemRepository.GetLinkedCategoryId(item.Id);
                item.Links.AddReference("self", $"http://localhost:5000/api/{itemId}");
                item.Links.AddReference("edit", $"http://localhost:5000/api/{itemId}");
                item.Links.AddReference("delete", $"http://localhost:5000/api/{itemId}");
                item.Links.AddReference("authors", $"http://localhost:5000/api/authors/{id}");
                item.Links.AddReference("categories", $"http://localhost:5000/api/categories/{categoryId}");
            }

            return(items);
        }