Пример #1
0
 public static void Main(string[] args)
 {
     var bookstore = new BookstoreEntities();
     var import = new Import(bookstore);
     import.LoadDocument("../../complex-books.xml");
     import.BooksComplexParse();
 }
        public static List<Book> SimpleSearch(string title, string author, string isbn)
        {
            using (BookstoreEntities dbContext = new BookstoreEntities())
            {
                //Create query
                IQueryable<Book> booksFound = dbContext.Books;

                if (title != null)
                {
                    booksFound = booksFound.Where(b => b.Title.ToLower() == title.ToLower());
                }

                if (isbn != null)
                {
                    long isbnNumber = long.Parse(isbn);
                    booksFound = booksFound.Where(b => b.ISBN == isbnNumber);
                }

                if (author != null)
                {
                    booksFound = booksFound.Where(b => b.Authors.Any(a => a.Name.ToLower() == author));
                }

                //Order result
                booksFound = booksFound.OrderBy(b => b.Title);

                //Return list of SimpleBookInfo objects
                return booksFound.ToList();
            }
        }
        //Creating a single result-set in the output xml file
        private static void RenderReviewsToXml(IList<Review> reviewsToRender, XmlTextWriter xmlWriter, BookstoreEntities dbContext)
        {
            xmlWriter.WriteStartElement("result-set");
            foreach (var review in reviewsToRender)
            {
                xmlWriter.WriteStartElement("review");
                xmlWriter.WriteElementString("date", String.Format("{0:dd-MMM-yyyy}", review.Creation_Date));
                xmlWriter.WriteElementString("content", review.Text);
                xmlWriter.WriteStartElement("book");
                Book book = review.Book;
                xmlWriter.WriteElementString("title", book.Title);

                if (book.Authors.Count > 0)
                {
                    xmlWriter.WriteElementString("authors", string.Join(", ", book.Authors.Select(a => a.Name).OrderBy(n => n).ToList()));
                }

                if (book.ISBN != null)
                {
                    xmlWriter.WriteElementString("isbn", book.ISBN.ToString());
                }

                if (book.Web_Site != null)
                {
                    xmlWriter.WriteElementString("url", book.Web_Site);
                }
                xmlWriter.WriteEndElement();
                xmlWriter.WriteEndElement();
            }
            xmlWriter.WriteEndElement();
        }
Пример #4
0
 public static void Main(string[] args)
 {
     var bookstore = new BookstoreEntities();
     var parse = new Import(bookstore);
     parse.LoadDocument("../../simple-books.xml");
     parse.SimpleBooksParse();
 }
