'use strict';

const express = require('express');

const routes = (Book) => {
  const bookRouter = express.Router();

  bookRouter.route('/')
    .post((req, res) => {
      const book = new Book(req.body);

      book.save();
      res.status(201).send(book);
    })
    .get((req, res) => {
      let query = {};

      if (req.query.genre) {
        query.genre = req.query.genre;
      }

      Book.find(query, (err, books) => {
        if (err) {
          res.status(500).send(err);
        } else {
          res.json(books);
        }
      });
    });

  bookRouter.route('/:bookId')
    .get((req, res) => {
      Book.findById(req.params.bookId, (err, book) => {
        if (err) {
          res.status(500).send(err);
        } else {
          res.json(book);
        }
      });
    })
    .put((req, res) => {
      Book.findById(req.params.bookId, (err, book) => {
        if (err) {
          res.status(500).send(err);
        } else {
          book.title  = req.body.title;
          book.author = req.body.author;
          book.genre  = req.body.genre;
          book.read   = req.body.read;
          book.save();
          res.json(book);
        }
      });
    });

  return bookRouter;
};

module.exports = routes;

@@ -566,28 +566,26 @@
  bookRouter.
-rout
+us
 e('/:bookId'
@@ -584,27 +584,18 @@
 :bookId'
-)%0A    .get(
+, 
 (req, re
@@ -587,32 +587,38 @@
 okId', (req, res
+, next
 ) =%3E %7B%0A      Boo
@@ -604,34 +604,32 @@
  next) =%3E %7B%0A    
-  
 Book.findById(re
@@ -660,34 +660,32 @@
 ook) =%3E %7B%0A      
-  
 if (err) %7B%0A     
@@ -679,34 +679,32 @@
 (err) %7B%0A        
- 
- 
 res.status(500).
@@ -716,37 +716,43 @@
 err);%0A      
-  
 %7D else 
+if (book) 
 %7B%0A
-  
         res.
@@ -745,36 +745,37 @@
 %7B%0A        re
-s.json(
+q.book = 
 book
-)
 ;%0A        %7D%0A
@@ -768,33 +768,39 @@
 ok;%0A        
-%7D
+next();
 %0A      %7D
 );%0A    %7D)%0A  
@@ -791,169 +791,197 @@
    %7D
-);
+ else %7B
 %0A    
-%7D)%0A
     
-.put((req, res) =%3E %7B%0A      Book.findById(req.params.bookId, (err, book) =%3E %7B%0A        if (err) %7B%0A  
+res.status(404).send('no book found');%0A      %7D%0A    %7D);%0A  %7D);%0A%0A  bookRouter.route('/:bookId')%0A    .get((req, res) =%3E %7B%0A
       
-  
 res.
-status(500).send(err
+json(req.book
 );%0A    
+%7D)%0A
     
-%7D else
+.put((req, res) =%3E
  %7B%0A 
@@ -981,28 +981,28 @@
  =%3E %7B%0A      
-    
+req.
 book.title  
@@ -1021,28 +1021,28 @@
 itle;%0A      
-    
+req.
 book.author 
@@ -1062,28 +1062,28 @@
 thor;%0A      
-  
-  
+req.
 book.genre  
@@ -1102,28 +1102,28 @@
 enre;%0A      
-    
+req.
 book.read   
@@ -1145,20 +1145,20 @@
 ;%0A      
-    
+req.
 book.sav
@@ -1164,28 +1164,24 @@
 ve();%0A      
-    
 res.json(boo
@@ -1181,32 +1181,16 @@
 son(
+req.
 book
-);%0A        %7D%0A      %7D
 );%0A 

Add middleware for '/:bookId' routes