public bool createBook(Book book, Genre genre, List <Author> authors, BookLocation location) { bool result = true; Genre g = context.Genres.FirstOrDefault(x => x.GenreName == genre.GenreName); List <Author> a = new List <Author>(); authors.ForEach(x => { a.Add(context.Authors.FirstOrDefault(y => y.AuthorName == x.AuthorName)); }); //context.Entry(a).State = EntityState.Unchanged; Book b = new Book() { BookName = book.BookName, BookEdition = book.BookEdition, BookISBN = book.BookISBN, BookPagination = book.BookPagination, Authors = a, Genre = g }; context.Books.Add(b); context.SaveChanges(); int id = b.BookId; Book b2 = context.Books.FirstOrDefault(x => x.BookId == id); BookLocation bl = context.BookLocations.FirstOrDefault(x => x.BookLocationPlace == location.BookLocationPlace); if (b2 != null) { BookExemplar be = new BookExemplar() { Book = b2, BookLocation = bl }; context.BookExemplars.Add(be); context.SaveChanges(); } else { result = false; } return(result); }
private void listBoxExemplars_SelectedValueChanged(object sender, EventArgs e) { var content = listBoxExemplars.SelectedItem; if (content != null) { string[] contentArray = content.ToString().Split('-'); this.bookExemplar = exemplars.Find(item => item.BookExemplarId == Int32.Parse(contentArray[1])); } else if (listBoxExemplars.Items.Count > 1) { listBoxExemplars.SelectedIndex = 0; } }
public bool updateBookExemplarByBookExemplar(BookExemplar bookExemplar) { context.SaveChanges(); BookExemplar oExemplar = context.BookExemplars.FirstOrDefault(item => item.BookExemplarId == bookExemplar.BookExemplarId); if (oExemplar == null) { return(false); } context.SaveChanges(); List <Author> nAuthors = new List <Author>(); bookExemplar.Book.Authors.ToList().ForEach(item => { Author a = context.Authors.FirstOrDefault(aut => aut.AuthorName == item.AuthorName); if (a != null) { nAuthors.Add(a); } }); oExemplar.BookLocation = context.BookLocations.FirstOrDefault(item => item.BookLocationPlace == bookExemplar.BookLocation.BookLocationPlace); context.SaveChanges(); Genre oGenre = context.Genres.FirstOrDefault(item => bookExemplar.Book.Genre.GenreName == item.GenreName); if (oGenre == null) { return(false); } Book oBook = context.Books.FirstOrDefault(item => item.BookId == bookExemplar.Book.BookId); if (oBook == null) { return(false); } oBook.BookEdition = bookExemplar.Book.BookEdition; oBook.BookISBN = bookExemplar.Book.BookISBN; oBook.BookPagination = bookExemplar.Book.BookPagination; oBook.BookName = bookExemplar.Book.BookName; oBook.Authors = nAuthors; oBook.Genre = oGenre; context.SaveChanges(); return(true); }
private void listBoxBookModify_SelectedValueChanged(object sender, EventArgs e) { var selected = listBoxBookModify.SelectedItem; if (selected != null) { if (selected.ToString().Length != 0) { string[] splitted = selected.ToString().Split('-'); BookExemplar search = exemplars.Find(x => x.BookExemplarId == Int32.Parse(splitted[1])); this.selected = search; if (search != null) { txtModifyName.Text = search.Book.BookName; listBoxGenreModify.SelectedItem = search.Book.Genre.GenreName; listBoxModifyAuthor.ClearSelected(); List <Author> list = search.Book.Authors.ToList(); list.ForEach(x => { int index = listBoxModifyAuthor.FindString(x.AuthorName); if (index > -1) { listBoxModifyAuthor.SetSelected(index, true); } }); txtModifyISBN.Text = search.Book.BookISBN; txtModifyPagination.Text = search.Book.BookPagination.ToString(); txtModifyEdition.Text = search.Book.BookEdition.ToString(); txtModifyExemplar.Text = search.BookExemplarId.ToString(); comboModifyLocation.SelectedItem = search.BookLocation.BookLocationPlace; } } } else { if (listBoxBookModify.Items.Count > 0) { listBoxBookModify.SelectedIndex = 0; } } }
private void listBoxExemplars_SelectedValueChanged(object sender, EventArgs e) { var selection = listBoxExemplars.SelectedItem; if (selection != null) { string[] content = selection.ToString().Split('-'); Lending found = lendings.Find(item => item.BookExemplar.BookExemplarId == Int32.Parse(content[1])); if (found != null) { lending = found; bookExemplar = found.BookExemplar; dateTimePickerFrom.Value = found.LendingDate; dateTimePickerTo.Value = found.LendingReturn; } } else if (listBoxExemplars.Items.Count > 0) { listBoxExemplars.SelectedIndex = 0; } }
public bool createLendingByLending(Lending lending) { Lending existing = context.Lendings .Include(be => be.BookExemplar) .FirstOrDefault(item => item.BookExemplar.BookExemplarId == lending.BookExemplar.BookExemplarId); if (existing != null) { return(false); } BookExemplar bookExemplar = context.BookExemplars.FirstOrDefault(item => item.BookExemplarId == lending.BookExemplar.BookExemplarId); if (bookExemplar == null) { return(false); } Member member = context.Members.FirstOrDefault(item => item.MemberId == lending.Member.MemberId); if (member == null) { return(false); } Lending toSave = new Lending() { BookExemplar = bookExemplar, LendingDate = lending.LendingDate, LendingReturn = lending.LendingReturn, Member = member }; context.Lendings.Add(toSave); context.SaveChanges(); return(true); }
public bool deleteAuthorAndAssociatedBooksByAuhorId(int id, bool deleteWhereAuthorisCoauthor) { Author author = context.Authors .Include(item => item.Books) .First(item => item.AuthorId == id); List <Book> bookToDelete = new List <Book>(); List <BookExemplar> exemplarToDelete = new List <BookExemplar>(); author.Books.ToList().ForEach(item => { if (item.Authors.ToList().Count > 1 && deleteWhereAuthorisCoauthor) { bookToDelete.Add(item); } if (item.Authors.ToList().Count == 1) { bookToDelete.Add(item); } }); bookToDelete.ForEach(item => { BookExemplar be = context.BookExemplars .Include(b => b.Book) .First(b => b.Book.BookId == item.BookId); exemplarToDelete.Add(be); }); exemplarToDelete.ForEach(item => context.BookExemplars.Remove(item)); context.SaveChanges(); bookToDelete.ForEach(item => context.Books.Remove(item)); context.SaveChanges(); context.Authors.Remove(author); context.SaveChanges(); return(true); }
public bool deleteExemplarAndBookByBookExemplarId(int bookExemplarId) { BookExemplar exemplar = context.BookExemplars.Include(b => b.Book).First(item => item.BookExemplarId == bookExemplarId); if (exemplar == null) { return(false); } Book book = context.Books.Find(exemplar.Book.BookId); if (book == null) { return(false); } context.BookExemplars.Remove(exemplar); context.SaveChanges(); context.Books.Remove(book); context.SaveChanges(); return(true); }
private void button1_Click(object sender, EventArgs e) { bool hasChanged = false; bool isInputValid = true; String txtGenre = "", txtName = "", txtISBN = "", txtLocation = ""; int edition = 0, pagination = 0, exemplar = 0; try { txtGenre = listBoxGenreModify.SelectedItem.ToString().Trim(); txtName = txtModifyName.Text.Trim(); txtISBN = txtModifyISBN.Text.Trim(); txtLocation = comboModifyLocation.SelectedItem.ToString().Trim(); edition = 0; Int32.TryParse(txtModifyEdition.Text.Trim(), out edition); pagination = 0; Int32.TryParse(txtModifyPagination.Text.Trim(), out pagination); exemplar = Int32.Parse(txtModifyExemplar.Text); } catch (Exception error) { } if (txtGenre.Length == 0) { isInputValid = false; } if (txtName.Length < 5) { isInputValid = false; } if (!Regex.IsMatch(txtISBN, @"^\d{3}\-{1}\d{1}\-{1}\d{1,4}\-{1}\d{4,10}\-{1}\d{1}$")) { isInputValid = false; } if (txtLocation.Length == 0) { isInputValid = false; } if (pagination == 0) { isInputValid = false; } if (edition < 1) { isInputValid = false; } if (txtModifyEdition.Text != selected.Book.BookEdition.ToString()) { hasChanged = true; } if (txtModifyISBN.Text != selected.Book.BookISBN) { hasChanged = true; } if (txtModifyName.Text != selected.Book.BookName) { hasChanged = true; } if (txtModifyPagination.Text != selected.Book.BookPagination.ToString()) { hasChanged = true; } if (comboModifyLocation.SelectedText != selected.BookLocation.BookLocationPlace) { hasChanged = true; } if (listBoxGenreModify.SelectedItem.ToString() != selected.Book.Genre.GenreName) { hasChanged = true; } List <String> selectedAuthorsString = listBoxModifyAuthor.SelectedItems.Cast <String>().ToList(); List <Author> selectedAuthors = new List <Author>(); selected.Book.Authors.ToList().ForEach(item => { var found = selectedAuthorsString.Find(i => i == item.AuthorName); if (found == null) { hasChanged = true; } }); selectedAuthorsString.ForEach(item => { selectedAuthors.Add(new Author() { AuthorName = item }); }); BookExemplar toUpdate = new BookExemplar(); toUpdate.Book = new Book(); toUpdate.Book.Genre = new Genre(); toUpdate.Book.Authors = new List <Author>(); toUpdate.BookLocation = new BookLocation(); toUpdate.BookExemplarId = selected.BookExemplarId; toUpdate.Book.BookEdition = edition; toUpdate.Book.BookId = selected.Book.BookId; toUpdate.Book.BookISBN = txtISBN; toUpdate.BookLocation.BookLocationPlace = txtLocation; toUpdate.Book.Genre.GenreName = txtGenre; toUpdate.Book.BookName = txtName; toUpdate.Book.BookISBN = txtISBN; toUpdate.Book.BookId = selected.Book.BookId; toUpdate.Book.BookPagination = pagination; toUpdate.Book.Authors = selectedAuthors; if (hasChanged && isInputValid) { if (BookExemplarRepository.Instance.updateBookExemplarByBookExemplar(toUpdate)) { MessageBox.Show("Das Buch \"" + txtName + "\" wurde erfolgreich geändert", "Geändert", MessageBoxButtons.OK, MessageBoxIcon.Information); loadExemplars(); } } }