public ActionResult Addnew(BookDetail book) { try { // TODO: Add insert logic here dbmodel.BookDetails.Add(book); dbmodel.SaveChanges(); return(RedirectToAction("Index")); } //else //{ // // ViewBag.Error = TempData["Year should be between 1500 and " + Convert.ToInt32(book.Year)]; // // TempData["error"]="Year should be between 1500 and " + Convert.ToInt32(book.Year); // // Response.Write("Year should be between 1500 and " + Convert.ToInt32(book.Year)); // //("Year should be between 1500 and " + Convert.ToInt32(book.Year)); // return RedirectToAction("Index"); //} // } catch { return(View()); } }
public ActionResult Create([Bind(Include = "BCategoryID,BCategoryName")] Category category) { if (ModelState.IsValid) { db.Categories.Add(category); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(category)); }
public ActionResult Create([Bind(Include = "BName,BDesc,BCategoryID")] Book book) { if (ModelState.IsValid) { db.Books.Add(book); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(book)); }
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(); }
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(); } }
/// <summary> /// Created By : Ashwajit Bansod /// Created For : To save and update book data by using book id /// </summary> /// <param name="obj"></param> /// <returns></returns> public bool SaveBookData(BookListModel obj) { bool isSave = false; try { if (obj.BookId == 0) { var tableData = new Tbl_BookStore(); tableData.BookName = obj.BookName; tableData.BookPic = obj.BookPic; tableData.BookPrice = obj.BookPrice; tableData.BookISBN = obj.BookISBN; tableData.BookPublication = obj.BookPublication; tableData.BookDate = obj.BookDate; tableData.AuthorName = obj.AuthorName; tableData.BookDescription = obj.BookDescription; tableData.IsDelete = false; _db.Tbl_BookStore.Add(tableData); _db.SaveChanges(); isSave = true; } else { var getData = _db.Tbl_BookStore.Where(x => x.BookId == obj.BookId).FirstOrDefault(); getData.BookName = obj.BookName; getData.BookPic = obj.BookPic; getData.BookPrice = obj.BookPrice; getData.BookISBN = obj.BookISBN; getData.BookPublication = obj.BookPublication; getData.BookDate = obj.BookDate; getData.AuthorName = obj.AuthorName; getData.BookDescription = obj.BookDescription; getData.IsDelete = false; _db.Entry(getData).State = EntityState.Modified; _db.SaveChanges(); isSave = true; } } catch (Exception ex) { isSave = false; throw; } return(isSave); }
private static void ParseAndSaveBook(BookstoreEntities bookstoreContext, XPathNodeIterator iterator) { using (var scope = new TransactionScope()) { XPathNavigator currentNode = iterator.Current; XPathNavigator bookAuthorNode = currentNode.SelectSingleNode("author"); XPathNavigator bookTitleNode = currentNode.SelectSingleNode("title"); XPathNavigator bookIsbnNode = currentNode.SelectSingleNode("isbn"); XPathNavigator bookPriceNode = currentNode.SelectSingleNode("price"); XPathNavigator bookWebSiteNode = currentNode.SelectSingleNode("web-site"); string authorName = Utils.GetNodeValue(bookAuthorNode); if (authorName == null) { throw new XPathException("Book author is a required tag."); } string bookIsbn = Utils.GetNodeValue(bookIsbnNode); string bookTitle = Utils.GetNodeValue(bookTitleNode); if (bookTitle == null) { throw new XPathException("Book title is a required tag."); } string bookPriceAsString = Utils.GetNodeValue(bookPriceNode); decimal?bookPrice = null; if (bookPriceAsString != null) { bookPrice = decimal.Parse(bookPriceAsString); } string bookWebSite = Utils.GetNodeValue(bookWebSiteNode); var author = Utils.GetAuthor(bookstoreContext, bookAuthorNode); var book = new Book { BookTitle = bookTitle, BookISBN = bookIsbn, BookPrice = bookPrice, BookWebSite = bookWebSite }; book.Authors.Add(author); bookstoreContext.Books.Add(book); bookstoreContext.SaveChanges(); scope.Complete(); } }
public ActionResult DeleteBook(int id) { Book target = null; using (BookstoreEntities dbContext = new BookstoreEntities()) { target = dbContext.Book.SingleOrDefault(t => t.Id == id); target.status = false; dbContext.Entry(target).State = System.Data.Entity.EntityState.Modified; dbContext.SaveChanges(); } return(RedirectToAction("BookList", new { pageNumber = 1 })); }
public ActionResult EditBook(Book book) { using (BookstoreEntities dbContext = new BookstoreEntities()) { if (this.Request.Files != null && this.Request.Files.Count > 0 && this.Request.Files[0].ContentLength > 0) { string fileName = Path.GetFileName(this.Request.Files[0].FileName); string filePathOfWebsite = "~/Images/Covers/" + fileName; book.CoverImagePath = filePathOfWebsite; this.Request.Files[0].SaveAs(this.Server.MapPath(filePathOfWebsite)); } dbContext.Entry(book).State = System.Data.Entity.EntityState.Modified; dbContext.SaveChanges(); } return(RedirectToAction("BookList", new { pageNumber = 1 })); }
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); }
private static Author CreateOrLoadAuthor(BookstoreEntities dbContext, string authorName) { Author existingAuthor = dbContext.Authors.Where(a => a.Name == authorName.ToLower()).FirstOrDefault(); if (existingAuthor != null) { return(existingAuthor); } else { Author newAuthor = new Author() { Name = authorName.ToLower() }; dbContext.Authors.Add(newAuthor); dbContext.SaveChanges(); return(newAuthor); } }
public static Author GetAuthor(BookstoreEntities bookstoreContext, XPathNavigator node) { string authorName = Utils.GetNodeValue(node); var author = bookstoreContext.Authors.FirstOrDefault(a => a.AuthorName == authorName); if (author == null) { author = new Author { AuthorName = authorName }; bookstoreContext.Authors.Add(author); bookstoreContext.SaveChanges(); } return(author); }
public static void ImportBooksAndAuthors(string bookTitle, string isbn, string author, decimal?price, string webSite) { using (BookstoreEntities dbContext = new BookstoreEntities()) { Book newBook = new Book() { Title = bookTitle.ToLower(), ISBN = isbn, Price = price, website = webSite, }; Author bookAutor = CreateOrLoadAuthor(dbContext, author); newBook.Authors.Add(bookAutor); dbContext.Books.Add(newBook); dbContext.SaveChanges(); } }
public static void ImportBooksAuthorsAndReviews(string bookTitle, string isbn, decimal?price, string webSite, List <string> authorsNames, List <ReviewData> reviewsData) { using (BookstoreEntities dbContext = new BookstoreEntities()) { Book newBook = new Book { Title = bookTitle.ToLower(), ISBN = isbn, Price = price, website = webSite, }; foreach (string authorName in authorsNames) { Author bookAutor = CreateOrLoadAuthor(dbContext, authorName); newBook.Authors.Add(bookAutor); } foreach (ReviewData reviewData in reviewsData) { Review newReview = new Review() { Content = reviewData.Content, CreationDate = reviewData.Date, }; if (reviewData.AuthorName != null) { newReview.Author = CreateOrLoadAuthor(dbContext, reviewData.AuthorName); } newBook.Reviews.Add(newReview); } dbContext.Books.Add(newBook); dbContext.SaveChanges(); } }
private static void ParseAndSaveBook(BookstoreEntities bookstoreContext, XPathNodeIterator iterator) { using (var scope = new TransactionScope()) { XPathNavigator currentNode = iterator.Current; XPathNavigator bookTitleNode = currentNode.SelectSingleNode("title"); XPathNavigator bookAuthorsNode = currentNode.SelectSingleNode("authors"); XPathNavigator bookWebSiteNode = currentNode.SelectSingleNode("web-site"); XPathNavigator bookReviewsNode = currentNode.SelectSingleNode("reviews"); XPathNavigator bookIsbnNode = currentNode.SelectSingleNode("isbn"); XPathNavigator bookPriceNode = currentNode.SelectSingleNode("price"); string bookTitle = Utils.GetNodeValue(bookTitleNode); if (bookTitle == null) { throw new XPathException("Book title is a required tag."); } var book = new Book(); book.BookTitle = bookTitle; if (bookAuthorsNode != null) { var authorsIterator = bookAuthorsNode.SelectChildren(XPathNodeType.Element); while (authorsIterator.MoveNext()) { XPathNavigator authorNode = authorsIterator.Current; var author = Utils.GetAuthor(bookstoreContext, authorNode); book.Authors.Add(author); } } string bookWebSite = Utils.GetNodeValue(bookWebSiteNode); book.BookWebSite = bookWebSite; string bookIsbn = Utils.GetNodeValue(bookIsbnNode); book.BookISBN = bookIsbn; string bookPriceAsString = Utils.GetNodeValue(bookPriceNode); decimal?bookPrice = null; if (bookPriceAsString != null) { bookPrice = decimal.Parse(bookPriceAsString); } book.BookPrice = bookPrice; bookstoreContext.Books.Add(book); bookstoreContext.SaveChanges(); if (bookReviewsNode != null) { var reviewsIterator = bookReviewsNode.SelectChildren(XPathNodeType.Element); while (reviewsIterator.MoveNext()) { var bookReview = new BookReview(); XPathNavigator reviewNode = reviewsIterator.Current; string reviewContents = Utils.GetNodeValue(reviewNode); bookReview.BookReviewContents = reviewContents; var reviewAuthorNode = reviewNode.SelectSingleNode("@author"); if (reviewAuthorNode != null) { var author = Utils.GetAuthor(bookstoreContext, reviewAuthorNode); bookReview.Author = author; } var reviewDateNode = reviewNode.SelectSingleNode("@date"); DateTime bookReviewDate = DateTime.Now; if (reviewDateNode != null) { bookReviewDate = Utils.GetDate(reviewDateNode); } bookReview.BookReviewDate = bookReviewDate; bookReview.Book = book; bookstoreContext.BookReviews.Add(bookReview); } bookstoreContext.SaveChanges(); } scope.Complete(); } }