Guest User

Untitled

a guest
Feb 19th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.15 KB | None | 0 0
  1. // Av Daniel Larsson och Martin Ekström
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. namespace LibraryProject
  9. {
  10. /// <summary>
  11. /// A service that handles books, uses singleton pattern and can only be instantiated/accessed through GetInstance().
  12. /// </summary>
  13. public class BookService
  14. {
  15. private static BookService instance;
  16. private Database db;
  17.  
  18. /// <summary>
  19. /// Event for when a book is added, edited or removed
  20. /// </summary>
  21. public event Action Updated = delegate { };
  22.  
  23. /// <summary>
  24. /// Default constructor
  25. /// </summary>
  26. private BookService()
  27. {
  28. db = Database.GetInstance();
  29. }
  30.  
  31. /// <summary>
  32. /// Method to instantiate/access the book service.
  33. /// </summary>
  34. /// <returns>An instance of BookService</returns>
  35. public static BookService GetInstance()
  36. {
  37. if (instance == null)
  38. instance = new BookService();
  39.  
  40. return instance;
  41. }
  42.  
  43. /// <summary>
  44. /// Method that returns all books
  45. /// </summary>
  46. /// <returns>List of all books</returns>
  47. public List<Book> GetAllBooks()
  48. {
  49. return db.Books.ToList();
  50. }
  51.  
  52. /// <summary>
  53. /// Searches all books and returns a list of matching books
  54. /// </summary>
  55. /// <param name="searchString">Search string</param>
  56. /// <returns>List of matching books</returns>
  57. public List<Book> SearchAllBooks(string searchString)
  58. {
  59. return MultiplePropertiesSearch(searchString, GetAllBooks());
  60. }
  61.  
  62. /// <summary>
  63. /// Method that returns a list of books that are currently available.
  64. /// </summary>
  65. /// <returns>List of available books</returns>
  66. public List<Book> GetAvailableBooks()
  67. {
  68. IEnumerable<int> copyIdsOnLoan = db.Loans.Select(loan => loan.Fk_Book_Copies_Id);
  69. IEnumerable<int> availableBookIds = db.Book_Copies.Where(copy => !copyIdsOnLoan.Contains(copy.Id)).Select(theCopy => theCopy.Fk_Books_Id);
  70. List<Book> availableBooks = db.Books.Where(book => availableBookIds.Contains(book.Id)).ToList();
  71.  
  72. return availableBooks;
  73. }
  74.  
  75. /// <summary>
  76. /// Method that searches available books and returns a list of matching books
  77. /// </summary>
  78. /// <param name="searchString">Search string</param>
  79. /// <returns>List of matching available books</returns>
  80. public List<Book> SearchAvailableBooks(string searchString)
  81. {
  82. return MultiplePropertiesSearch(searchString, GetAvailableBooks());
  83. }
  84.  
  85. /// <summary>
  86. /// Method to find all the books of an specific author
  87. /// </summary>
  88. /// <param name="author">Name of the author</param>
  89. /// <returns></returns>
  90. public List<Book> GetBooksByAuthor(string author)
  91. {
  92. return db.Books.Where(b => b.Authors.Where(a => a.Name.ToLower().Contains(author.ToLower())).Count() > 0).ToList();
  93. }
  94.  
  95. /// <summary>
  96. /// Method to find all books with a specific title
  97. /// </summary>
  98. /// <param name="title">Title of the book</param>
  99. /// <returns></returns>
  100. public List<Book> GetBooksByTitle(string title)
  101. {
  102. return db.Books.Where(b => b.Title.ToLower().Contains(title.ToLower())).ToList();
  103. }
  104.  
  105. /// <summary>
  106. /// Method to find all books of an specific ISBN
  107. /// </summary>
  108. /// <param name="isbn">ISBN of the book</param>
  109. /// <returns></returns>
  110. public List<Book> GetBooksByIsbn(string isbn)
  111. {
  112. return db.Books.Where(b => b.Isbn.Equals(isbn)).ToList();
  113. }
  114.  
  115. /// <summary>
  116. /// Method that takes a string and returns a list of books where the
  117. /// string matches any property of the book.
  118. /// </summary>
  119. /// <param name="searchString">Search string</param>
  120. /// <returns>List of matching books</returns>
  121. public List<Book> MultiplePropertiesSearch(String searchString)
  122. {
  123. List<Book> searchResult;
  124.  
  125. if (String.IsNullOrWhiteSpace(searchString))
  126. {
  127. searchResult = GetAllBooks();
  128. }
  129. else
  130. {
  131. var byIsbnResult = GetBooksByIsbn(searchString);
  132. var byTitleResult = GetBooksByTitle(searchString);
  133. var byAuthorResult = GetBooksByAuthor(searchString);
  134.  
  135. searchResult = byIsbnResult.Concat(byTitleResult.Concat(byAuthorResult)).Distinct().ToList();
  136. }
  137.  
  138. return searchResult;
  139. }
  140.  
  141. /// <summary>
  142. /// Method that takes a list of books and a search string and returns a list of books
  143. /// were the search string matches one or more of the books properties
  144. /// </summary>
  145. /// <param name="searchString">Search string</param>
  146. /// <param name="listOfBooks">List of matching books</param>
  147. /// <returns></returns>
  148. public List<Book> MultiplePropertiesSearch(String searchString, List<Book> listOfBooks)
  149. {
  150. List<Book> searchResult;
  151.  
  152. if (String.IsNullOrWhiteSpace(searchString))
  153. {
  154. searchResult = listOfBooks;
  155. }
  156. else
  157. {
  158. List<Book> byIsbnResult = listOfBooks.Where(book => book.Isbn.Contains(searchString)).ToList();
  159. List<Book> byTitleResult = listOfBooks.Where(book => book.Title.ToLower().Contains(searchString.ToLower())).ToList();
  160. List<Book> byAuthorResult = listOfBooks.Where(book => book.Authors.Where(auth => auth.Name.ToLower().Contains(searchString.ToLower())).Count() > 0).ToList();
  161.  
  162. searchResult = byIsbnResult.Concat(byTitleResult.Concat(byAuthorResult)).Distinct().ToList();
  163. }
  164.  
  165. return searchResult;
  166. }
  167.  
  168. /// <summary>
  169. /// Method that takes an ISBN and returns the matching book
  170. /// </summary>
  171. /// <param name="isbn">ISBN to search</param>
  172. /// <returns>Matching book</returns>
  173. public Book GetBookFromIsbn(string isbn)
  174. {
  175. IEnumerable<Book> books = db.Books.Where(b => b.Isbn.Equals(isbn));
  176. Book book = books.SingleOrDefault();
  177.  
  178. if (!book.Isbn.Equals(isbn))
  179. throw new InvalidOperationException();
  180.  
  181. return book;
  182. }
  183.  
  184. /// <summary>
  185. /// Method that adds a book to the database
  186. /// </summary>
  187. /// <param name="book">Book to add</param>
  188. public void AddBook(Book book)
  189. {
  190. if (BookExists(book) ||
  191. !ValidateIsbn(book))
  192. {
  193. throw new InvalidOperationException();
  194. }
  195. else
  196. {
  197. db.AddToBooks(book);
  198. db.SaveChanges();
  199.  
  200. Updated();
  201. }
  202. }
  203.  
  204. /// <summary>
  205. /// Method that removes a book from the database
  206. /// </summary>
  207. /// <param name="book">Book to remove</param>
  208. public void RemoveBook(Book book)
  209. {
  210. IEnumerable<Book_Copy> copies = db.Book_Copies;
  211. IEnumerable<Book_Copy> selectedCopies = copies.Where(copy => copy.Fk_Books_Id.Equals(book.Id));
  212. IEnumerable<int> selectedCopyIds = selectedCopies.Select(currentCopy => currentCopy.Id);
  213. IEnumerable<Loan> selectedLoans = db.Loans.Where(loan => selectedCopyIds.Contains(loan.Fk_Book_Copies_Id));
  214.  
  215. foreach (Loan currLoan in selectedLoans)
  216. db.DeleteObject(currLoan);
  217.  
  218. foreach (Book_Copy currCopy in selectedCopies)
  219. db.DeleteObject(currCopy);
  220.  
  221. db.Books.DeleteObject(book);
  222. db.SaveChanges();
  223.  
  224. Updated();
  225. }
  226.  
  227. /// <summary>
  228. /// Method that checks if a book has a valid ISBN (10 or 13 digits)
  229. /// </summary>
  230. /// <param name="book">Book to check</param>
  231. /// <returns>True if the ISBN is valid, false if it's not</returns>
  232. private bool ValidateIsbn(Book book)
  233. {
  234. long result;
  235. return long.TryParse(book.Isbn, out result) &&
  236. book.Isbn.Length == 10 ||
  237. book.Isbn.Length == 13;
  238. }
  239.  
  240. /// <summary>
  241. /// Method that checks if there is a book with the specified ISBN
  242. /// </summary>
  243. /// <param name="book">Book to check</param>
  244. /// <returns>True if the books ISBN exists in the DB, false if it doesn't</returns>
  245. private bool BookExists(Book book)
  246. {
  247. return db.Books.Where(theBook => theBook.Isbn.Equals(book.Isbn)).Count() > 0;
  248. }
  249.  
  250. /// <summary>
  251. /// Method that updates the properties of a book
  252. /// </summary>
  253. /// <param name="book">Book to edit</param>
  254. public void EditBook(Book book)
  255. {
  256. db.Books.ApplyCurrentValues(book);
  257. db.SaveChanges();
  258.  
  259. Updated();
  260. }
  261.  
  262. /// <summary>
  263. /// Method that connects a book to an author
  264. /// </summary>
  265. /// <param name="book">Book to connect</param>
  266. /// <param name="author">Author to connect</param>
  267. public void ConnectBookToAuthors(Book book, Author author)
  268. {
  269. book.Authors.Add(author);
  270. author.Books.Add(book);
  271. }
  272.  
  273. /// <summary>
  274. /// Method that connects a book to a list of authors
  275. /// </summary>
  276. /// <param name="book">Book to connect</param>
  277. /// <param name="authorList">List of authors to connect</param>
  278. public void ConnectBookToAuthorList(Book book, List<Author> authorList)
  279. {
  280. foreach (Author author in authorList)
  281. {
  282. ConnectBookToAuthors(book, author);
  283. }
  284. }
  285. }
  286. }
Add Comment
Please, Sign In to add comment