public async Task <IActionResult> CreateBook([FromBody] BookForCreateDto bookForCreateDto) { var location = GetControllerActionNames(); try { _logger.LogInfo($" {location}: Creating Book"); if (bookForCreateDto == null) { _logger.LogWarn($" {location}: Empty Request was submitted"); return(BadRequest(ModelState)); } if (!ModelState.IsValid) { _logger.LogWarn($" {location}: Data was incomplete"); return(BadRequest(ModelState)); } var book = _mapper.Map <Book>(bookForCreateDto); var IsSuccess = await _bookRepository.Create(book); if (!IsSuccess) { return(InternalError($" {location}: Creation Failed")); } return(Created("Create", new { book })); } catch (Exception _ex) { return(InternalError($"{_ex.Message}\n{_ex.InnerException}")); } }
public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreateDto book) { if (book == null) { return(BadRequest()); } if (book.Title == book.Description) { ModelState.AddModelError(nameof(BookForCreateDto), "The provided description should be different from title."); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } if (!_libraryRepository.AuthorExists(authorId)) { return(NotFound()); } var bookToCreate = Mapper.Map <Book>(book); _libraryRepository.AddBookForAuthor(authorId, bookToCreate); if (!_libraryRepository.Save()) { throw new Exception($"Creating a book for author {authorId} failed on save."); } var bookToReturn = Mapper.Map <BookDto>(bookToCreate); return(CreatedAtRoute("GetBookForAuthor", new { id = bookToReturn.Id }, CreateLinksForBook(bookToReturn))); }
public IActionResult CreateBookForAuthor(Guid authorId, [FromBody] BookForCreateDto book) { if (book == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } book.AuthorId = authorId; if (!_libraryRepository.AuthorExists(authorId)) { return(NotFound()); } var bookEntity = Mapper.Map <Book>(book); _libraryRepository.AddBookForAuthor(authorId, bookEntity); if (!_libraryRepository.Save()) { return(StatusCode(500, "A problem when add book")); } var bookToReturn = Mapper.Map <BookDto>(bookEntity); return(CreatedAtRoute("GetBook", new { authorId = book.AuthorId, bookId = bookToReturn.Id }, bookToReturn)); }
public async Task <IActionResult> CreateBook([FromBody] BookForCreateDto input) { if (ModelState.IsValid) { var book = _mapper.Map <Book>(input); var result = await _bookService.CreateBookAsync(book); if (result) { return(Ok()); } } return(BadRequest(new { message = ModelState.Values.First().Errors[0].ErrorMessage })); }
public IActionResult AddBookForAuthor(Guid authorId, [FromBody] BookForCreateDto bookforCreateDto) { if (bookforCreateDto == null) { return(BadRequest()); } if (bookforCreateDto.Description == bookforCreateDto.Title) { ModelState.AddModelError(nameof(BookForCreateDto), "Title is equal to Description"); } if (!ModelState.IsValid) { return(new UnprocessableEntityObjectResult(ModelState)); } if (!_libraryRepository.AuthorExists(authorId)) { return(NotFound()); } var book = AutoMapper.Mapper.Map <Book>(bookforCreateDto); _libraryRepository.AddBookForAuthor(authorId, book); if (!_libraryRepository.Save()) { throw new Exception("Creating an author failed on server"); //return StatusCode(500,"Unexpected problem occurs, try again later!") } var bookDto = AutoMapper.Mapper.Map <BookDto>(book); bookDto = CreateLinksForBook(bookDto); //return new JsonResult(bookDto); return(CreatedAtAction(nameof(GetBookForAutor), new { authorId, bookId = book.Id }, book)); }