//// POST: api/Books
        //[ResponseType(typeof(Book))]
        public IHttpActionResult PostBook(BookDto book)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _booksService.Add(book);

            return(CreatedAtRoute("DefaultApi", new { id = book.Id }, book));
        }
Esempio n. 2
0
 public ActionResult <Book> Create([FromBody] Book newBook)
 {
     try
     {
         return(Ok(_bs.Add(newBook)));
     }
     catch (System.Exception err)
     {
         return(BadRequest(err.Message));
     }
 }
        public void RemovingLink_BetweenTwoWords_WillRelinkWordsInBooks(
            [Values(true, false)] bool enBookLang,
            [Values(true, false)] bool invertRemoval)
        {
            // Arrange
            var langEn  = new Language("English");
            var langEsp = new Language("Spanish");

            var masterA = new MasterWord();
            var wordA   = new Word(masterA, "Hi", langEn);
            var wordB   = new Word(masterA, "Hola", langEsp);

            new Word(masterA, "some word", new Language("some lang"));

            var book = new Book("name", enBookLang ? langEn : langEsp);

            book.AddWord(masterA);

            MasterWordsService.Add(masterA);
            BooksService.Add(book);


            // Act
            RefreshServicesAndClearCache();
            wordA = WordsService.GetAll().First(w => w.ID == wordA.ID);
            wordB = WordsService.GetAll().First(w => w.ID == wordB.ID);

            if (invertRemoval)
            {
                WordsService.RemoveTranslation(wordA, wordB);
            }
            else
            {
                WordsService.RemoveTranslation(wordB, wordA);
            }

            // Assert
            book = BooksService.Get(book.ID);

            Assert.AreEqual(1, book.WordCount, "Book word count should be 1. Old Ref");
            Assert.AreEqual(1, book.WordCount, "Book word count should be 1. New Ref");

            Assert.IsNotNull(book.Words.First()[book.Language],
                             "After removing translation between two words, book should keep the master word which aligns with book language. New Ref");

            var wordInBook      = enBookLang ? wordA : wordB;
            var wordWithoutBook = enBookLang ? wordB : wordA;

            Assert.AreEqual(book, wordInBook.MasterWord.Books.First(), "word should be in a book");
            Assert.AreEqual(book.Language, wordInBook.Language, "book and word languages should be the same");
            Assert.AreEqual(book.Words.First(), wordInBook.MasterWord, "master word should be the same of word left of correct language");
            Assert.AreEqual(0, wordWithoutBook.MasterWord.Books.Count(), "other word should have no books");
        }
Esempio n. 4
0
        public void AddBook_ShouldIncreseBooks()
        {
            var booksService = new BooksService(mockedBooksRepository, mockedLogger);

            var excpectedCount = 4;

            booksService.Add(new Book {
                Id = 4, Title = "NewBook"
            });

            Assert.AreEqual(excpectedCount, inMemoryBooks.Count());
        }
        public void AddShould_ReturnValue()
        {
            // Arrange
            var  bookService = new BooksService(bookRepoMock.Object, contextMock.Object);
            Book book        = new Book();

            // Arrange
            contextMock.Setup(x => x.Commit()).Returns(1);
            var result = bookService.Add(book);

            // Assert
            Assert.IsInstanceOf(typeof(int), result);
        }
Esempio n. 6
0
        public async Task <ActionResult> Create(BookViewModel model)
        {
            try
            {
                await service.Add(model.Name, model.Author, model.ISBN);

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View());
            }
        }
Esempio n. 7
0
 public IActionResult Add([FromBody] BookRequestDTO book)
 {
     try
     {
         var message = _bookService.Add(book);
         if (string.IsNullOrEmpty(message))
         {
             return(Ok("Livro cadastrado com sucesso!"));
         }
         else
         {
             return(BadRequest(message));
         }
     }
     catch (Exception ex)
     {
         return(BadRequest(ex.InnerException.Message));
     }
 }
        public void AddingLink_BetweenTwoWords_WillUpdateRefsInBooksToCorrectMasterWord(
            [Values(true, false)] bool bookLinksToBoth,
            [Values(true, false)] bool cleanCache)
        {
            // Arrange
            PrepareTwoWords(out MasterWord masterA, out Word wordA, out MasterWord masterB, out Word wordB);

            var book = new Book("name", wordA.Language);

            book.AddWord(masterB); // masterB is the one being destroyed

            if (bookLinksToBoth)
            {
                book.AddWord(masterA);
            }

            BooksService.Add(book);

            if (cleanCache)
            {
                RefreshServicesAndClearCache();
                wordA = WordsService.GetAll().First(w => w.ID == wordA.ID);
                wordB = WordsService.GetAll().First(w => w.ID == wordB.ID);
            }

            // Act
            WordsService.AddTranslation(wordA, wordB);

            // Assert
            book    = BooksService.Get(book.ID);
            masterA = MasterWordsService.Get(masterA.ID);
            masterB = MasterWordsService.Get(masterB.ID);

            Assert.AreEqual(masterA.ID, book.Words.First().ID, "Master words should be the same");
            Assert.AreEqual(1, book.Words.Count(), "Master words should be the same");

            Assert.AreEqual(book, masterA.Books.First(), "MasterA should link to correct book");
            Assert.IsNull(masterB, "MasterB should have been destroyed");
        }
        public IActionResult CreateBook(Guid authorId, [FromBody] BookCreateDTO model)
        {
            if (model == null)
            {
                return(BadRequest());
            }
            if (!AuthorsService.Exists(authorId))
            {
                return(NotFound());
            }
            if (!ModelState.IsValid)
            {
                return(UnprocessableEntity());
            }

            model.AuthorId = authorId;
            var entity = Mapper.Map <BookCreateDTO, Book>(model);

            BooksService.Add(entity);

            var createdModel = Mapper.Map <Book, BookDTO>(entity);

            return(CreatedAtRoute(nameof(GetBook), new { id = entity.Id }, createdModel));
        }
