public void AddBookReview(BookReview newBookReview)
        {
            XDocument xmlDoc = XMLHelper.LoadDocument(XMLBooksFilePath + XMlBookReviewsFileName);

            int newId = 1;

            //Add the root element to the XML doc, if it's empty
            if (xmlDoc.Element(BookReviewsRoot) == null)
            {
                XElement root = new XElement(BookReviewsRoot);
                xmlDoc.Add(root);
            }
            else
            {
                //Retrieve the new ID
                var query = (from item in xmlDoc.Descendants().Elements(BookReviewsID)
                             select int.Parse(item.Value.ToString())).Max();

                newId = (int)query + 1;
            }

            XElement bookID = new XElement(BookReviewsBookID, newBookReview.BookReviewed.ID.ToString());
            XElement comment = new XElement(BookReviewsComment, newBookReview.Comment);
            XElement isFavourite = new XElement(BookReviewsIsFavourite, newBookReview.IsFavourite.ToString());
            XElement rating = new XElement(BookReviewsRating, newBookReview.Rating.ToString());
            XElement id = new XElement(BookReviewsID, newId.ToString());

            XElement bookReview = new XElement(BookReviewsElement, id, bookID, comment, isFavourite, rating);

            xmlDoc.Element(BookReviewsRoot).Add(bookReview);

            XMLHelper.SaveDocument(xmlDoc, XMLBooksFilePath + XMlBookReviewsFileName);
        }
 public static void ValidateBookReview(BookReview expectedReview, BookReview actualReview)
 {
     Assert.AreEqual(expectedReview.ID, actualReview.ID, "Wrong BookReview ID");
     Assert.AreEqual(expectedReview.Comment, actualReview.Comment, "Wrong BookReview Comment");
     Assert.AreEqual(expectedReview.IsFavourite, actualReview.IsFavourite, "Wrong BookReview IsFavourite");
     Assert.AreEqual(expectedReview.Rating, actualReview.Rating, "Wrong BookReview Rating");
     ValidateBook(expectedReview.BookReviewed, actualReview.BookReviewed);
 }
        /// <summary>
        /// Updated the book review with Id = 2
        /// </summary>
        private XDocument GetBookReviewsReturnedDocumentUpdated(BookReview bookReview)
        {
            XDocument xmlDoc = new XDocument();

            XElement review1 = CreateBookReviewItem(1, 23, RatingBook.Good, false, "The book El nombre de la rosa could have been better");
            XElement review2 = CreateBookReviewItem(bookReview.ID, bookReview.BookReviewed.ID, bookReview.Rating, bookReview.IsFavourite, bookReview.Comment);
            XElement review3 = CreateBookReviewItem(3, 100, RatingBook.Bad, false, "Es tan malo este libro que mejor no comento su título");
            XElement review4 = CreateBookReviewItem(4, 5, RatingBook.Good, false, "El hombre terminal es un libro mejor de lo esperado");

            XElement root = new XElement(BookRepository.BookReviewsRoot);

            xmlDoc.Add(root);

            xmlDoc.Element(BookRepository.BookReviewsRoot).Add(review1);
            xmlDoc.Element(BookRepository.BookReviewsRoot).Add(review2);
            xmlDoc.Element(BookRepository.BookReviewsRoot).Add(review3);
            xmlDoc.Element(BookRepository.BookReviewsRoot).Add(review4);

            return xmlDoc;
        }
        public void UpdateBookReview_WrongID()
        {
            XDocument returnedDoc = GetBookReviewsReturnedDocument();

            //Update book review with Id = 2
            BookReview updatedBookReview = new BookReview() { ID = -1 };

            using (MockRepository.Record())
            {
                Expect.Call(XMLHelper.LoadDocument(BookRepository.XMLBooksFilePath + BookRepository.XMlBookReviewsFileName))
                                                                                          .Repeat.Times(1)
                                                                                          .Return(returnedDoc);
            }

            using (MockRepository.Playback())
            {
                RepositoryBook.UpdateBookReview(updatedBookReview);
            }
        }
        public void UpdateBookReview()
        {
            XDocument returnedDoc = GetBookReviewsReturnedDocument();

            //Update book review with Id = 2
            BookReview updatedBookReview = new BookReview()
            {
                BookReviewed = new Book() { ID = 3 },
                Comment = "This was a great book",
                ID = 2,
                IsFavourite = true,
                Rating = RatingBook.VeryGood
            };

            //create the expected xmldoc to be saved:
            XDocument updatedDocs = GetBookReviewsReturnedDocumentUpdated(updatedBookReview);

            using (MockRepository.Record())
            {
                Expect.Call(XMLHelper.LoadDocument(BookRepository.XMLBooksFilePath + BookRepository.XMlBookReviewsFileName))
                                                                                          .Repeat.Times(1)
                                                                                          .Return(returnedDoc);

                Expect.Call(delegate { XMLHelper.SaveDocument(updatedDocs, BookRepository.XMLBooksFilePath + BookRepository.XMlBookReviewsFileName); })
                                                                .IgnoreArguments()
                                                                .Constraints(Property.AllPropertiesMatch(updatedDocs),
                                                                             Property.AllPropertiesMatch(BookRepository.XMLBooksFilePath + BookRepository.XMlBookReviewsFileName))
                                                                .Repeat.Once();
            }

            using (MockRepository.Playback())
            {
                RepositoryBook.UpdateBookReview(updatedBookReview);
            }
        }
        public void DeleteBookReview()
        {
            XDocument returnedDoc = GetBookReviewsReturnedDocument();

            //delete book review with Id = 2
            BookReview deletedBookReview = new BookReview() { ID = 2 };

            //create the expected xmldoc to be saved:
            XDocument updatedDocs = GetBookReviewsReturnedDocumentDeleted(deletedBookReview.ID);

            using (MockRepository.Record())
            {
                Expect.Call(XMLHelper.LoadDocument(BookRepository.XMLBooksFilePath + BookRepository.XMlBookReviewsFileName))
                                                                                          .Repeat.Times(1)
                                                                                          .Return(returnedDoc);

                Expect.Call(delegate { XMLHelper.SaveDocument(updatedDocs, BookRepository.XMLBooksFilePath + BookRepository.XMlBookReviewsFileName); })
                                                                .IgnoreArguments()
                                                                .Constraints(Property.AllPropertiesMatch(updatedDocs),
                                                                             Property.AllPropertiesMatch(BookRepository.XMLBooksFilePath + BookRepository.XMlBookReviewsFileName))
                                                                .Repeat.Once();
            }

            using (MockRepository.Playback())
            {
                RepositoryBook.DeleteBookReview(deletedBookReview);
            }
        }
        public void AddBookReview_ToEmptyFile()
        {
            //Create new book review
            BookReview newBookReview = new BookReview()
            {
                BookReviewed = new Book() {  ID = 3 },
                Comment = "This was a great book",
                ID = 1,
                IsFavourite = true,
                Rating = RatingBook.VeryGood
            };

            //Create the element for that Book Review
            XElement xmlBookReview = CreateBookReviewItem(newBookReview.ID, newBookReview.BookReviewed.ID, newBookReview.Rating, newBookReview.IsFavourite, newBookReview.Comment);

            //create the expected xmldoc to be saved:
            XDocument expectedDoc = new XDocument();
            XElement root = new XElement(BookRepository.BookReviewsRoot);
            expectedDoc.Add(root);
            expectedDoc.Element(BookRepository.BookReviewsRoot).Add(xmlBookReview);

            using (MockRepository.Record())
            {
                XDocument returnedDoc = new XDocument();

                Expect.Call(XMLHelper.LoadDocument(BookRepository.XMLBooksFilePath + BookRepository.XMlBookReviewsFileName))
                                                                .Repeat.Times(1)
                                                                .Return(returnedDoc);

                Expect.Call(delegate { XMLHelper.SaveDocument(expectedDoc, BookRepository.XMLBooksFilePath + BookRepository.XMlBookReviewsFileName); })
                                                                .IgnoreArguments()
                                                                .Constraints(Property.AllPropertiesMatch(expectedDoc),
                                                                             Property.AllPropertiesMatch(BookRepository.XMLBooksFilePath + BookRepository.XMlBookReviewsFileName))
                                                                .Repeat.Once();
            }

            using (MockRepository.Playback())
            {
                RepositoryBook.AddBookReview(newBookReview);
            }
        }
        public void UpdateBookReview(BookReview updatedReview)
        {
            XDocument xmlDoc = XMLHelper.LoadDocument(XMLBooksFilePath + XMlBookReviewsFileName);

            if (xmlDoc.Element(BookReviewsRoot) == null)
            {
                throw new Exception("The list of books reviews is empty");
            }

            XElement query = (from review in xmlDoc.Element(BookReviewsRoot).Elements(BookReviewsElement)
                              where int.Parse(review.Element(BookReviewsID).Value.ToString()) == updatedReview.ID
                              select review).SingleOrDefault();

            if (query != null)
            {
                query.Element(BookReviewsComment).Value = updatedReview.Comment;
                query.Element(BookReviewsIsFavourite).Value = updatedReview.IsFavourite.ToString();
                query.Element(BookReviewsRating).Value = updatedReview.Rating.ToString();

                XMLHelper.SaveDocument(xmlDoc, XMLBooksFilePath + XMlBookReviewsFileName);
            }
            else
                throw new ArgumentException("The Book Review to update with ID = " + updatedReview.ID.ToString() + " was not present in the list of books reviews");
        }
        public void DeleteBookReview(BookReview review)
        {
            XDocument xmlDoc = XMLHelper.LoadDocument(XMLBooksFilePath + XMlBookReviewsFileName);

            if (xmlDoc.Element(BookReviewsRoot) == null)
                throw new Exception("The list of book reviews is empty");

            XElement query = (from book in xmlDoc.Element(BookReviewsRoot).Elements(BookReviewsElement)
                              where int.Parse(book.Element(BookReviewsID).Value.ToString()) == review.ID
                              select book).FirstOrDefault();
            if (query != null)
            {
                query.Remove();
                XMLHelper.SaveDocument(xmlDoc, XMLBooksFilePath + XMlBookReviewsFileName);
            }
            else
                throw new ArgumentException("The Book Review to delete with ID = " + review.ID.ToString() + " was not present in the list of book reviews");
        }