private void AddButtonClick(object sender, EventArgs e) { if (availableBookRentsComboBox.SelectedItem == null || availableStudentsComboBox.SelectedItem == null) { var errorForm = new ErrorForm("You need to select student and book"); errorForm.ShowDialog(); } else { if (dateOfReturnDateTimePicker.Value < dateOfRentDateTimePicker.Value && isReturnedCheckBox.Checked) { var errorForm = new ErrorForm("Date of return cant be before date of rent"); errorForm.ShowDialog(); } else { var bookRentToAdd = new BookRent { Student = availableStudentsComboBox.SelectedItem as Student, Book = availableBookRentsComboBox.SelectedItem as Book, DateOfRent = dateOfRentDateTimePicker.Value, DateOfReturn = dateOfReturnDateTimePicker.Value }; if (!isReturnedCheckBox.Checked) { bookRentToAdd.DateOfReturn = null; } var specificBookRent = BookRentRepository.GetAllBookRents() .Where(x => x.BookId == bookRentToAdd.Book.BookId).ToList(); var specificBookRent2 = BookRentRepository.GetAllBookRents() .Where(x => x.StudentId == bookRentToAdd.Student.StudentId).ToList(); if (BookRentRepository.CheckAvailableDate(specificBookRent, bookRentToAdd) && BookRentRepository.CheckAvailableDate(specificBookRent2, bookRentToAdd)) { BookRentRepository.AddBookRent(bookRentToAdd); Close(); } else { var errorForm = new ErrorForm("That date is already taken"); errorForm.ShowDialog(); } } } }
private void SaveButtonClick(object sender, EventArgs e) { if (returnDateDateTimePicker.Value < rentDateDateTimePicker.Value && isReturnedCheckBox.Checked) { var errorForm = new ErrorForm("Date of return cant be before date of rent"); errorForm.ShowDialog(); } else { var bookRentToEdit = new BookRent { DateOfRent = rentDateDateTimePicker.Value, DateOfReturn = returnDateDateTimePicker.Value, Student = SelectedBookRent.Student, StudentId = SelectedBookRent.Student.StudentId, BookId = SelectedBookRent.Book.BookId, BookRentId = SelectedBookRent.BookRentId, Book = SelectedBookRent.Book }; if (!isReturnedCheckBox.Checked) { bookRentToEdit.DateOfReturn = null; } var specificBookRent = BookRentRepository.GetAllBookRents() .Where(x => x.BookId == bookRentToEdit.Book.BookId).ToList(); var wantedBookRent = BookRentRepository.GetAllBookRents() .Where(x => x.BookRentId == bookRentToEdit.BookRentId).ToList(); var specificBookRentFiltered = specificBookRent.Except(wantedBookRent).ToList(); var specificBookRent2 = BookRentRepository.GetAllBookRents() .Where(x => x.StudentId == bookRentToEdit.Student.StudentId).ToList(); var specificBookRent2Filtered = specificBookRent2.Except(wantedBookRent).ToList(); if (BookRentRepository.CheckAvailableDate(specificBookRentFiltered, bookRentToEdit) && BookRentRepository.CheckAvailableDate(specificBookRent2Filtered, bookRentToEdit)) { BookRentRepository.EditBookRent(bookRentToEdit); Close(); } else { var errorForm = new ErrorForm("That date is already taken"); errorForm.ShowDialog(); } } }