private static Review GetSingleReview(BookstoreEntities context, XmlNode reviewItem)
        {
            if (reviewItem.Name != "review" || reviewItem.InnerText.Length == 0)
            {
                throw new ApplicationException("Invalid input XML file - Invalid Review tag");
            }

            var review = new Review();
            review.ReviewText = reviewItem.InnerText.Trim();
            if (reviewItem.Attributes["date"] != null)
            {
                review.Date = DateTime.Parse(reviewItem.Attributes["date"].InnerText.Trim());
            }
            else
            {
                review.Date = DateTime.Now.Date;
            }

            if (reviewItem.Attributes["author"] != null)
            {
                review.Author = GetSingleAuthor(context, reviewItem.Attributes["author"]);
            }

            return review;
        }
        private Review SerializeToReview(BookstoreEntities context, ReviewTransferObject reviewTransferObject)
        {
            Review review = new Review();
            review.Content = reviewTransferObject.Content;
            string authorName = reviewTransferObject.Author;
            if (authorName != null)
            {
                review.Author = CreateOrLoadAuthor(context, authorName);
            }

            review.DateOfCreation = reviewTransferObject.Date;
            
            return review;
        }
Beispiel #3
0
        public static Review AddReview(int? authorId, int? bookId, DateTime reviewDate, string reviewContent)
        {
            BookstoreEntities context = new BookstoreEntities();
            Review review = new Review();
            review.AuthorId = authorId;
            review.BookId = bookId;
            review.Date = reviewDate;
            review.Text = reviewContent;

            context.Reviews.Add(review);
            context.SaveChanges();

            return review;
        }
		public static Review AddReview(Book book, string reviewAuthor, 
			DateTime reviewDate, string reviewContent)
		{
			BookstoreEntities context = new BookstoreEntities();
			Review newReview = new Review();
			newReview.BookId = book.BookId;
			if (reviewAuthor != null)
			{
				Author author = CreateOrLoadAuthor(context, reviewAuthor);
				newReview.Author = author;
			}
			newReview.Date = reviewDate;
			newReview.Content = reviewContent;
			context.Reviews.Add(newReview);
			context.SaveChanges();

			return newReview;
		}
        private static Review CreateOrLoadReview(BookstoreEntities context, int reviewId)
        {
            Review existingReview =
                (from r in context.Reviews
                 where r.ReviewId == reviewId
                 select r).FirstOrDefault();

            if (existingReview != null)
            {
                return existingReview;
            }

            Review newReview = new Review();
            newReview.ReviewId = reviewId;
            context.Reviews.Add(newReview);

            return newReview;
        }
        public static void ComplexBookAdd(string title, string isbn, string price, string webSiteURL, List<string> authors, List<Dictionary<string, string>> reviews)
        {
            BookstoreEntities context = new BookstoreEntities();
            Book newBook = new Book();

            foreach (var author in authors)
            {
                Author bookAuthor = CreateOrLoadAuthor(context, author);
                newBook.Authors.Add(bookAuthor);
            }

            foreach (var review in reviews)
            {
                Review bookReview = new Review();
                if (review["author"] != null && review["author"] != string.Empty)
                {
                    Author reviewAuthor = CreateOrLoadAuthor(context, review["author"]);
                    bookReview.Author = reviewAuthor;
                }
                bookReview.Text = review["text"];
                var creationDate = DateTime.Now;
                if (review["creationDate"] != null && review["creationDate"] != string.Empty)
                {
                    creationDate = DateTime.Parse(review["creationDate"]);
                }
                bookReview.CreationDate = creationDate;
                newBook.Reviews.Add(bookReview);
            }

            newBook.Title = title;
            newBook.WebSiteURL = webSiteURL;
            if (isbn != string.Empty && isbn != null)
            {
                newBook.ISBN = long.Parse(isbn);
            }

            if (price != string.Empty && price != null)
            {
                newBook.Price = decimal.Parse(price);
            }
            context.Books.Add(newBook);
            context.SaveChanges();
        }
        private static void ProcessReviewNode(BookstoreEntities context, BookstoreDAL bookstoreDAL,
            Book bookToAddReviewsTo, XmlNode reviewNode)
        {
            string reviewAuthorName = GetChildText(reviewNode, "@author");
            string reviewCreationDateString = GetChildText(reviewNode, "@date");
            string reviewText = reviewNode.InnerText.Trim();

            if (string.IsNullOrEmpty(reviewText))
            {
                throw new ArgumentException("Review text cannot be empty.");
            }

            DateTime reviewCreationDate = DateTime.Now;
            if (!string.IsNullOrEmpty(reviewCreationDateString))
            {
                reviewCreationDate = DateTime.Parse(reviewCreationDateString);
            }

            Review newReview = new Review();
            newReview.CreationDate = reviewCreationDate;
            newReview.Text = reviewText;
            newReview.Book = bookToAddReviewsTo;

            if (!string.IsNullOrEmpty(reviewAuthorName))
            {
                newReview.Author = bookstoreDAL.CreateOrLoadAuthor(context, reviewAuthorName);
            }

            context.Reviews.Add(newReview);
            context.SaveChanges();
        }
        static void Main(string[] args)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("../../complex-books.xml");
            string xPathQuery = "/catalog/book";

            XmlNodeList booksList = xmlDoc.SelectNodes(xPathQuery);

            foreach (XmlNode bookNode in booksList)
            {
                string title = bookNode.SelectSingleNode("title").InnerText;

                var authorsExsist = bookNode.SelectSingleNode("authors");

                List<string> authors = new List<string>();
                if (authorsExsist != null)
                {
                    foreach (XmlNode author in bookNode.SelectNodes("authors"))
                    {
                        foreach (XmlNode item in author.SelectNodes("author"))
                        {
                            authors.Add(item.InnerXml.Trim());
                        }
                    }
                }

                var isbnExsist = bookNode.SelectSingleNode("isbn");

                string isbn = null;
                if (isbnExsist != null)
                {
                    isbn = isbnExsist.InnerText;
                }

                var priceExsist = bookNode.SelectSingleNode("price");

                string price = null;
                if (priceExsist != null)
                {
                    price = priceExsist.InnerText;
                }

                var siteExsist = bookNode.SelectSingleNode("web-site");

                string site = null;
                if (siteExsist != null)
                {
                    site = siteExsist.InnerText;
                }

                var reviewsExsist = bookNode.SelectSingleNode("reviews");

                List<Review> reviewsList = new List<Review>();
                if (reviewsExsist != null)
                {
                    foreach (XmlNode reviews in bookNode.SelectNodes("reviews"))
                    {
                        foreach (XmlNode review in reviews.SelectNodes("review"))
                        {
                            DateTime date = DateTime.Now;
                            int? authorId = null;

                            var text = review.InnerText;
                            var dateExsict = review.Attributes["date"];
                            var authorExsict = review.Attributes["author"];

                            if (dateExsict != null)
                            {
                                date = Convert.ToDateTime(dateExsict.Value);
                            }

                            if (authorExsict != null)
                            {
                                string author = authorExsict.Value;
                                authorId = BookstoreDAL.GetAuthorId(author);
                                //if (authorId == 0)
                                //{
                                //    throw new Exception();
                                //}
                            }

                            Review curReview = new Review();
                            curReview.DateOfCreation = date;
                            curReview.AuthorId = authorId;
                            curReview.ReviewText = text;
                            reviewsList.Add(curReview);
                        }
                    }
                }
                //ISBN, price and web site are optional while author and title are obligatory
                //Console.WriteLine("{0}-{1}-{2}-{3}-{4}", isbn, price, site, author, title);

                BookstoreDAL.AddBookComplex(title, authors, isbn, price, site, reviewsList);
            }
        }
        public static void AddToDbComplex(XmlNodeList authors, string title, string isbn, string price, string website, List<ReviewInfo> reviews)
        {
            using (BookstoreDBEntities bookstoreContext = new BookstoreDBEntities())
            {
                Book book = new Book();
                foreach (XmlNode author in authors)
                {
                    var authorToCheck = CreateOrLoadAuthor(bookstoreContext, author.InnerText);
                    book.Authors.Add(authorToCheck);
                }

                book.Title = title;
                book.ISBN = isbn;

                if (price == null)
                {
                    book.Price = null;
                }
                else
                {
                    var convertedPrice = decimal.Parse(price, System.Globalization.CultureInfo.InvariantCulture);
                    book.Price = convertedPrice;
                }

                book.URL = website;

                if (reviews != null)
                {
                    foreach (var review in reviews)
                    {
                        Review currentReview = new Review();
                        if (review.Author == null)
                        {
                            currentReview.AuthorId = null;
                        }
                        else
                        {
                            var currentReviewAuthor = CreateOrLoadAuthor(bookstoreContext, review.Author);
                            currentReview.AuthorId = currentReviewAuthor.AuthorId;
                        }

                        currentReview.Text = review.Text;

                        //GetAuthorId(bookstoreContext, review.Author);
                        if (review.CreatedOn == null)
                        {
                            currentReview.CreatedOn = DateTime.Now;
                        }
                        currentReview.CreatedOn = review.CreatedOn;

                        book.Reviews.Add(currentReview);

                        bookstoreContext.SaveChanges();
                    }
                }

                bookstoreContext.Books.Add(book);
                bookstoreContext.SaveChanges();

            }
        }
        private static void WriteReviewInfo(XmlTextWriter writer, Review review)
        {
            writer.WriteStartElement("review");

            writer.WriteStartElement("date");
            writer.WriteString(review.CreationDate.ToString("dd-MMM-yyyy"));
            writer.WriteEndElement();

            writer.WriteStartElement("content");
            writer.WriteString(review.Text);
            writer.WriteEndElement();

            WriteReviewedBookInfo(writer, review);

            writer.WriteEndElement();
        }
        private static void WriteReviewedBookInfo(XmlTextWriter writer, Review review)
        {
            writer.WriteStartElement("book");

            Book bookReviewed = review.Book;
            List<Author> bookAuthors = bookReviewed.Authors.OrderBy(a => a.Name).ToList();
            StringBuilder authorNamesBuffer = new StringBuilder();
            FillStringBuilderWithCommaSeparatedAuthors(bookAuthors, authorNamesBuffer);

            writer.WriteStartElement("title");
            writer.WriteString(bookReviewed.Title);
            writer.WriteEndElement();

            if (bookReviewed.Authors.Count > 0)
            {
                writer.WriteStartElement("authors");
                writer.WriteString(authorNamesBuffer.ToString());
                writer.WriteEndElement();
            }

            if (!string.IsNullOrEmpty(bookReviewed.ISBN))
            {
                writer.WriteStartElement("isbn");
                writer.WriteString(bookReviewed.ISBN);
                writer.WriteEndElement();
            }

            if (!string.IsNullOrEmpty(bookReviewed.Website))
            {
                writer.WriteStartElement("url");
                writer.WriteString(bookReviewed.Website);
                writer.WriteEndElement();
            }

            writer.WriteEndElement();
        }
        public static void AddComplexBook(string title, List<string> authors, List<Tuple<string, string, string>> reviews, string isbn, string price, string website)
        {
            BookstoreEntities context = new BookstoreEntities();
            Book newBook = new Book();
            newBook.Title = title;

            if (authors.Count > 0)
            {
                foreach (var authorName in authors)
                {
                    Author author = CreateOrLoadAuthor(context, authorName);
                    newBook.Authors.Add(author);
                }
            }

            if (reviews.Count > 0)
            {
                foreach (var review in reviews)
                {
                    Review newReview = new Review();
                    newReview.ReviewContent = review.Item1;

                    if (review.Item2 != null)
                    {
                        newReview.Author = CreateOrLoadAuthor(context, review.Item2);
                    }

                    newReview.DateOfCreation = DateTime.Parse(review.Item3);
                    newBook.Reviews.Add(newReview);
                }
            }

            if (isbn != null)
            {
                newBook.ISBN = isbn;
            }

            if (price != null)
            {
                newBook.Price = decimal.Parse(price);
            }

            if (website != null)
            {
                newBook.OfficialWebsite = website;
            }

            context.Books.Add(newBook);
            try
            {
                context.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Debug.WriteLine("Property: {0} Error: {1}",
                                   validationError.PropertyName, validationError.ErrorMessage);
                    }
                }
            }
        }
        public List<Review> FindReviewsByPeriod(BookstoreEntities context, 
            DateTime startDate, DateTime endDate)
        {
            IQueryable<Review> reviewsQuery = context.Reviews.Where(
                r => r.CreationDate >= startDate && r.CreationDate <= endDate);
            Review rev = new Review();

            reviewsQuery = reviewsQuery.OrderBy(r => r.Text).OrderBy(r => r.CreationDate);

            return reviewsQuery.ToList();
        }
        private static Review CreateReview(BookstoreEntities context, string review)
        {
            Review newReview = new Review();

            newReview.Notes=review;

            context.Reviews.Add(newReview);
            context.SaveChanges();

            return newReview;
        }