static void ReturnBook(int userID, int bookID) { using (BookDatabaseDataContext connection = new BookDatabaseDataContext()) { var listOfBooks = (from book in connection.Books select book.BookID).ToList(); var listOfUsers = (from user in connection.Users select user.UserID).ToList(); if (!listOfBooks.Contains(bookID)) throw new BookNotExistantException(); if (!listOfUsers.Contains(userID)) throw new UserNotExistantException(); var listOfUserBookPairs = (from userBook in connection.UserBooks select userBook.BookID).ToList(); if (!listOfUserBookPairs.Contains(bookID)) throw new UserBookPairNotExistantException(); var listOfUsersWhoHaveThisBook = (from userbook in connection.UserBooks where userbook.BookID == bookID select userbook.UserID).ToList(); if (!listOfUsersWhoHaveThisBook.Contains(userID)) throw new UserHasNotBeenGivenThisBookException(); connection.Users.Where(x => x.UserID == userID).FirstOrDefault().Books_lent--; connection.RemoveUserBookRecord(userID, bookID); connection.SubmitChanges(); } Console.WriteLine("Book successfully returned."); }
static void LoanBookToUser(int bookID, int userID, DateTime dateGiven, DateTime dateReturn) { using (BookDatabaseDataContext connection = new BookDatabaseDataContext()) { var listOfBooks = (from book in connection.Books select book.BookID).ToList(); if(!listOfBooks.Contains(bookID)) throw new BookNotExistantException(); var listOfUsers = (from user in connection.Users select user.UserID).ToList(); if (!listOfUsers.Contains(userID)) throw new UserNotExistantException(); int numberOfBookCopies = (from book in connection.Books where book.BookID == bookID select book.Number_of_copies).SingleOrDefault(); int numberOfBookCopiesLoaned = (from book in connection.UserBooks where book.BookID == bookID select book).Count(); if (numberOfBookCopiesLoaned >= numberOfBookCopies) { Console.WriteLine("No copies left."); return; } if(connection.Users.Where(x => x.UserID == userID).FirstOrDefault().Books_lent >= 5) { Console.WriteLine("This user has already been given 5 books. No more books can be loaned until some are returned."); return; } connection.Users.Where(x => x.UserID == userID).FirstOrDefault().Books_lent++; connection.AddUserBookRecord(bookID, userID, numberOfBookCopiesLoaned + 1, dateGiven, dateReturn); connection.SubmitChanges(); } Console.WriteLine("Book successfully loaned."); }
static void InsertBook(string title, string description, string datePublished, string publisher, string genre, int pages, int isbn, int numberOfCopies) { using (BookDatabaseDataContext connection = new BookDatabaseDataContext()) { var listOfBooks = connection.Books; foreach (var book in listOfBooks) if (book.Title == title) throw new BookAlreadyInDatabaseException(); foreach (var book in listOfBooks) if (book.ISBN == isbn) throw new BookISBNAlreadyExistsInDatabaseException(); if (description == "") description = null; Book bookToAdd = new Book() { Title = title, Description = description, Date_Published = DateTime.Parse(datePublished), Publisher = publisher, Genre = genre, Pages = pages, ISBN = isbn, Number_of_copies = numberOfCopies }; connection.Books.InsertOnSubmit(bookToAdd); connection.SubmitChanges(); } }
static void InsertUser(string firstName, string lastName, string pseudonim, string email, int phone) { using (BookDatabaseDataContext connection = new BookDatabaseDataContext()) { if (pseudonim == "") pseudonim = null; if (email == "") email = null; User userToAdd = new User() { First_Name = firstName, Last_Name = lastName, Pseudonim = pseudonim, Email = email, Phone = phone, Books_lent = 0 }; connection.Users.InsertOnSubmit(userToAdd); connection.SubmitChanges(); } }
static void InsertAuthor(string firstName, string lastName, int yearBorn, int yearDied) { using (BookDatabaseDataContext connection = new BookDatabaseDataContext()) { Author authorToAdd = new Author() { First_Name = firstName, Last_Name = lastName, Year_Born = yearBorn, Year_Died = yearDied }; connection.Authors.InsertOnSubmit(authorToAdd); connection.SubmitChanges(); } }