Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Av Daniel Larsson och Martin Ekström
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace LibraryProject
- {
- /// <summary>
- /// A service that handles books, uses singleton pattern and can only be instantiated/accessed through GetInstance().
- /// </summary>
- public class BookService
- {
- private static BookService instance;
- private Database db;
- /// <summary>
- /// Event for when a book is added, edited or removed
- /// </summary>
- public event Action Updated = delegate { };
- /// <summary>
- /// Default constructor
- /// </summary>
- private BookService()
- {
- db = Database.GetInstance();
- }
- /// <summary>
- /// Method to instantiate/access the book service.
- /// </summary>
- /// <returns>An instance of BookService</returns>
- public static BookService GetInstance()
- {
- if (instance == null)
- instance = new BookService();
- return instance;
- }
- /// <summary>
- /// Method that returns all books
- /// </summary>
- /// <returns>List of all books</returns>
- public List<Book> GetAllBooks()
- {
- return db.Books.ToList();
- }
- /// <summary>
- /// Searches all books and returns a list of matching books
- /// </summary>
- /// <param name="searchString">Search string</param>
- /// <returns>List of matching books</returns>
- public List<Book> SearchAllBooks(string searchString)
- {
- return MultiplePropertiesSearch(searchString, GetAllBooks());
- }
- /// <summary>
- /// Method that returns a list of books that are currently available.
- /// </summary>
- /// <returns>List of available books</returns>
- public List<Book> GetAvailableBooks()
- {
- IEnumerable<int> copyIdsOnLoan = db.Loans.Select(loan => loan.Fk_Book_Copies_Id);
- IEnumerable<int> availableBookIds = db.Book_Copies.Where(copy => !copyIdsOnLoan.Contains(copy.Id)).Select(theCopy => theCopy.Fk_Books_Id);
- List<Book> availableBooks = db.Books.Where(book => availableBookIds.Contains(book.Id)).ToList();
- return availableBooks;
- }
- /// <summary>
- /// Method that searches available books and returns a list of matching books
- /// </summary>
- /// <param name="searchString">Search string</param>
- /// <returns>List of matching available books</returns>
- public List<Book> SearchAvailableBooks(string searchString)
- {
- return MultiplePropertiesSearch(searchString, GetAvailableBooks());
- }
- /// <summary>
- /// Method to find all the books of an specific author
- /// </summary>
- /// <param name="author">Name of the author</param>
- /// <returns></returns>
- public List<Book> GetBooksByAuthor(string author)
- {
- return db.Books.Where(b => b.Authors.Where(a => a.Name.ToLower().Contains(author.ToLower())).Count() > 0).ToList();
- }
- /// <summary>
- /// Method to find all books with a specific title
- /// </summary>
- /// <param name="title">Title of the book</param>
- /// <returns></returns>
- public List<Book> GetBooksByTitle(string title)
- {
- return db.Books.Where(b => b.Title.ToLower().Contains(title.ToLower())).ToList();
- }
- /// <summary>
- /// Method to find all books of an specific ISBN
- /// </summary>
- /// <param name="isbn">ISBN of the book</param>
- /// <returns></returns>
- public List<Book> GetBooksByIsbn(string isbn)
- {
- return db.Books.Where(b => b.Isbn.Equals(isbn)).ToList();
- }
- /// <summary>
- /// Method that takes a string and returns a list of books where the
- /// string matches any property of the book.
- /// </summary>
- /// <param name="searchString">Search string</param>
- /// <returns>List of matching books</returns>
- public List<Book> MultiplePropertiesSearch(String searchString)
- {
- List<Book> searchResult;
- if (String.IsNullOrWhiteSpace(searchString))
- {
- searchResult = GetAllBooks();
- }
- else
- {
- var byIsbnResult = GetBooksByIsbn(searchString);
- var byTitleResult = GetBooksByTitle(searchString);
- var byAuthorResult = GetBooksByAuthor(searchString);
- searchResult = byIsbnResult.Concat(byTitleResult.Concat(byAuthorResult)).Distinct().ToList();
- }
- return searchResult;
- }
- /// <summary>
- /// Method that takes a list of books and a search string and returns a list of books
- /// were the search string matches one or more of the books properties
- /// </summary>
- /// <param name="searchString">Search string</param>
- /// <param name="listOfBooks">List of matching books</param>
- /// <returns></returns>
- public List<Book> MultiplePropertiesSearch(String searchString, List<Book> listOfBooks)
- {
- List<Book> searchResult;
- if (String.IsNullOrWhiteSpace(searchString))
- {
- searchResult = listOfBooks;
- }
- else
- {
- List<Book> byIsbnResult = listOfBooks.Where(book => book.Isbn.Contains(searchString)).ToList();
- List<Book> byTitleResult = listOfBooks.Where(book => book.Title.ToLower().Contains(searchString.ToLower())).ToList();
- List<Book> byAuthorResult = listOfBooks.Where(book => book.Authors.Where(auth => auth.Name.ToLower().Contains(searchString.ToLower())).Count() > 0).ToList();
- searchResult = byIsbnResult.Concat(byTitleResult.Concat(byAuthorResult)).Distinct().ToList();
- }
- return searchResult;
- }
- /// <summary>
- /// Method that takes an ISBN and returns the matching book
- /// </summary>
- /// <param name="isbn">ISBN to search</param>
- /// <returns>Matching book</returns>
- public Book GetBookFromIsbn(string isbn)
- {
- IEnumerable<Book> books = db.Books.Where(b => b.Isbn.Equals(isbn));
- Book book = books.SingleOrDefault();
- if (!book.Isbn.Equals(isbn))
- throw new InvalidOperationException();
- return book;
- }
- /// <summary>
- /// Method that adds a book to the database
- /// </summary>
- /// <param name="book">Book to add</param>
- public void AddBook(Book book)
- {
- if (BookExists(book) ||
- !ValidateIsbn(book))
- {
- throw new InvalidOperationException();
- }
- else
- {
- db.AddToBooks(book);
- db.SaveChanges();
- Updated();
- }
- }
- /// <summary>
- /// Method that removes a book from the database
- /// </summary>
- /// <param name="book">Book to remove</param>
- public void RemoveBook(Book book)
- {
- IEnumerable<Book_Copy> copies = db.Book_Copies;
- IEnumerable<Book_Copy> selectedCopies = copies.Where(copy => copy.Fk_Books_Id.Equals(book.Id));
- IEnumerable<int> selectedCopyIds = selectedCopies.Select(currentCopy => currentCopy.Id);
- IEnumerable<Loan> selectedLoans = db.Loans.Where(loan => selectedCopyIds.Contains(loan.Fk_Book_Copies_Id));
- foreach (Loan currLoan in selectedLoans)
- db.DeleteObject(currLoan);
- foreach (Book_Copy currCopy in selectedCopies)
- db.DeleteObject(currCopy);
- db.Books.DeleteObject(book);
- db.SaveChanges();
- Updated();
- }
- /// <summary>
- /// Method that checks if a book has a valid ISBN (10 or 13 digits)
- /// </summary>
- /// <param name="book">Book to check</param>
- /// <returns>True if the ISBN is valid, false if it's not</returns>
- private bool ValidateIsbn(Book book)
- {
- long result;
- return long.TryParse(book.Isbn, out result) &&
- book.Isbn.Length == 10 ||
- book.Isbn.Length == 13;
- }
- /// <summary>
- /// Method that checks if there is a book with the specified ISBN
- /// </summary>
- /// <param name="book">Book to check</param>
- /// <returns>True if the books ISBN exists in the DB, false if it doesn't</returns>
- private bool BookExists(Book book)
- {
- return db.Books.Where(theBook => theBook.Isbn.Equals(book.Isbn)).Count() > 0;
- }
- /// <summary>
- /// Method that updates the properties of a book
- /// </summary>
- /// <param name="book">Book to edit</param>
- public void EditBook(Book book)
- {
- db.Books.ApplyCurrentValues(book);
- db.SaveChanges();
- Updated();
- }
- /// <summary>
- /// Method that connects a book to an author
- /// </summary>
- /// <param name="book">Book to connect</param>
- /// <param name="author">Author to connect</param>
- public void ConnectBookToAuthors(Book book, Author author)
- {
- book.Authors.Add(author);
- author.Books.Add(book);
- }
- /// <summary>
- /// Method that connects a book to a list of authors
- /// </summary>
- /// <param name="book">Book to connect</param>
- /// <param name="authorList">List of authors to connect</param>
- public void ConnectBookToAuthorList(Book book, List<Author> authorList)
- {
- foreach (Author author in authorList)
- {
- ConnectBookToAuthors(book, author);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment