public async Task <ActionResult <Book> > PostBook(int authorId, [FromBody] Book book) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var bookCreated = await booksService.AddBookAsync(authorId, book); return(Created($"/api/authors/{authorId}/books/{book.Id}", bookCreated)); } catch (InvalidOperationException ex) { return(BadRequest(ex.Message)); } catch (NotFoundItemException ex) { return(NotFound(ex.Message)); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }
public async Task <IActionResult> AddBook([FromBody] Book book) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var result = await _booksService.AddBookAsync(book); return(Ok(result)); }
public async Task PostBook([FromBody] Book book) { try { await _booksService.AddBookAsync(book); } catch (Exception e) { Console.WriteLine(e.Message); } }
public async Task <IActionResult> AddBooksAsync([FromBody] BookDTO bookDTO) { var bookToAdd = _mapper.Map <BookDTO, Book>(bookDTO); try { await _booksServices.AddBookAsync(bookToAdd); } catch (Exception _ex) { _logger.Write(_ex); return(NoContent()); } return(Json(_mapper.Map <Book, BookDTO>(bookToAdd))); }
protected override async Task OnSaveCoreAsync() { try { if (IsNew) { await _booksService.AddBookAsync(Item); } else { await _booksService.UpdateBookAsync(Item); } } catch (Exception ex) { SetError(ex.Message); } }
private void AddBook() { Console.WriteLine("Creating a book."); var authors = _authorsService.GetAllAsync().Result; if (authors.Count == 0) { Console.WriteLine("There are no authors to choose from. Add them first!"); return; } PrintAuthors(authors, true); int authorId = _ioHelper.GetIntFromUser("Select author id"); if (!authors.Any(author => author.Id == authorId)) { Console.WriteLine("Incorrect author Id!"); return; } var newBook = new Book() { AuthorId = authorId, Title = _ioHelper.GetTextFromUser("Enter title"), Description = _ioHelper.GetTextFromUser("Enter description"), Genre = _ioHelper.GetBookGenreFromUser("Enter genre"), HardCover = _ioHelper.GetBoolFromUser("Is in hard cover"), PagesCount = _ioHelper.GetUintFromUser("Enter pages count"), Price = _ioHelper.GetFloatFromUser("Enter price"), PublishDate = _ioHelper.GetDateTimeFromUser("Publish date"), Publisher = _ioHelper.GetTextFromUser("Enter publisher"), CopiesCount = _ioHelper.GetUintFromUser("Enter amount of copies") }; _booksService.AddBookAsync(newBook).Wait(); Console.WriteLine("Book added successfully"); //TODO Move to AddBook _notificationService.NotifyNewBookArrivalAsync(newBook).Wait(); }