private void buttonReservation_Click(object sender, EventArgs e) { var titleToReserve = textBoxReservation.Text; if (titleToReserve == "") { MessageBox.Show("Podaj tytuł książki do rezerwacji."); return; } var bookToReserve = (from books in context.Books join copies in context.Copies on books.Id equals copies.BookId where copies.Status == "Dostępna" && books.Name == titleToReserve select copies).FirstOrDefault(); if (bookToReserve == null) { MessageBox.Show("Brak dostępnej książki do rezerwacji"); return; } bookToReserve.Status = "Zarezerwowana"; bookToReserve.UserId = userLoggedIn.Id; context.SubmitChanges(); MessageBox.Show("Zarezerwowano"); }
private void buttonAddUser_Click(object sender, EventArgs e) { if (textBoxName.Text.Length < 63 && textBoxSurname.Text.Length < 63 && textBoxEMail.Text.Length < 63 && textBoxPhoneNumber.Text.Length < 15 && textBoxAddress.Text.Length < 63 && textBoxCity.Text.Length < 63 && textBoxCountry.Text.Length < 31 && textBoxZipCode.Text.Length < 7 && textBoxName.Text.Length > 0 && textBoxSurname.Text.Length > 0 && textBoxEMail.Text.Length > 0 && textBoxPhoneNumber.Text.Length > 0 && textBoxAddress.Text.Length > 0 && textBoxCity.Text.Length > 0 && textBoxCountry.Text.Length > 0 && textBoxZipCode.Text.Length > 0) { var user = (from usr in context.Users where usr.Email == textBoxEMail.Text select usr).FirstOrDefault(); if (user != null) { MessageBox.Show("Klient ma już konto w bazie danych!", "Klient w bazie!"); } else { var userToAdd = new User(); userToAdd.Firstname = textBoxName.Text; userToAdd.Lastname = textBoxSurname.Text; userToAdd.Email = textBoxEMail.Text; userToAdd.Phone = textBoxPhoneNumber.Text; userToAdd.Address = textBoxAddress.Text; userToAdd.City = textBoxCity.Text; userToAdd.Country = textBoxCountry.Text; userToAdd.ZipCode = textBoxZipCode.Text; var passwordToDataBase = CalculateMD5Hash(textBoxPassword.Text); userToAdd.Password = passwordToDataBase; context.Users.InsertOnSubmit(userToAdd); context.SubmitChanges(); var userToCheck = (from usr in context.Users where usr.Email == textBoxEMail.Text select usr).FirstOrDefault(); if (userToCheck != null) { MessageBox.Show("Dodano!", "Dodano", MessageBoxButtons.OK); this.Close(); } else { MessageBox.Show("Nieoczekiwany błąd!", "Błąd!", MessageBoxButtons.OK); } } } MessageBox.Show("Nieprawidłowy format danych!", "Błąd!"); }
private void buttonAdd_Click(object sender, EventArgs e) { // First, we need to create the book, as the author list demands an id of the book if (textBoxName.Text.Length > 0 && textBoxISBN.Text.Length > 0 && textBoxGenre.Text.Length > 0 && textBoxName.Text.Length < 63 && textBoxISBN.Text.Length < 15 && textBoxGenre.Text.Length < 31) { var bookToAdd = new Book(); bookToAdd.Name = textBoxName.Text; bookToAdd.ISBN = textBoxISBN.Text; bookToAdd.Genre = textBoxGenre.Text; bookToAdd.DateOfAdd = DateTime.Now; //Jak z tymi kopiami robimy context.Books.InsertOnSubmit(bookToAdd); context.SubmitChanges(); //As we have just created the book, we check the id of it var bookId = from Book in context.Books where (Book.Name == textBoxName.Text) && (Book.ISBN == textBoxISBN.Text) && (Book.Genre == textBoxGenre.Text) select Book.Id; int quantity = int.Parse(textBoxCopies.Text); for (int i = 0; i < quantity; i++) { Copy copyToAdd = new Copy(); copyToAdd.BookId = bookId.FirstOrDefault(); copyToAdd.Status = "Dostępna"; context.Copies.InsertOnSubmit(copyToAdd); context.SubmitChanges(); } List <int> authors = new List <int>(); //checking if the Athor is already in DB foreach (var element in listOfAuthors) { var author = from auth in context.Authors where (auth.Firstname == element.Firstname) && (auth.Lastname == element.Lastname) select auth.Id; var authorFirst = author.FirstOrDefault(); if (authorFirst != 0) { var authorList = new AuthorList(); authorList.AuthorId = authorFirst; authorList.BookId = bookId.FirstOrDefault(); context.AuthorLists.InsertOnSubmit(authorList); context.SubmitChanges(); } else { var id = (from auth in context.Authors select auth.Id).Max(); var authorToAdd = new Author(); authorToAdd.Firstname = element.Firstname; authorToAdd.Lastname = element.Lastname; context.Authors.InsertOnSubmit(authorToAdd); context.SubmitChanges(); var addedAuthor = (from authrs in context.Authors where authrs.Lastname == element.Lastname select authrs.Id).FirstOrDefault(); var authorList = new AuthorList() { AuthorId = addedAuthor, BookId = bookId.FirstOrDefault() }; context.AuthorLists.InsertOnSubmit(authorList); context.SubmitChanges(); } } } else { MessageBox.Show("Zły format danych", "Błąd!"); } this.Close(); }