//Method that edits books in the database //1) Finds the book and author entries in the database //2) Updates the data with the inputs on the selected author and book public void EditBook(string authorFirst, string authorLast, string bookTitle, string isbn10 = null, string isbn13 = null, string publisher = null, string publishDate = null, int numOfPages = 0, string description = null, int review = 0, bool read = false) { using (var db = new BiblioContext()) { var authorQuery = db.Authors.Where(a => a.AuthorId == SelectedBook.AuthorId); if (authorQuery.Count() != 0) { var author = authorQuery.First(); author.FirstName = authorFirst; author.LastName = authorLast; } db.SaveChanges(); var bookQuery = db.Books.Where(b => b.BookId == SelectedBook.BookId); if (bookQuery.Count() != 0) { var book = bookQuery.First(); book.Title = bookTitle; book.Isbn10 = isbn10; book.Isbn13 = isbn13; book.Publisher = publisher; book.PublishedDate = publishDate; book.NumOfPages = numOfPages; book.Description = description; book.Read = read; book.Review = review; } db.SaveChanges(); } }
public void AddBookTestNewBook() { int preAddTestBooks = 0; int preAddTestAuthor = 0; int postAddTestBooks = 0; int postAddTestAuthor = 0; using (var db = new BiblioContext()) { preAddTestBooks = db.Books.Count(); preAddTestAuthor = db.Authors.Count(); } _testBiblio.AddBook(AuthorFirst, AuthorLast, Title, Isbn10, Isbn13, Publisher, PublishedDate, NumOfPages, Description, Review, Read); using (var db = new BiblioContext()) { postAddTestBooks = db.Books.Count(); postAddTestAuthor = db.Authors.Count(); } Assert.AreEqual(preAddTestBooks, postAddTestBooks - 1); Assert.AreEqual(preAddTestAuthor, postAddTestAuthor - 1); using (var db = new BiblioContext()) { var removeBook = db.Books.OrderByDescending(b => b.BookId).First(); var removeAuthor = db.Authors.OrderByDescending(a => a.AuthorId).First(); db.Remove(removeBook); db.SaveChanges(); db.Remove(removeAuthor); db.SaveChanges(); } }
public void EditBookTest() { using (var db = new BiblioContext()) { Authors testAuthor = new Authors { FirstName = "Aaron A.", LastName = "Aardvark" }; db.Add(testAuthor); db.SaveChanges(); int authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId; Books testBook = new Books { Title = "A Aardvark Attractiveness Album", AuthorId = authorId, Isbn10 = "0000000000", Isbn13 = "0000000000000", NumOfCopies = 1, NumOfPages = 200, Read = true, Review = 5, PublishedDate = "2020", Publisher = "Aardvark 'ardbacks", Description = "Aardvark attractiveness alternates accorinding to austerity apparentley, as acknowledged by Aardvark 'ardbacks anyway." }; db.Add(testBook); db.SaveChanges(); _testBiblio.SetSelectedBook(testBook); } string editDescription = "new description"; int editReview = 4; bool editRead = false; _testBiblio.EditBook("Aaron a.", "Aardvark", "An Aardvark Attractiveness Album", "0000000000", "0000000000000", "Aardvark 'ardbacks", "2020", 200, editDescription, editReview, editRead); using (var db = new BiblioContext()) { string newDescription = db.Books.OrderByDescending(b => b.BookId).First().Description; int newReview = db.Books.OrderByDescending(b => b.BookId).First().Review; bool newRead = db.Books.OrderByDescending(b => b.BookId).First().Read; Assert.AreEqual(editDescription, newDescription); Assert.AreEqual(editReview, newReview); Assert.AreEqual(editRead, newRead); var removeBook = db.Books.OrderByDescending(b => b.BookId).First(); var removeAuthor = db.Authors.OrderByDescending(a => a.AuthorId).First(); db.Remove(removeBook); db.SaveChanges(); db.Remove(removeAuthor); db.SaveChanges(); } }
public IActionResult AjouterLivre([FromBody] Livre livre) { if (livre == null) { return(BadRequest()); } _context.Livres.Add(livre); _context.SaveChanges(); return(CreatedAtRoute("GetAll", new { isbn = livre.ISBN }, livre)); }
public void FillOffer() { using (var context = new BiblioContext()) { var list = new List <Offer>(); while (_xmlReader.ReadToFollowing(Constant.KeyField)) { var id = _xmlReader.GetAttribute("id"); var rTree = _xmlReader.ReadSubtree(); rTree.MoveToContent(); var xmlDoc = new XmlDocument(); xmlDoc.LoadXml(rTree.ReadOuterXml()); var nodes = xmlDoc.SelectNodes(Constant.KeyField); foreach (XmlNode item in nodes) { var categoryId = item.SelectSingleNode(Field.CategoryId.ToQueryString())?.InnerText; Category category = null; if (categoryId != null) { category = context.Categories.FirstOrDefault(x => x.CatId.ToString() == categoryId); } var offer = new Offer { Author = item.SelectSingleNode(Field.Author.ToQueryString())?.InnerText, Description = item.SelectSingleNode(Field.Description.ToQueryString())?.InnerText, Title = item.SelectSingleNode(Field.Name.ToQueryString())?.InnerText, Year = item.SelectSingleNode(Field.Year.ToQueryString())?.InnerText, Publisher = item.SelectSingleNode(Field.Publisher.ToQueryString())?.InnerText, ISBN = item.SelectSingleNode(Field.ISBN.ToQueryString())?.InnerText, Pages = item.SelectSingleNode(Field.Page_Extent.ToQueryString())?.InnerText, OfferId = int.Parse(id), Category = category }; list.Add(offer); if (list.Count > 10000) { context.Offers.AddRange(list); context.SaveChanges(); list.Clear(); } } } context.Offers.AddRange(list); context.SaveChanges(); } }
public void OrderByTitleTest() { using (var db = new BiblioContext()) { Authors testAuthor = new Authors { FirstName = "Aaron A.", LastName = "Aardvark" }; db.Add(testAuthor); db.SaveChanges(); int authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId; Books testBook = new Books { Title = "A Aardvark Attractiveness Album", AuthorId = authorId, Isbn10 = "0000000000", Isbn13 = "0000000000000", NumOfCopies = 1, NumOfPages = 200, Read = true, Review = 5, PublishedDate = "2020", Publisher = "Aardvark 'ardbacks", Description = "Aardvark attractiveness alternates accorinding to austerity apparentley, as acknowledged by Aardvark 'ardbacks anyway." }; db.Add(testBook); db.SaveChanges(); } var result = _testBiblio.GetAllBooksByTitle().First(); Assert.AreEqual(result.Title, "A Aardvark Attractiveness Album"); Assert.AreEqual(result.Author.FirstName, "Aaron A."); Assert.AreEqual(result.Author.LastName, "Aardvark"); using (var db = new BiblioContext()) { var removeBook = db.Books.OrderByDescending(b => b.BookId).First(); var removeAuthor = db.Authors.OrderByDescending(a => a.AuthorId).First(); db.Remove(removeBook); db.SaveChanges(); db.Remove(removeAuthor); db.SaveChanges(); } }
public void DeleteBookTest() { Books selectedBook; using (var db = new BiblioContext()) { Authors testAuthor = new Authors { FirstName = "Aaron A.", LastName = "Aardvark" }; db.Add(testAuthor); db.SaveChanges(); int authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId; Books testBook = new Books { Title = "A Aardvark Attractiveness Album", AuthorId = authorId, Isbn10 = "0000000000", Isbn13 = "0000000000000", NumOfCopies = 1, NumOfPages = 200, Read = true, Review = 5, PublishedDate = "2020", Publisher = "Aardvark 'ardbacks", Description = "Aardvark attractiveness alternates accorinding to austerity apparentley, as acknowledged by Aardvark 'ardbacks anyway." }; db.Add(testBook); db.SaveChanges(); selectedBook = testBook; } _testBiblio.DeleteBook(selectedBook); using (var db = new BiblioContext()) { int resultBook = db.Books.Where(b => b.Title == "A Aardvark Attractiveness Album").Count(); int resultAuthor = db.Authors.Where(a => a.LastName == "Aardvark" && a.FirstName == "Aaron A.").Count(); Assert.AreEqual(resultBook, 0); Assert.AreEqual(resultAuthor, 1); var removeAuthor = db.Authors.OrderByDescending(a => a.AuthorId).First(); db.SaveChanges(); db.Remove(removeAuthor); db.SaveChanges(); } }
//Method that deletes the selected book public void DeleteBook(Books selectedBook) { using (var db = new BiblioContext()) { var bookQuery = db.Books.Where(b => b.BookId == selectedBook.BookId).ToList().First(); db.Remove(bookQuery); db.SaveChanges(); } }
public ActionResult Create([Bind(Include = "Title,ISBN,Price")] Book book) { try { var au = int.Parse(Request["Author"]); book.Author = db.Authors.Find(au); if (ModelState.IsValid) { db.Books.Add(book); db.SaveChanges(); return(RedirectToAction("Index")); } } catch (RetryLimitExceededException /* dex */) { //Log the error (uncomment dex variable name and add a line here to write a log.) ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists, see your system administrator."); } return(View(book)); }
public BiblioController(BiblioContext context) { _context = context; if (_context.Livres.Count() == 0) { _context.Livres.Add(new Livre("harry potter", "jk rowling", "poche", 12345, 12)); _context.Livres.Add(new Livre("lord of the rings", "Tolkien", "ace books", 87452, 6)); _context.Livres.Add(new Livre("the hobbit", "Tolkien", "ace books", 87450, 6)); _context.Livres.Add(new Livre("don quixote", "de cervantes", "glenat", 25478, 8)); _context.Users.Add(new UtilisateurAbonne("ali")); _context.Users.Add(new UtilisateurAbonne("tom")); _context.Users.Add(new UtilisateurAbonne("tom")); _context.Commentaires.Add(new Commentaire(12345, "un commentaire hp")); _context.Commentaires.Add(new Commentaire(12345, "un deuxieme commentaire")); _context.SaveChanges(); } }
public void FillGroup() { using (var context = new BiblioContext()) { var list = new List <Category>(); while (_xmlReader.ReadToFollowing(Constant.KeyCategory)) { var id = _xmlReader.GetAttribute("id"); var parentId = _xmlReader.GetAttribute("parentId"); var value = _xmlReader.ReadElementContentAsString(); var category = new Category { CatId = int.Parse(id), Parent = parentId == null ? null : list .FirstOrDefault(x => x.Id.ToString() == parentId), Name = value }; list.Add(category); } context.Categories.AddRange(list); context.SaveChanges(); } }
public void AddBookTestExistingBook() { int preAddTestBooks = 0; int preAddTestAuthor = 0; int preNumCopies = 0; int postAddTestBooks = 0; int postAddTestAuthor = 0; int postNumCopies = 0; using (var db = new BiblioContext()) { Authors testAuthor = new Authors { FirstName = "Aaron A.", LastName = "Aardvark" }; db.Add(testAuthor); db.SaveChanges(); int authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId; Books testBook = new Books { Title = "A Aardvark Attractiveness Album", AuthorId = authorId, Isbn10 = "0000000000", Isbn13 = "0000000000000", NumOfCopies = 1, NumOfPages = 200, Read = true, Review = 5, PublishedDate = "2020", Publisher = "Aardvark 'ardbacks", Description = "Aardvark attractiveness alternates accorinding to austerity apparentley, as acknowledged by Aardvark 'ardbacks anyway." }; db.Add(testBook); db.SaveChanges(); preAddTestBooks = db.Books.Count(); preAddTestAuthor = db.Authors.Count(); preNumCopies = db.Books.Where(b => b.Title == "A Aardvark Attractiveness Album").First().NumOfCopies; } _testBiblio.AddBook(AuthorFirst, AuthorLast, Title, Isbn10, Isbn13, Publisher, PublishedDate, NumOfPages, Description, Review, Read); using (var db = new BiblioContext()) { postAddTestBooks = db.Books.Count(); postAddTestAuthor = db.Authors.Count(); postNumCopies = db.Books.Where(b => b.Title == "A Aardvark Attractiveness Album").First().NumOfCopies; } Assert.AreEqual(preAddTestBooks, postAddTestBooks); Assert.AreEqual(preAddTestAuthor, postAddTestAuthor); Assert.AreEqual(postNumCopies - 1, preNumCopies); using (var db = new BiblioContext()) { var removeBook = db.Books.OrderByDescending(b => b.BookId).First(); var removeAuthor = db.Authors.OrderByDescending(a => a.AuthorId).First(); db.Remove(removeBook); db.SaveChanges(); db.Remove(removeAuthor); db.SaveChanges(); } }
//Method that adds books to the database //1) Checks if the author is in the database //2a) If author is in then author Id is set to the inputted author //2b) If author isn't in then the author is added and author Id is set to the latest one in the database //2b.1) If the firstname contains Dr. or Prof. then that is separated and added as author Title. //3) Checks if the book is in the database //3a) If book is in then nothing is added //3b) If book isnt then the book is added with inputted data public void AddBook(string authorFirst, string authorLast, string bookTitle, string isbn10 = null, string isbn13 = null, string publisher = null, string publishDate = null, int numOfPages = 0, string description = null, int review = 0, bool read = false) { using (var db = new BiblioContext()) { int authorId; var authorQuery = db.Authors.Where(a => a.FirstName.Contains(authorFirst) && a.LastName.Contains(authorLast)).ToList(); if (authorQuery.Count() == 0) { if (authorFirst.ToUpper().Contains("DR.")) { Authors author = new Authors { Title = "Dr.", FirstName = authorFirst.Remove(0, 4), LastName = authorLast }; db.Add(author); authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId; } else if (authorFirst.ToUpper().Contains("PROF.")) { Authors author = new Authors { Title = "Prof.", FirstName = authorFirst.Remove(0, 6), LastName = authorLast }; db.Add(author); authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId; } else { Authors author = new Authors { Title = null, FirstName = authorFirst, LastName = authorLast }; db.Add(author); } db.SaveChanges(); authorId = db.Authors.OrderByDescending(a => a.AuthorId).First().AuthorId; } else { authorId = authorQuery.First().AuthorId; } var bookQuery = db.Books.Where(b => b.Title == bookTitle).ToList(); if (bookQuery.Count() == 0) { Books book = new Books { Title = bookTitle, Isbn10 = isbn10, Isbn13 = isbn13, Publisher = publisher, PublishedDate = publishDate, NumOfPages = numOfPages, NumOfCopies = 1, Description = description, Read = read, AuthorId = authorId, Review = review }; db.Add(book); } else { bookQuery.First().NumOfCopies++; } db.SaveChanges(); } }