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
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
3 using System.Linq;
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
4 using LibrarySystem.Interfaces;
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
5
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
6 namespace LibrarySystem.Core
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
7 {
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
8 public class Library : ILibrary
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
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
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
16
43
aef06698d9e2 Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents: 41
diff changeset
17 public static ILibrary Create(string location)
41
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
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
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
23 }
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
24
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
25 public void AddStock(params IBook[] books)
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
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
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
30 var stock = new List<IStock>(StockList.Count + books.Length);
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
31
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
32 stock.AddRange(StockList);
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
33
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
34 foreach (var book in books)
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
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
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
42 }
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
43
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
44 StockList = stock;
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
45 }
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
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
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
54 public bool IsBookAvailableByISBN(string isbn)
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
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
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
59 var qry = from books in StockList
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
60 where books.Book.ISBN == isbn
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
61 & books.IsAvailable
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
62 select books.IsAvailable;
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
63
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
64 return qry.FirstOrDefault();
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
65 }
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
66
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
67 public IList<IStock> FindBooks(string searchString)
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
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
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
72 searchString = searchString.ToLower();
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
73
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
74 var qry = from books in StockList
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
75 where books.Book.Title.ToLower().Contains(searchString)
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
76 || books.Book.Author.ToLower().Contains(searchString)
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
77 select books;
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
78
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
79 return qry.ToList();
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
80 }
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
81
43
aef06698d9e2 Added the library tests
Steven Hollidge <stevenhollidge@hotmail.com>
parents: 41
diff changeset
82 public IBook GetBookById(string id)
41
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
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
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
114 }
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
115 }
dbd242eb9c33 Initial wave of TDD development
adminsh@apollo
parents:
diff changeset
116 }