Пример #5
0
        public static void Main()
        {
            var query = XDocument.Load("../../simple-query.xml");

            var titleElement = query.Root.Element("title");
            var authorElement = query.Root.Element("author");
            var isbnElement = query.Root.Element("isbn");

            var context = new BookstoreEntities();
            var books = context.Books.AsQueryable();
            if (titleElement != null)
            {
                books = books.Where(b => b.Title == titleElement.Value);
            }

            if (authorElement != null)
            {
                books = books.Where(b => b.Authors.Any(a => a.Name == authorElement.Value));
            }

            if (isbnElement != null)
            {
                books = books.Where(b => b.ISBN == isbnElement.Value);
            }

            var booksMinData = books.Select(b => new
              {
                  b.Title,
                  ReviewsCount = b.Reviews.Count()
              }).ToList();


            if (!booksMinData.Any())
            {
                Console.WriteLine("Nothing found");
            }
            else
            {
                Console.WriteLine("{0} books found", booksMinData.Count());

                foreach (var book in booksMinData)
                {
                    if (book.ReviewsCount > 0)
                    {
                        Console.WriteLine("{0} --> {1} ", book.Title, book.ReviewsCount);
                    }
                    else
                    {
                        Console.WriteLine("{0} --> No reviews ", book.Title);
                    }
                }
            }
        }
        public static void AddBookComplex(string title, List<string> authors, long? isbn, decimal? price, string webSite, List<ReviewStruct> reviews)
        {
            using (BookstoreEntities dbContext = new BookstoreEntities())
            {
                //Create authors
                List<Author> bookAuthors = new List<Author>();
                foreach (var authorName in authors)
                {
                    Author author = CreateOrLoadAuthor(authorName, dbContext);
                    bookAuthors.Add(author);
                }

                //Create reviews
                List<Review> bookReviews = new List<Review>();
                foreach (var reviewInfo in reviews)
                {
                    Review review = new Review();
                    if (reviewInfo.AuthorName != null)
                    {
                        review.Author = CreateOrLoadAuthor(reviewInfo.AuthorName, dbContext);
                    }
                    review.Creation_Date = reviewInfo.CreationDate;
                    review.Text = reviewInfo.Text;

                    bookReviews.Add(review);
                }

                CheckIsbnUniqueness(isbn, dbContext);

                //Create book
                Book newBook = new Book() { Title = title, ISBN = isbn, Price = price, Web_Site = webSite };
                foreach (var author in bookAuthors)
                {
                    newBook.Authors.Add(author);
                }

                foreach (var review in bookReviews)
                {
                    newBook.Reviews.Add(review);
                }

                //Save book
                dbContext.Books.Add(newBook);
                dbContext.SaveChanges();
            }
        }
        private static void PerformSearch(XmlTextWriter xmlWriter)
        {
            using (BookstoreEntities dbContext = new BookstoreEntities())
            {
                //Open xml file
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("../../reviews-queries.xml");

                //Create query to xml file
                string xPathQuery = "/review-queries/query";
                XmlNodeList queryList = xmlDoc.SelectNodes(xPathQuery);
                foreach (XmlNode queryNode in queryList)
                {
                    //***Save query to Logs - task 7***
                    SaveQueryToLogs(queryNode.OuterXml);

                    //Find reviews for current query
                    List<Review> reviewsFound = new List<Review>();
                    string type = queryNode.Attributes["type"].Value;
                    if (type == "by-period")
                    {
                        DateTime startDate = DateTime.Parse(queryNode.SelectSingleNode("start-date").InnerText);
                        DateTime endDate = DateTime.Parse(queryNode.SelectSingleNode("end-date").InnerText);
                        reviewsFound = BookstoreDAL.FinReviewsByDate(startDate, endDate, dbContext);
                    }
                    else
                    {
                        string authorName = queryNode.SelectSingleNode("author-name").InnerText;
                        reviewsFound = BookstoreDAL.FinReviewsByAuhtor(authorName, dbContext);
                    }

                    //Sort the reviews 
                    var reviewsToRender = reviewsFound.OrderBy(r => r.Creation_Date).ThenBy(r => r.Text).ToList();

                    //Render reviews for current query
                    RenderReviewsToXml(reviewsToRender, xmlWriter, dbContext);
                }
            }
        }
        public static void AddBook(string title, string authorName, long? isbn, decimal? price)
        {
            using (BookstoreEntities dbContext = new BookstoreEntities())
            {
                Author author = CreateOrLoadAuthor(authorName, dbContext);
                CheckIsbnUniqueness(isbn, dbContext);

                Book newBook = new Book() { Title = title, ISBN = isbn, Price = price };
                newBook.Authors.Add(author);

                dbContext.Books.Add(newBook);
                dbContext.SaveChanges();
            }
        }
 public static List<Review> FinReviewsByAuhtor(string authorName, BookstoreEntities dbContext)
 {
     List<Review> reviews = dbContext.Reviews.Where(r => r.Book.Authors.Any(a => a.Name == authorName)).ToList();
     return reviews;
 }
 public static List<Review> FinReviewsByDate(DateTime startDate, DateTime endDate, BookstoreEntities dbContext)
 {
     List<Review> reviews = dbContext.Reviews.Where(r => r.Creation_Date >= startDate && r.Creation_Date <= endDate).ToList();
     return reviews;
 }
        private static Author CreateOrLoadAuthor(string authorName, BookstoreEntities dbContext)
        {
            Author author = dbContext.Authors.FirstOrDefault(a => a.Name == authorName);

            if (author == null)
            {
                author = new Author() { Name = authorName };
            }

            dbContext.SaveChanges();
            return author;
        }
 private static void CheckIsbnUniqueness(long? isbn, BookstoreEntities dbContext)
 {
     Book book = dbContext.Books.FirstOrDefault(b => b.ISBN == isbn);
     if (book != null)
     {
         throw new DuplicateIsbnException(String.Format("Book with isbn {0} already exists", isbn));
     }
 }
        public static int GetReviewCount(int bookId)
        {
            using (BookstoreEntities dbContext = new BookstoreEntities())
            {
                int reviewCount = 0;
                Book book = dbContext.Books.Where(b => b.BookId == bookId).FirstOrDefault();
                if (book != null)
                {
                    reviewCount = book.Reviews.Count;
                }

                return reviewCount;
            }
        }
Пример #14
0
 public Import(BookstoreEntities entities)
 {
     Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
     this.entities = entities;
 }