Esempio n. 10
0
        static void Main(string[] args)
        {
            BooksService   bs = new BooksService();
            GenresService  gs = new GenresService();
            ReviewsService rs = new ReviewsService();

            int menuChoice = 0;

            do
            {
                bool correct = true;
                do
                {
                    try
                    {
                        ShowMenu();
                        Console.WriteLine("Input menu choice: ");
                        menuChoice = Convert.ToInt32(Console.ReadLine());
                        correct    = true;
                    }
                    catch
                    {
                        correct = false;
                    }
                }while (correct == false);


                Console.Clear();
                switch (menuChoice)
                {
                case 1:
                    //Add Book

                    Book myBook = new Book();

                    Console.WriteLine("Input book name: ");
                    myBook.Name = Console.ReadLine().ToLower();
                    Console.WriteLine();
                    Console.WriteLine("Input book isbn: ");
                    myBook.Isbn = Console.ReadLine();
                    Console.WriteLine();
                    Console.WriteLine("Input book publisher: ");
                    myBook.Publisher = Console.ReadLine();
                    Console.WriteLine();
                    Console.WriteLine("Input book author: ");
                    myBook.Author = Console.ReadLine();

                    Console.WriteLine();
                    Console.WriteLine("Input book year: ");
                    myBook.Year = Convert.ToInt32(Console.ReadLine());

                    //get all the genres that i have in my db
                    Console.WriteLine();

                    foreach (var g in gs.GetGenres())
                    {
                        Console.WriteLine($"{g.Id}. {g.Name}");
                    }
                    Console.WriteLine();
                    Console.WriteLine("Select one of the Genres by inputting the number next to the name: ");
                    myBook.GenreFK = Convert.ToInt32(Console.ReadLine());

                    Console.WriteLine();



                    bs.Add(myBook);
                    Console.WriteLine("Book was added successfully");
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadKey();

                    break;

                case 2:
                    //Get Books
                    List <Book> listOfAllBooks = bs.GetBooks();
                    DisplayListOfBooks(listOfAllBooks);
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadKey();
                    break;

                case 3:
                    //Get Books by Genre

                    foreach (var g in gs.GetGenres())
                    {
                        Console.WriteLine($"{g.Id}. {g.Name}");
                    }
                    Console.WriteLine();
                    Console.WriteLine("Select one of the Genres by inputting the number next to the name: ");
                    int selectedGenre = Convert.ToInt32(Console.ReadLine());

                    //overloading
                    //static polymorphism
                    var listOfBooksFilteredByGenre = bs.GetBooks(selectedGenre);
                    DisplayListOfBooks(listOfBooksFilteredByGenre);
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadKey();



                    break;

                case 4:
                    var total = bs.GetTotalNoOfBooks();

                    Console.WriteLine($"The total no of books in the library db is {total}");
                    Console.WriteLine();
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadKey();

                    break;

                case 5:
                    Console.WriteLine("Please input the no. next to the field you want to sort books by");
                    Console.WriteLine("1. Name");
                    Console.WriteLine("2. Author");
                    Console.WriteLine("3. Year");
                    Console.WriteLine("Input 1-3: ");
                    int fieldChoice = Convert.ToInt32(Console.ReadLine());

                    var sortedList = bs.GetBooksSorted(fieldChoice);
                    DisplayListOfBooks(sortedList);

                    Console.WriteLine("Press a key to continue...");
                    Console.ReadKey();

                    break;

                case 6:


                    break;

                case 7:
                    Console.WriteLine("Which book do you want to update? Input the isbn");
                    string isbn = Console.ReadLine();

                    Book myBookToEdit = new Book();

                    Console.WriteLine("Input book name: ");
                    myBookToEdit.Name = Console.ReadLine().ToLower();
                    Console.WriteLine();

                    Console.WriteLine("Input book publisher: ");
                    myBookToEdit.Publisher = Console.ReadLine();
                    Console.WriteLine();
                    Console.WriteLine("Input book author: ");
                    myBookToEdit.Author = Console.ReadLine();

                    Console.WriteLine();
                    Console.WriteLine("Input book year: ");
                    myBookToEdit.Year = Convert.ToInt32(Console.ReadLine());

                    //get all the genres that i have in my db
                    Console.WriteLine();

                    foreach (var g in gs.GetGenres())
                    {
                        Console.WriteLine($"{g.Id}. {g.Name}");
                    }
                    Console.WriteLine();
                    Console.WriteLine("Select one of the Genres by inputting the number next to the name: ");
                    myBookToEdit.GenreFK = Convert.ToInt32(Console.ReadLine());

                    Console.WriteLine();
                    bs.Update(isbn, myBookToEdit.Name, myBookToEdit.Year, myBookToEdit.Publisher, myBookToEdit.Author, myBookToEdit.GenreFK);

                    Console.WriteLine("Book details updated");
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadKey();

                    break;

                case 8:

                    Console.WriteLine("Author\t\tTotal");
                    foreach (AuthorBook item in bs.GetAuthorWithNoOfBooks())
                    {
                        Console.WriteLine($"{item.Author}\t\t{item.Total}");
                    }

                    Console.WriteLine("Press a key to continue...");
                    Console.ReadKey();
                    break;

                case 9:

                    Console.WriteLine("Input isbn:");
                    string isbnForReview = Console.ReadLine();

                    Console.WriteLine("Input comment:");
                    string commentForReview = Console.ReadLine();

                    Console.WriteLine("Input rating:");
                    int ratingForReview = Convert.ToInt32(Console.ReadLine());

                    rs.ReviewABook(isbnForReview, commentForReview
                                   , ratingForReview);

                    Console.WriteLine("Press a key to continue...");
                    Console.ReadKey();

                    break;


                case 10:

                    Console.WriteLine("Input isbn:");
                    string isbn1 = Console.ReadLine();

                    foreach (var r in rs.GetReviews(isbn1))
                    {
                    }
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadKey();
                    break;


                case 11:
                    Console.WriteLine("Input isbn:");
                    string isbn2 = Console.ReadLine();

                    Console.WriteLine($"Avg rating for this book is {rs.GetAverageRatingOfBook(isbn2)}");
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadKey();
                    break;

                case 12:

                    foreach (var item in rs.GetBooksWithNoOfReviews())
                    {
                        Console.WriteLine($"Book isbn {item.Isbn} has {item.TotalReviews} with avg rating of {item.AvgRating}");
                    }
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadKey();
                    break;


                default:
                    if (menuChoice != 999)
                    {
                        Console.WriteLine("Input is not valid");
                    }
                    break;
                }
            } while (menuChoice != 999);
        }
