Mercurial > silverbladetech
annotate Library/LibrarySystem/Core/Library.cs @ 109:11994ccdc50b
First wave of tests complete
author | stevenhollidge <stevenhollidge@hotmail.com> |
---|---|
date | Sun, 06 May 2012 20:37:32 +0100 |
parents | 3591c26bd63e |
children |
rev | line source |
---|---|
43
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
1 using System; |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
2 using System.Collections.Generic; |
41 | 3 using System.Linq; |
4 using LibrarySystem.Interfaces; | |
5 | |
6 namespace LibrarySystem.Core | |
7 { | |
8 public class Library : ILibrary | |
9 { | |
43
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
10 public string Location { get; private set; } |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
11 public IList<IStock> StockList { get; private set; } |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
12 |
59
3591c26bd63e
MVVMLight added
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
43
diff
changeset
|
13 private Library() |
43
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
14 { |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
15 } |
41 | 16 |
43
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
17 public static ILibrary Create(string location) |
41 | 18 { |
43
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
19 if (location == null) |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
20 throw new ArgumentNullException(); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
21 |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
22 return new Library() {Location = location, StockList = new List<IStock>(0)}; |
41 | 23 } |
24 | |
25 public void AddStock(params IBook[] books) | |
26 { | |
43
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
27 if (books == null) |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
28 throw new ArgumentNullException(); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
29 |
41 | 30 var stock = new List<IStock>(StockList.Count + books.Length); |
31 | |
32 stock.AddRange(StockList); | |
33 | |
34 foreach (var book in books) | |
35 { | |
43
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
36 if (book == null) |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
37 throw new ArgumentNullException(); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
38 |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
39 ThrowExceptionIfBookIsAlreadyInStock(book); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
40 |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
41 stock.Add(Stock.Create(book, true)); |
41 | 42 } |
43 | |
44 StockList = stock; | |
45 } | |
46 | |
43
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
47 private void ThrowExceptionIfBookIsAlreadyInStock(IBook book) |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
48 { |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
49 var lookup = ((List<IStock>) StockList).Find(s => s.Book.Id == book.Id); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
50 if (lookup != null) |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
51 throw new Exception(string.Format("Book id {0} is already in stock", book.Id)); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
52 } |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
53 |
41 | 54 public bool IsBookAvailableByISBN(string isbn) |
55 { | |
43
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
56 if (isbn == null) |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
57 throw new ArgumentNullException(); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
58 |
41 | 59 var qry = from books in StockList |
60 where books.Book.ISBN == isbn | |
61 & books.IsAvailable | |
62 select books.IsAvailable; | |
63 | |
64 return qry.FirstOrDefault(); | |
65 } | |
66 | |
67 public IList<IStock> FindBooks(string searchString) | |
68 { | |
43
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
69 if (searchString == null) |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
70 throw new ArgumentNullException(); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
71 |
41 | 72 searchString = searchString.ToLower(); |
73 | |
74 var qry = from books in StockList | |
75 where books.Book.Title.ToLower().Contains(searchString) | |
76 || books.Book.Author.ToLower().Contains(searchString) | |
77 select books; | |
78 | |
79 return qry.ToList(); | |
80 } | |
81 | |
43
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
82 public IBook GetBookById(string id) |
41 | 83 { |
43
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
84 if (id == null) |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
85 throw new ArgumentNullException(); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
86 |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
87 id = id.ToLower(); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
88 |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
89 var qry = from books in StockList |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
90 where books.Book.Id.ToLower() == id |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
91 select books.Book; |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
92 |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
93 return qry.FirstOrDefault(); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
94 } |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
95 |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
96 |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
97 public void LendBook(IPerson person, string id) |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
98 { |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
99 if (person == null || id == null) |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
100 throw new ArgumentNullException(); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
101 |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
102 var stock = ThrowExceptionIfBookIsNotInStock(id); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
103 stock.IsAvailable = false; |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
104 |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
105 // FUTURE: register with person that they have borrowed the book |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
106 } |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
107 |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
108 private IStock ThrowExceptionIfBookIsNotInStock(string id) |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
109 { |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
110 var lookup = ((List<IStock>) StockList).Find(s => s.Book.Id == id); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
111 if (lookup == null) |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
112 throw new Exception(string.Format("Book with id {0} is not in stock", id)); |
aef06698d9e2
Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents:
41
diff
changeset
|
113 return lookup; |
41 | 114 } |
115 } | |
116 } |