public async Task <IActionResult> OnPost(int?bookId) { if (!ModelState.IsValid) { return(Page()); } TempData["Message"] = "Book was saved"; if (bookId.HasValue) { var book = await _infoContext.BooksList.Include(a => a.AuthorInfo).SingleAsync(b => b.Id == bookId); book.IBIN = Book.IBIN.Value; book.Title = Book.Title; book.Type = Book.Type.Value; book.AuthorInfoId = Book.AuthorId; await _infoContext.SaveChangesAsync(); return(RedirectToPage("./Detail", new { bookId })); } else { var newBook = new Books(Book.IBIN.Value, Book.Title, Book.Type.Value, Book.AuthorId); _infoContext.Add(newBook); await _infoContext.SaveChangesAsync(); return(RedirectToPage("./Detail", new { bookId = newBook.Id })); } }
public async Task <IActionResult> OnPost(int?authorId) { if (!ModelState.IsValid) { return(Page()); } TempData["Message"] = "Author was saved"; if (!authorId.HasValue) { var newAuthor = new AuthorInfo(Author.FirstName, Author.LastName); _booksInfo.Add(newAuthor); await _booksInfo.SaveChangesAsync(); } return(RedirectToPage("./AuthorsList")); }
public async Task <IActionResult> OnPost(int bookId) { var book = await _booksInfoContext.BooksList.SingleAsync(b => b.Id == bookId); if (book != null) { _booksInfoContext.Remove(book); await _booksInfoContext.SaveChangesAsync(); } else { return(RedirectToPage("./NotFound")); } TempData["Message"] = $"{book.Title} deleted"; return(RedirectToPage("./List")); }
public async Task <IActionResult> OnPost(int authorId) { if (!ModelState.IsValid) { return(Page()); } TempData["Message"] = "Author was saved"; var author = await _infoContext.AuthorsList.SingleAsync(a => a.Id == authorId); author.FirstName = Author.FirstName; author.LastName = Author.LastName; await _infoContext.SaveChangesAsync(); return(RedirectToPage("./AuthorDetails")); }
public async Task <IActionResult> OnPost(int authorId) { Author = await _infoContext.AuthorsList.SingleAsync(a => a.Id == authorId); var book = await _infoContext.BooksList.AnyAsync(b => b.AuthorInfoId == authorId); if (book == false) { _infoContext.Remove(Author); await _infoContext.SaveChangesAsync(); } else { TempData["Message"] = $"{Author.Id} can't be deleted"; return(RedirectToPage("./AuthorDetails")); } TempData["Message"] = $"{Author.FirstName} + {Author.LastName} was deleted"; return(RedirectToPage("./AuthorsList")); }