Ejemplo n.º 1
0
        public static void AddBook(XmlNodeList authors, XmlNodeList reviews,
            string title, string webSite, decimal price, string isbn)
        {
            if (title == null)
            {
                throw new ArgumentNullException("BookTitle must not be null");
            }

            BookStoreDB context = new BookStoreDB();

            Book newBook = new Book();
            foreach (XmlNode author in authors)
            {
                string authorName = author.InnerText;
                newBook.Authors.Add(CreateOrLoadAuthor(context, authorName));
            }

            foreach (XmlNode review in reviews)
            {
                string authorName = null;
                XmlAttributeCollection atributes = review.Attributes;
                string reviewText = review.InnerText.Trim();
                if (review.Attributes.GetNamedItem("author") != null)
                {
                    authorName  = review.Attributes.GetNamedItem("author").Value;
                }
                string date = "";
                if (review.Attributes.GetNamedItem("date") != null)
                {
                     date = review.Attributes["date"].Value;
                }
                DateTime parsed;
                string dateFormat = "d-MMM-yyyy";
                  DateTime.TryParseExact(date, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out parsed);
               
                newBook.Reviews.Add(CreateReview(context, parsed, authorName, reviewText));
            }
            newBook.Title = title;
            newBook.Website = webSite;
            newBook.Price = price;
            newBook.Isbn = isbn;

            context.Books.Add(newBook);
            context.SaveChanges();
        }
Ejemplo n.º 2
0
 // Task 1 - 2 are in Code First aproach and I em using DataAnotattions Atributes for adding constraints
 public static void AddBook(string author,
     string title, string webSite, decimal price, string isbn)
 {
     if (author == null || title == null)
     {
         throw new ArgumentNullException("BookTitle and BookAuthor(s) must not be null");
     }
     BookStoreDB context = new BookStoreDB();
     Book newBook = new Book();
     newBook.Authors.Add(CreateOrLoadAuthor(context, author));
     newBook.Title = title;
     newBook.Website = webSite;
     newBook.Price = price;
     newBook.Isbn = isbn;
   
     context.Books.Add(newBook);
     context.SaveChanges();
 }
Ejemplo n.º 3
0
        private static void CreateAndAddBookInDb(
            BookStoreDBEntities db, string author, string title, string isbn, decimal price, string webSite)
        {
            ICollection<Author> bookAuthors = new List<Author>();

            // Check is title exist
            var titleInDb = (from bookDb in db.Books
                             where bookDb.BookTitle == title
                             select bookDb).FirstOrDefault();

            var authorInDb = (from authorDb in db.Authors
                              where authorDb.AuthorName == author
                              select authorDb).FirstOrDefault();

            if (titleInDb != null && authorInDb != null)
            {
                return;
            }

            if (authorInDb == null)
            {
                Author newAuthor = new Author() { AuthorName = author };
                db.Authors.Add(newAuthor);
                db.SaveChanges();
                bookAuthors.Add(newAuthor);

                if (titleInDb != null)
                {
                    titleInDb.Authors.Add(newAuthor);
                }
            }

            Book newBook = new Book() { Authors = bookAuthors, BookTitle = title };

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

                var isbnInDb = (from b in db.Books
                                where b.ISBN == isbn
                                select b).FirstOrDefault();

                if (isbnInDb != null)
                {
                    throw new ApplicationException("Duplicated ISBN");
                }
            }

            if (price != 0m)
            {
                newBook.Price = price;
            }

            if (webSite != null)
            {
                newBook.WebSite = webSite;
            }

            db.Books.Add(newBook);
            db.SaveChanges();
        }