public static void BookDelete(int bookId, string path) { using (var library = new MyLibraryContext()) { var book = from b in library.Books where b.BookId == bookId select b; library.Books.Remove(book.First()); library.SaveChanges(); } //prevent deleting noimage.jpg file if (File.Exists(path) && path != "") { File.Delete(path); } }
public static void BookSave(string title, string authorName, string authorSurname, int genreId, HttpPostedFileBase imageUrlFile) { string imageUrl = GetImageUrl(imageUrlFile); using (MyLibraryContext dbLibrary = new MyLibraryContext()) { int authorId = GetAuthorId(authorName, authorSurname); //create and save new book var newBook = new Book(); newBook.Title = title; newBook.AuthorId = authorId; newBook.GenreId = genreId; newBook.ImageUrl = imageUrl; dbLibrary.Books.Add(newBook); dbLibrary.SaveChanges(); } }
public static void BookUpdate(int bookId, string title, string authorName, string authorSurname, int genreId, HttpPostedFileBase imageUrlFile) { using (MyLibraryContext dbLibrary = new MyLibraryContext()) { int authorId = GetAuthorId(authorName, authorSurname); //find book by id var book = from b in dbLibrary.Books where b.BookId == bookId select b; //update book Book bookToUpdate = book.First(); bookToUpdate.Title = title; bookToUpdate.AuthorId = authorId; bookToUpdate.GenreId = genreId; //if imageUrlFile == null we are keeping current cover, so don't include in update if (imageUrlFile != null) { bookToUpdate.ImageUrl = GetImageUrl(imageUrlFile); } dbLibrary.SaveChanges(); } }