/// <summary> /// Adds given user to database. Exception is thrown if user.username is taken. /// </summary> /// <param name="user">New User to be added to database</param> public void AddUser(User user) { if (ValidateUsername(user.Username)) { User _user = new User() { Username = user.Username, Password = user.Password }; if (user.Book != null) { foreach (Book book in user.Book) { _user.Book.Add(book); } } var db = new LibraryDBEntities(); db.User.Add(_user); db.SaveChanges(); } else { throw new Exception("Username taken! Can't allow duplicate usernames in database."); } }
/// <summary> /// Deletes given book from database. /// </summary> /// <param name="book">Book to be deleted</param> public void DeleteBook(Book book) { var db = new LibraryDBEntities(); var query = db.Book.Where(p => p.BookId.Equals(book.BookId)).FirstOrDefault(); db.Book.Remove(query); db.SaveChanges(); }
/// <summary> /// Assignes user to given book. /// </summary> /// <param name="user">user who loans a book</param> /// <param name="book">book to be loaned</param> public void setBookHolder(User user, Book book) { var db = new LibraryDBEntities(); var query = db.Book.Where(p => p.BookId.Equals(book.BookId)).First(); query.User = user; db.SaveChanges(); }
/// <summary> /// Makes a query to database to update given book's User and DueDate. Meaning that book is loaned out to given user by username for given timespan. /// </summary> /// <param name="book">Book to be updated</param> /// <param name="username">User's username who will loan given book</param> /// <param name="loanTime">Due date is calculated by DateTime.Now + loanTime</param> public void LoanBook(Book book, string username, TimeSpan loanTime) { var db = new LibraryDBEntities(); var query = db.Book.Where(p => p.BookId.Equals(book.BookId)).First(); query.User = db.User.Where(p => p.Username.Equals(username)).First(); query.DueDate = DateTime.Now + loanTime; db.SaveChanges(); }
/// <summary> /// Updates given book's User(holder) to null and DueDate to null. Meaning that User has returned that book. /// </summary> /// <param name="book">Book whose user is to be assigned to null</param> public void ReturnBook(Book book) { var db = new LibraryDBEntities(); var query = db.Book.Where(p => p.BookId.Equals(book.BookId)).First(); query.BookHolderId = null; query.DueDate = null; db.SaveChanges(); }
/// <summary> /// Makes a query to database to update book's title. All books that have same title and author are updated. /// </summary> /// <param name="title">New title</param> /// <param name="book">Book to be updated</param> public void setBookTitle(string title, Book book) { var db = new LibraryDBEntities(); var query = db.Book.Where(p => p.BookTitle.Equals(book.BookTitle) && p.BookAuthorId.Equals(book.BookAuthorId)).Select(p => (Book)p); foreach (var _book in query) { _book.BookTitle = title; } db.SaveChanges(); }
/// <summary> /// Adds author to database. /// </summary> /// <param name="author">Author to be added to database</param> public void setAuthor(Author author) { var db = new LibraryDBEntities(); using (db) { var query = db.Author.Where(p => p.AuthorId.Equals(author.AuthorId)).FirstOrDefault(); query.AuthorId = author.AuthorId; query.AuthorFirstName = author.AuthorFirstName; query.AuthorLastName = author.AuthorLastName; db.SaveChanges(); } }
/// <summary> /// Adds given book to database. If book.Author's first name and last name are not found in database then new author is created. /// </summary> /// <param name="book">Book to be added to database</param> public void AddBook(Book book) { var db = new LibraryDBEntities(); Book newBook = new Book() { BookTitle = book.BookTitle, }; var author = db.Author.Where(p => p.AuthorFirstName.Equals(book.Author.AuthorFirstName) && p.AuthorLastName.Equals(book.Author.AuthorLastName)).FirstOrDefault(); if (author == null) { newBook.Author = book.Author; db.Book.Add(newBook); } else { newBook.Author = author; db.Book.Add(newBook); } db.SaveChanges(); }