public async Task <ActionResult> Post([FromBody] BookForCreationDTO bookForCreationDTO, int authorId) { if (bookForCreationDTO == null) { _logger.LogError("bookForCreationDTO object sent from client is null."); return(BadRequest("EmployeeForCreationDto object is null")); } /* Falta validar que el libro no exista previamente */ var author = await _repositoryManager.Author.GetAuthorByIdWithBooksAsync(authorId); if (author == null) { _logger.LogInfo($"Author with publicationId: {authorId} doesn't exist in the database."); return(NotFound()); } var book = _mapper.Map <Book>(bookForCreationDTO); _repositoryManager.Book.CreateBookForAuthor(author, book); await _repositoryManager.SaveAsync(); var bookToReturn = _mapper.Map <BookReturnCreateDTO>(book); return(CreatedAtRoute("GetBookForAuthor", new { authorId, publicationId = bookToReturn.PublicationId }, bookToReturn)); }
public async Task <BookDTO> CreateBook(int authorId, BookForCreationDTO value) { var bookEntity = mapper.Map <Book>(value); await booksRepository.AddBookToAuthor(authorId, bookEntity); return(createLinksStrategy.CreateLinksForBookResource(mapper.Map <BookDTO>(bookEntity))); }
public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreationDTO book) { if (book == null) { return(BadRequest()); } if (book.Description == book.Title) { ModelState.AddModelError(nameof(BookForCreationDTO), "The provided description should be different from the title."); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } if (!libRepo.AuthorExists(authorId)) { return(NotFound()); } var bookEntity = Mapper.Map <Book>(book); libRepo.AddBookForAuthor(authorId, bookEntity); if (!libRepo.Save()) { throw new Exception($"Creating a book for author {authorId} failed on save."); } var bookToReturn = Mapper.Map <BookDTO>(bookEntity); return(CreatedAtRoute("GetBook", new { authorId = authorId, id = bookToReturn.Id }, bookToReturn)); }
public async Task <IActionResult> Post(int authorId, [FromBody] BookForCreationDTO value) { if (value == null) { return(BadRequest()); } if (!bookValidationStrategy.IsValid(value)) { return(new UnprocessableEntityObjectResult(bookValidationStrategy.GetValidationResults(value))); } var authorExists = await createBookStrategy.AuthorExists(authorId); if (!authorExists) { return(NotFound()); } var bookToReturn = await createBookStrategy.CreateBook(authorId, value); return(Created("/", bookToReturn)); }