static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            BookstoreDAL dal = new BookstoreDAL();

            XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load("../../simple-books.xml");
            xmlDoc.Load("../../../tests/5/simple-books.xml");
            string xPathQuery = "/catalog/book";

            XmlNodeList books = xmlDoc.SelectNodes(xPathQuery);

            using (var context = new BookstoreEntities())
            {
                foreach (XmlNode bookNode in books)
                {
                    string title = GetChildText(bookNode, "title");
                    if (string.IsNullOrEmpty(title))
                    {
                        throw new ArgumentException("The Book <title> node is empty or missing",
                            "title");
                    }

                    string ISBN = GetChildText(bookNode, "isbn");
                    string priceString = GetChildText(bookNode, "price");
                    decimal? price = null;
                    if (priceString != null)
                    {
                        price = decimal.Parse(priceString);
                    }

                    string websiteUrl = GetChildText(bookNode, "web-site");

                    List<string> authorNames = new List<string>();
                    foreach (XmlNode authorNode in bookNode.SelectNodes("author"))
                    {
                        string authorName = authorNode.InnerText.Trim();
                        if (authorName == String.Empty)
                        {
                            throw new ArgumentException("Author name cannot be empty.");
                        }

                        authorNames.Add(authorName);
                    }

                    if (authorNames.Count == 0)
                    {
                        throw new ArgumentException("The <author> node is empty or missing.",
                            "author");
                    }

                    using (var tran = new TransactionScope())
                    {
                        dal.AddBook(context, authorNames, title, ISBN, price, websiteUrl);

                        tran.Complete();
                    }
                }
            }
        }
