예제 #1
0
        public NewsItemDetailDto GetNewsItemById(int id)
        {
            var newsItem = _newsItemRepository.GetNewsItemById(id);

            if (newsItem == null)
            {
                throw new Exception($"News Item with id {id} was not found.");
            }

            int itemId     = newsItem.Id;
            int authorId   = _newsItemRepository.GetLinkedAuthorId(id);
            int categoryId = _newsItemRepository.GetLinkedCategoryId(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}");

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

            dict = null;

            return(newsItem);
        }
예제 #2
0
        public NewsItemDetailDto GetNewsItemById(int id)
        {
            var news = _newsItemRepository.GetNewsItemById(id);

            if (news == null)
            {
                throw new ResourceNotFoundException($"News item with id {id} was not found. ");
            }
            AddLinksToNewsItems(news, news.Id);

            return(news);
        }
        public NewsItemDetailDto GetNewsItemById(int id)
        {
            var newsItem = _newsItemRepository.GetNewsItemById(id);

            newsItem.Links.AddReference("self", $"api/{newsItem.Id}");
            newsItem.Links.AddReference("edit", $"api/{newsItem.Id}");
            newsItem.Links.AddReference("delete", $"api/{newsItem.Id}");
            newsItem.Links.AddListReference("authors", _authorRepository.GetAuthorsByNewsItemId(newsItem.Id).Select(a => new { href = $"/api/authors/{a.Id}" }));
            newsItem.Links.AddListReference("categories", _categoryRepository.GetCategoriesByNewsItemId(newsItem.Id).Select(c => new { href = $"/api/categories/{c.Id}" }));

            return(newsItem);
        }
예제 #4
0
 public NewsItemAuthors CreateNewsItemAuthor(int authorId, int newsItemId)
 {
     if (_authorRepository.GetAuthorById(authorId) != null)
     {
         if (_newsItemRepository.GetNewsItemById(newsItemId) != null)
         {
             if (!_authorRepository.CheckNewsItemAuthorRelation(authorId, newsItemId))
             {
                 return(_authorRepository.CreateNewsItemAuthor(authorId, newsItemId));
             }
             throw new ResourceAlreadyExistsException();
         }
         throw new ResourceNotFoundException($"News item with id {newsItemId} was not found.");
     }
     throw new ResourceNotFoundException($"Author with id {authorId} was not found.");
 }
        public NewsItemDetailDto GetNewsItemById(int newsItemId)
        {
            var entity      = _newsItemRepository.GetNewsItemById(newsItemId);
            var authorIds   = _newsItemRepository.GetAllAuthorIdByNewsItemId(entity.Id).ToList();
            var categoryIds = _categoryRepository.GetAllCategoriesIdByNewsItemId(entity.Id).ToList();

            entity.Links.AddReference("self", new { href = $"api/{entity.Id}" });
            entity.Links.AddReference("edit", new { href = $"api/{entity.Id}" });
            entity.Links.AddReference("delete", new { href = $"api/{entity.Id}" });
            authorIds.ForEach(s => {
                entity.Links.AddReference("authors", new { href = $"api/authors/{s}" });
            });
            categoryIds.ForEach(s => {
                entity.Links.AddReference("categories", new { href = $"api/authors/{s}" });
            });

            return(entity);
        }
예제 #6
0
 public NewsItemDetailDto getNewsItemById(int id)
 {
     return(_newsItemRepository.GetNewsItemById(id));
 }