public ActionResult NewBook(BookEntity bookEntity) { using (LibraryContext db = new LibraryContext()) { Book book = new Book(); int authorId = BookService.GetAuthorIdByName(bookEntity.Author); int genreId = BookService.GetGanreIdByName(bookEntity.Genre); if (authorId == 0) { Author newAuthor = new Author(); newAuthor.Name = bookEntity.Author; db.Authors.Add(newAuthor); db.SaveChanges(); } if (genreId == 0) { Genre newGenre = new Genre(); newGenre.name = bookEntity.Genre; db.Genres.Add(newGenre); db.SaveChanges(); } book = BookService.GetBook(bookEntity); db.Books.Add(book); db.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult EditBook(BookEntity bookEntity) { Book book = BookService.GetBook(bookEntity); using (LibraryContext db = new LibraryContext()) { db.Books.Remove((Book)db.Books.Find(book.Id)); db.Books.Add(book); db.SaveChanges(); } return(RedirectToAction("Index")); }
public JsonResult GetBooK(int id) { var book = _bookService.GetBook(id); return(Json(book, JsonRequestBehavior.AllowGet)); }