Implementing the book method

Next, let's copy and adapt the createBook method.

For this one, we need to add a book: Book parameter. That way, users of the service will be able to pass in the book to add to the collection.

Here is the new code for this method:

createBook(collectionIdentifier: string, book: Book): void { 
  if (!collectionIdentifier) { 
    throw new Error('The collection identifier is required to create a     new book!'); 
  } 
 
  if (!this._bookCollections.has(collectionIdentifier) ||     !this._bookCollections.get(collectionIdentifier)) { 
    console.error('Tried to add a book to an unknown collection.     Identifier: ', collectionIdentifier); 
    this.displayErrorMessage('Failed to create the new book!'); 
    return; 
  } 
 
  const existingCollection =  this._bookCollections.get(collectionIdentifier); ...

Get Learn TypeScript 3 by Building Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.