Пример #1
0
        public async Task <ActionResult <ArticleDTO> > GetArticleByIdAsync(int id)
        {
            var specification = new ArticleWithCategoryByIdSpecification(id);
            var article       = await _repository.GetEntityWithSpecificationAsync(specification);

            if (article == null)
            {
                return(NotFound("Article not found."));
            }
            return(Ok(_mapper.Map <Article, ArticleDTO>(article)));
        }
Пример #2
0
        public async Task <ActionResult <ArticleDTO> > UpdateArticleAsync(ArticleDTO article)
        {
            var result = await _repository.UpdateAsync(_mapper.Map <ArticleDTO, Article>(article));

            if (result != null)
            {
                var specification  = new ArticleWithCategoryByIdSpecification(result.Id);
                var updatedArticle = await _repository.GetEntityWithSpecificationAsync(specification);

                return(Ok(_mapper.Map <Article, ArticleDTO>(updatedArticle)));
            }
            return(BadRequest("Failed to update article."));
        }
Пример #3
0
        public async Task <ActionResult <ArticleDTO> > AddArticleAsync(ArticleDTO newArticle)
        {
            var mapped = _mapper.Map <ArticleDTO, Article>(newArticle);

            mapped.CreatedDateTime = DateTime.Now;
            var result = await _repository.CreateAsync(mapped);

            if (result != null)
            {
                var specification = new ArticleWithCategoryByIdSpecification(result.Id);
                var article       = await _repository.GetEntityWithSpecificationAsync(specification);

                return(Ok(_mapper.Map <Article, ArticleDTO>(article)));
            }
            return(BadRequest("Failed to add article."));
        }