コード例 #1
0
        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));
            }
        }
コード例 #2
0
        public async Task <IActionResult> AddBook([FromBody] Book book)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var result = await _booksService.AddBookAsync(book);

            return(Ok(result));
        }
コード例 #3
0
 public async Task PostBook([FromBody] Book book)
 {
     try
     {
         await _booksService.AddBookAsync(book);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
コード例 #4
0
ファイル: BooksController.cs プロジェクト: MarcosPiedra/Book
        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)));
        }
コード例 #5
0
 protected override async Task OnSaveCoreAsync()
 {
     try
     {
         if (IsNew)
         {
             await _booksService.AddBookAsync(Item);
         }
         else
         {
             await _booksService.UpdateBookAsync(Item);
         }
     }
     catch (Exception ex)
     {
         SetError(ex.Message);
     }
 }
コード例 #6
0
        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();
        }