コード例 #1
0
        public IActionResult CreateArticleForProduct(Guid productId, [FromBody] ArticleForCreationDto article)
        {
            if (article == null)
            {
                return(BadRequest());
            }

            if (!_libraryRepository.ProductExists(productId))
            {
                return(NotFound());
            }

            var articleEntity = Mapper.Map <Article>(article);

            _libraryRepository.AddArticleForProduct(productId, articleEntity);

            if (!_libraryRepository.Save())
            {
                throw new Exception("Creating an article failed on save.");
                //return StatusCode(500, "An unexpected fault happened. Try again later.");
            }

            var articleToReturn = Mapper.Map <ArticleDto>(articleEntity);

            return(CreatedAtRoute("GetArticleForProduct", new { productId = productId, id = articleToReturn.Id }, articleToReturn));
        }