示例#2
0
        public static ICollection <Model.Review> GetReviews(Reviews reviews)
        {
            var result = new List <Model.Review>();

            if (reviews != null)
            {
                reviews.reviews.ForEach(r =>
                {
                    var review    = new Review();
                    review.Author = BookstoreDAL.GetAuthor(r.Author);

                    if (r.Text.StartsWith("\n"))
                    {
                        review.Text = r.Text.Remove(0, 2).Trim();
                    }
                    else
                    {
                        review.Text = r.Text;
                    }

                    if (r.CreatDate == null)
                    {
                        review.CreatDate = DateTime.Now;
                    }
                    else
                    {
                        review.CreatDate = DateTime.Parse(r.CreatDate);
                    }

                    result.Add(review);
                });
            }

            return(result);
        }
        private static List<Review> FindReviewsByReviewQuery(BookstoreDAL dal, 
            BookstoreEntities context, XmlNode query)
        {
            string type = GetChildText(query, "@type");

            List<Review> foundReviews = null;

            switch (type)
            {
                case "by-period":

                    string startDateString = GetChildText(query, "start-date");
                    string endDateString = GetChildText(query, "end-date");
                    DateTime startDate = DateTime.Parse(startDateString);
                    DateTime endDate = DateTime.Parse(endDateString);

                    foundReviews =
                        dal.FindReviewsByPeriod(context, startDate, endDate);

                    break;
                case "by-author":

                    string authorName = GetChildText(query, "author-name");

                    foundReviews =
                        dal.FindReviewsByAuthor(context, authorName);

                    break;
                default:
                    throw new ArgumentException("Invalid review query type. " +
                        "Must be 'by-period' or 'by-author'.");
            }
            return foundReviews;
        }
        public List<Book> FindBooksByTitleAuthorAndISBN(BookstoreEntities context, 
            BookstoreDAL bookstoreDAL, string bookTitle, string authorName, string ISBN)
        {
            if (string.IsNullOrEmpty(bookTitle) && string.IsNullOrEmpty(authorName) &&
                string.IsNullOrEmpty(ISBN))
            {
                return new List<Book>();
            }

            IQueryable<Book> booksQuery = context.Books.Include("Authors");
            Book book = new Book();

            if (!string.IsNullOrEmpty(bookTitle))
            {
                booksQuery = booksQuery.Where(
                    b => b.Title.ToLower() == bookTitle.ToLower());
            }

            if (!string.IsNullOrEmpty(authorName))
            {
                booksQuery = booksQuery.Where(
                    b => b.Authors.Any(a => a.Name.ToLower() == authorName.ToLower()));
            }

            if (!string.IsNullOrEmpty(ISBN))
            {
                booksQuery = booksQuery.Where(b => b.ISBN == ISBN);
            }

            booksQuery = booksQuery.OrderBy(b => b.Title);
            return booksQuery.ToList();
        }
 public int FindBookReviewCount(BookstoreEntities context,
     BookstoreDAL bookstoreDAL, Book book)
 {
     int bookReviewCount =
         context.Reviews.Include("Book").Where(r => r.BookId == book.Id).Count();
     return bookReviewCount;
 }
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            BookstoreDAL dataAccessLayer = new BookstoreDAL();

            XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load("../../complex-books.xml");
            //xmlDoc.Load(@"../../../tests/4/test-transaction.xml");
            xmlDoc.Load(@"../../../tests/5/complex-books.xml");
            string xPathQuery = "/catalog/book";

            XmlNodeList books = xmlDoc.SelectNodes(xPathQuery);

            using (var context = new BookstoreEntities())
            {
                foreach (XmlNode bookNode in books)
                {
                    string title = GetChildText(bookNode, "title");
                    if (string.IsNullOrEmpty(title))
                    {
                        throw new ArgumentException("The Book <title> node is empty or missing",
                            "title");
                    }

                    string ISBN = GetChildText(bookNode, "isbn");
                    string priceString = GetChildText(bookNode, "price");
                    decimal? price = null;
                    if (priceString != null)
                    {
                        price = decimal.Parse(priceString);
                    }

                    string websiteUrl = GetChildText(bookNode, "web-site");

                    List<string> authorNames = new List<string>();
                    foreach (XmlNode authorNode in bookNode.SelectNodes("authors/author"))
                    {
                        authorNames.Add(authorNode.InnerText);
                    }

                    Book addedBook =
                        dataAccessLayer.AddBook(context, authorNames, title,
                        ISBN, price, websiteUrl);

                    using (var tran = new TransactionScope())
                    {
                        foreach (XmlNode reviewNode in bookNode.SelectNodes("reviews/review"))
                        {
                            ProcessReviewNode(context, dataAccessLayer, addedBook, reviewNode);
                        }

                        tran.Complete();
                    }
                }
            }
        }
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            BookstoreDAL dal = new BookstoreDAL();

            XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load("../../reviews-queries.xml");
            xmlDoc.Load("../../../tests/6/performance-test.xml");
            string xPathQuery = "/review-queries/query";

            XmlNodeList queries = xmlDoc.SelectNodes(xPathQuery);

            using (var context = new BookstoreEntities())
            {
                using (var logContext = new SearchLogsContext())
                {
                    string searchResultsFileName = "../../reviews-search-results.xml";
                    using (XmlTextWriter writer =
                        new XmlTextWriter(searchResultsFileName, Encoding.UTF8))
                    {
                        writer.Formatting = Formatting.Indented;
                        writer.IndentChar = '\t';
                        writer.Indentation = 1;

                        writer.WriteStartDocument();
                        writer.WriteStartElement("search-results");

                        foreach (XmlNode query in queries)
                        {
                            writer.WriteStartElement("result-set");

                            // log the query (task 7):
                            //SearchLog newLog = new SearchLog();
                            //newLog.Date = DateTime.Now;
                            //newLog.QueryXml = query.InnerXml;
                            //logContext.SearchLogs.Add(newLog);

                            List<Review> foundReviews =
                                FindReviewsByReviewQuery(dal, context, query);

                            foreach (var review in foundReviews)
                            {
                                WriteReviewInfo(writer, review);
                            }

                            writer.WriteEndElement();
                        }

                        writer.WriteEndDocument();
                    }

                    logContext.SaveChanges();
                }
            }
        }
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
            BookstoreDAL dal = new BookstoreDAL();

            XmlDocument xmlDoc = new XmlDocument();
            //xmlDoc.Load("../../simple-query.xml");
            xmlDoc.Load("../../../tests/5/test6.xml");
            string xPathQuery = "/query";

            using (var context = new BookstoreEntities())
            {
                XmlNode query = xmlDoc.SelectSingleNode(xPathQuery);

                string title = GetChildText(query, "title");
                string author = GetChildText(query, "author");
                string ISBN = GetChildText(query, "isbn");

                List<Book> foundBooks = dal.FindBooksByTitleAuthorAndISBN(context,
                    dal, title, author, ISBN);

                int foundBooksCount = foundBooks.Count;
                if (foundBooksCount > 0)
                {
                    Console.WriteLine("{0} books found:", foundBooks.Count);
                    foreach (var book in foundBooks)
                    {
                        int numberOfReviews = dal.FindBookReviewCount(context, dal, book);
                        if (numberOfReviews > 0)
                        {
                            Console.WriteLine("{0} --> {1} reviews", book.Title, numberOfReviews);
                        }
                        else
                        {
                            Console.WriteLine("{0} --> no reviews", book.Title);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Nothing found");
                }
            }
        }
        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();
        }