// Update row public bool Update(AuthorsBooks authorBook, int bookId) { bool result = false; using (var db = new LibData()) { var selectedAuthorBook = db.AuthorsBooks.Where(s => s.BookID == bookId).FirstOrDefault(); selectedAuthorBook.AuthorID = authorBook.AuthorID; selectedAuthorBook.BookID = authorBook.BookID; if (db.SaveChanges() != 0) { result = true; } else { result = false; } } if (result == true) { return(true); } return(false); }
public ActionResult AddBook(string title, int copies, string authors) { Book newBook = new Book(title, copies); newBook.Save(); string[] lotOfAuthors = authors.Split(','); foreach (var author in lotOfAuthors) { Author newAuthor = new Author(author.TrimStart()); newAuthor.Save(); newBook.AttributeTo(newAuthor); } AuthorsBooks allAuthorsBooks = new AuthorsBooks(); return(View("Catalogue", allAuthorsBooks)); }
// insert into row public bool Add(AuthorsBooks authorBook) { bool result = false; using (var db = new LibData()) { db.AuthorsBooks.Add(authorBook); if (db.SaveChanges() != 0) { result = true; } else { result = false; } } if (result == true) { return(true); } return(false); }
public IActionResult Catalogue() { AuthorsBooks allAuthorsBooks = new AuthorsBooks(); return(View(allAuthorsBooks)); }
// Add or Update private void button3_Click(object sender, EventArgs e) { if (isValid()) { if (thisBook == null) { Categories categ = cmbCathegory.SelectedItem as Categories; int bookID = books.Add(new Books(txtName.Text, Convert.ToDouble(txtPrice.Text), categ.id, Convert.ToInt32(txtCount.Text))); if (bookID > 0) { foreach (Authors item in listBox1.SelectedItems) { AuthorsBooks authorsBooks = new AuthorsBooks(); authorsBooks.BookID = bookID; authorsBooks.AuthorID = item.id; if (authorsBooksDB.Add(authorsBooks)) { txtInfo.Text = "Book added"; } else { txtInfo.Text = "Error: Author is not added"; } } } else { txtInfo.Text = "Error: Book is not added"; } } else { // Updating Categories categ = cmbCathegory.SelectedItem as Categories; if (books.Update(new Books(txtName.Text, Convert.ToDouble(txtPrice.Text), categ.id, Convert.ToInt32(txtCount.Text)), thisBook.id)) { txtInfo.Text = "Book is Updated!"; } else { txtInfo.Text = "Warning: book is not updated!"; } // Update all authors authorsBooksDB.DeleteRows(thisBook.id); // first remove all authors foreach (Authors item in listBox1.SelectedItems) { AuthorsBooks authorsBooks = new AuthorsBooks(); authorsBooks.BookID = thisBook.id; authorsBooks.AuthorID = item.id; if (authorsBooksDB.Add(authorsBooks)) { txtInfo.Text = "Book is Updated!"; } } } ResetForm(); button3.Text = "Save"; } }