Пример #1
0
        public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookInputDto bookInputDto)
        {
            if (bookInputDto == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                // return 422
                return(new UnprocessableEntityObjectResult(ModelState));
            }

            if (!_authorAppService.AuthorExists(authorId))
            {
                return(NotFound());
            }

            var result = _bookAppService.AddBookForAuthor(authorId, bookInputDto);

            if (!result.sucess)
            {
                throw new Exception($"Creating a book for author {authorId} failed on save.");
            }

            var bookToReturn = _mapper.Map <BookOutputDto>(bookInputDto);

            bookToReturn.Id       = result.id;
            bookToReturn.AuthorId = authorId;
            return(CreatedAtRoute("GetBookForAuthor",
                                  new { authorId = bookToReturn.AuthorId, id = bookToReturn.Id },
                                  CreateLinksForBook(bookToReturn)));
        }