Esempio n. 11
0
        static void Main(string[] args)
        {
            BooksService  bs = new BooksService();
            GenresService gs = new GenresService();


            int menuChoice = 0;

            do
            {
                ShowMenu();
                Console.WriteLine("Input menu choice: ");
                menuChoice = Convert.ToInt32(Console.ReadLine());

                Console.Clear();
                switch (menuChoice)
                {
                case 1:
                    //Add Book

                    Book myBook = new Book();

                    Console.WriteLine("Input book name: ");
                    myBook.Name = Console.ReadLine();
                    Console.WriteLine();
                    Console.WriteLine("Input book isbn: ");
                    myBook.Isbn = Console.ReadLine();
                    Console.WriteLine();
                    Console.WriteLine("Input book publisher: ");
                    myBook.Publisher = Console.ReadLine();
                    Console.WriteLine();
                    Console.WriteLine("Input book author: ");
                    myBook.Author = Console.ReadLine();

                    Console.WriteLine();
                    Console.WriteLine("Input book year: ");
                    myBook.Year = Convert.ToInt32(Console.ReadLine());

                    //get all the genres that i have in my db
                    Console.WriteLine();

                    foreach (var g in gs.GetGenres())
                    {
                        Console.WriteLine($"{g.Id}. {g.Name}");
                    }
                    Console.WriteLine();
                    Console.WriteLine("Select one of the Genres by inputting the number next to the name: ");
                    myBook.GenreFK = Convert.ToInt32(Console.ReadLine());

                    Console.WriteLine();


                    bs.Add(myBook);
                    Console.WriteLine("Book was added successfully");
                    Console.WriteLine("Press a key to continue...");
                    Console.ReadKey();

                    break;

                case 2:
                    //Get Books
                    break;

                case 3:
                    //Get Books by Genre

                    foreach (var g in gs.GetGenres())
                    {
                        Console.WriteLine($"{g.Id}. {g.Name}");
                    }
                    Console.WriteLine();
                    Console.WriteLine("Select one of the Genres by inputting the number next to the name: ");



                    break;

                default:
                    if (menuChoice != 999)
                    {
                        Console.WriteLine("Input is not valid");
                    }
                    break;
                }
            } while (menuChoice != 999);
        }
Esempio n. 12
0
        public void AddBook_ShouldThrowArgumentNullException_IfInputIsNull()
        {
            var booksService = new BooksService(mockedBooksRepository, mockedLogger);

            Assert.ThrowsException <ArgumentNullException>(() => booksService.Add(null));
        }
 public async Task <IActionResult> Add([FromBody] BookToCreate bookToAdd)
 {
     return(Ok(await booksService.Add(bookToAdd)));
 }