private static void InsertData(XmlNodeList authors, string title, string webSite, string isbn, string priceStr, XmlNodeList reviews)
        {
            var context = new BookstoreEntities();
            using (context)
            {
                Book book = new Book
                {
                    Isbn = isbn,
                    Title = title,
                    Website = webSite,
                };

                if (CheckForExistingBook(context, book))
                {
                    throw new ArgumentException("The book with isbn: {0} already exist!", isbn);
                }

                if (priceStr != null)
                {
                    book.Price = decimal.Parse(priceStr);
                }

                if (authors != null)
                {
                    foreach (XmlNode authorName in authors)
                    {
                        Author author = CreateOrLoadAuthor(context, authorName.InnerText);
                        book.Authors.Add(author);
                    }
                }

                if (reviews != null)
                {
                    foreach (XmlNode reviewNode in reviews)
                    {
                        Review review = new Review();
                        review.CreationDate = DateTime.Now;
                        if (reviewNode.Attributes["date"] != null)
                        {
                            string date = reviewNode.Attributes["date"].Value;
                            review.CreationDate = DateTime.ParseExact(date, "d-MMM-yyyy", CultureInfo.InvariantCulture);
                        }
                        if (reviewNode.Attributes["author"] != null)
                        {
                            string authorStr = reviewNode.Attributes["author"].Value;
                            Author author = CreateOrLoadAuthor(context, authorStr);
                            review.AuthorId = author.AuthorId;
                        }

                        review.ReviewText = reviewNode.InnerText;
                        book.Reviews.Add(review);
                    }
                    
                }


                context.Books.Add(book);
                context.SaveChanges();
            }
        }
Exemplo n.º 2
0
        private static Author CreateOrLoadAuthor(BookstoreEntities context, string authorName)
        {
            var user = context.Authors.FirstOrDefault(x => x.Name == authorName);
            if (user != null)
            {
                return user;
            }

            var newAuthor = new Author
            {
                Name = authorName
            };

            context.Authors.Add(newAuthor);
            context.SaveChanges();
            return newAuthor;
        }
Exemplo n.º 3
0
        public static void ExecureQuery(string filePath)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(filePath);
            string pathQuery = "/query";

            XmlNode xmlQuery = xmlDocument.SelectSingleNode(pathQuery);


            string title = xmlQuery.GetInnerText("title");
            string author = xmlQuery.GetInnerText("author");
            string isbn = xmlQuery.GetInnerText("isbn");

            BookstoreEntities context = new BookstoreEntities();
            var booksQuery =
                from b in context.Books
                select b;

            if (title != null)
            {
                booksQuery = context.Books.Where(x => x.Title == title);
            }

            if (author != null)
            {
                booksQuery = booksQuery.Where(x => x.Authors.Any(y => y.Name.ToLower() == author.ToLower()));
            }

            if (isbn != null)
            {
                booksQuery = booksQuery.Where(x => x.Isbn == isbn);
            }

            booksQuery = booksQuery.OrderBy(x => x.Title);

            PrintBooksOnConsole(booksQuery.ToList());
        }
Exemplo n.º 4
0
        private static void InsertData(string authorName, string title, string webSite, string isbn, string priceStr)
        {
            var context = new BookstoreEntities();
            Author author = CreateOrLoadAuthor(context, authorName);

            Book book;
            book = new Book
            {
                Isbn = isbn,
                Title = title,
                Website = webSite,
            };

            if (priceStr != null )
            {
                book.Price = decimal.Parse(priceStr);
            }

            
            book.Authors.Add(author);

            context.Books.Add(book);
            context.SaveChanges();
        }
        public static void ExecureQuery(string filePath)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(filePath);
            string pathQuery = "/review-queries/query";

            XmlNodeList xmlQuery = xmlDocument.SelectNodes(pathQuery);


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

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

                BookstoreEntities context = new BookstoreEntities();
                foreach (XmlNode query in xmlQuery)
                {
                    if (query.Attributes["type"].Value == "by-period")
                    {
                        string startDateStr = query["start-date"].InnerText;
                        string endDateStr = query["end-date"].InnerText;

                        DateTime startDate = DateTime.ParseExact(startDateStr, "d-MMM-yyyy", CultureInfo.InvariantCulture);
                        DateTime endDate = DateTime.ParseExact(endDateStr, "d-MMM-yyyy", CultureInfo.InvariantCulture);


                        var searchQuery =
                            from r in context.Reviews
                            where r.CreationDate >= startDate && r.CreationDate <= endDate
                            select r;

                        searchQuery = searchQuery.OrderByDescending(x => x.CreationDate);
                        searchQuery = searchQuery.OrderByDescending(x => x.ReviewText);

                        WriteBookmarks(writer, searchQuery.ToList());
                    }
                    else
                    {
                        string authorName = query["author-name"].InnerText;

                        var searchQuery =
                            from r in context.Reviews
                            join a in context.Authors on r.AuthorId equals a.AuthorId
                            where a.Name == authorName
                            select r;

                        searchQuery = searchQuery.OrderByDescending(x => x.CreationDate);
                        searchQuery = searchQuery.OrderByDescending(x => x.ReviewText);

                        WriteBookmarks(writer, searchQuery.ToList());
                    }

                    //Create log
                    var logsContext = new LogsContext();
                    Log log = new Log
                        {
                            Date = DateTime.Now,
                            QueryXml = CreateQueryXml(query)
                        };
                    logsContext.Logs.Add(log);
                    logsContext.SaveChanges();
                }

                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
        }
        //TODO: FIX
        private static bool CheckForExistingBook(BookstoreEntities context, Book book)
        {
            if (context.Books.Any(x => x.Isbn == book.Isbn))
            {
                return true;
            }

            return false;
        }