/// <summary> /// Initialise the data. /// </summary> private void LoadDatabase() { Book bk1 = new Book() { Id = 1, Title = "The furst book", Author = "Someone", Copies = new List<Copy>() }; bk1.Copies.Add(new Copy() { IsAvailable = false }); bk1.Copies.Add(new Copy() { IsAvailable = true }); Book bk2 = new Book() { Id = 2, Title = "The Second book", Author = "Someone Else", Copies = new List<Copy>() }; bk2.Copies.Add(new Copy() { IsAvailable = false }); Book bk3 = new Book() { Id = 3, Title = "The Third book", Author = "Someone", Copies = new List<Copy>() }; bk3.Copies.Add(new Copy() { IsAvailable = false }); bk3.Copies.Add(new Copy() { IsAvailable = true }); bk3.Copies.Add(new Copy() { IsAvailable = true }); _library.Add(bk1); _library.Add(bk2); _library.Add(bk3); }
/// <summary> /// Add a book to the fake database /// </summary> /// <param name="book">The book to be added</param> /// <returns>The Id of the book added.</returns> /// <remarks> /// If the book already exists, comparison by Title, /// then the copies are added to it. /// </remarks> public int AddBook(Book book) { int rc = 0; // Look for the book by Title. var idx = _library.FindIndex(b => b.Title == book.Title); if (idx >= 0) { // Update existing by adding the number of copies foreach (var c in book.Copies) _library[idx].Copies.Add(c); rc = _library[idx].Id; } else { // Assign the next available id. book.Id = GetNextId(); // Add a new book to the library _library.Add(book); rc = book.Id; } return rc; }