text: var Book = require('../models/book');
  var async = require('async');
  var Genre = require('../models/genre');
  
  //Import the functions for validation and sanitization
  const { body,validationResult } = require('express-validator/check');
  const { sanitizeBody } = require('express-validator/filter');
  
  // Display list of all Genre.
  exports.genre_list = function(req, res) {
      
      Genre.find()
      .sort([['name', 'ascending']])
      .exec(function (err, list_genres) {
          if (err) {return next(err);}
          //Successful, so render
          res.render('genre_list', {title: 'Genre List', genre_list: list_genres, user: req.cookies['user']});
      });
  };
  
  // Display detail page for a specific Genre.
  exports.genre_detail = function(req, res, next) {
  
      async.parallel({
          genre: function(callback) {
              Genre.findById(req.params.id)
                .exec(callback);
          },
  
          genre_books: function(callback) {
              Book.find({ 'genre': req.params.id })
                .exec(callback);
          },
  
      }, function(err, results) {
          if (err) { return next(err); }
          if (results.genre==null) { // No results.
              var err = new Error('I no see am ooo!');
              err.status = 404;
              return next(err);
          }
          // Successful, so render
          res.render('genre_detail', { title: 'Genre Detail', genre: results.genre, genre_books: results.genre_books, user: req.cookies['user'] } );
      });
  
  };
  
  // Display Genre create form on GET.
  exports.genre_create_get = function(req, res, next) {     
      res.render('genre_form', { title: 'Create Genre', user: req.cookies['user']});
    };
  
  // Handle Genre create on POST.
  exports.genre_create_post =  [
     
      // Validate that the name field is not empty and has only alphabets
      body('name').isLength({ min: 1 }).trim().withMessage('Genre name required.').isAlpha().withMessage('Genre name can only be alphabet letters.'),
      
      // Sanitize (escape) the name field.
      sanitizeBody('name').escape(),
    
      // Process request after validation and sanitization.
      (req, res, next) => {
    
        // Extract the validation errors from a request.
        const errors = validationResult(req);
    
        // Create a genre object with escaped and trimmed data.
        var genre = new Genre(
          { name: req.body.name }
        );
    
    
        if (!errors.isEmpty()) {
          // There are errors. Render the form again with sanitized values/error messages.
          res.render('genre_form', { title: 'Create Genre', genre: genre, errors: errors.array(), user: req.cookies['user']});
          return;
        }
        else {
          // Data from form is valid.
          // Check if Genre with same name already exists.
          Genre.findOne({ 'name': req.body.name })
            .exec( function(err, found_genre) {
               if (err) { return next(err); }
    
               if (found_genre) {
                 // Genre exists, redirect to its detail page.
                 res.redirect(found_genre.url);
               }
               else {
    
                 genre.save(function (err) {
                   if (err) { return next(err); }
                   // Genre saved. Redirect to genre detail page.
                   res.redirect(genre.url);
                 });
    
               }
    
             });
        }
      }
    ];
  
  // Display Genre delete form on GET.
  exports.genre_delete_get = function(req, res, next) {
  
    async.parallel({
      genre: function(callback) {
        Genre.findById(req.params.id).exec(callback)
      },
      genre_books: function(callback) {
        Book.find({'genre': req.params.id}).exec(callback)
      },
    }, function(err, results) {
      if (err) {return next(err); }
      if (results.genre == null) {
        res.redirect('/catalog/genres');
      };
      res.render('genre_delete', { title: 'Delete Genre', genre: results.genre, genre_books: results.genre_books, user: req.cookies['user']});
    });
  };
  
  // Handle Genre delete on POST.
  exports.genre_delete_post = function(req, res, next) {
  
    async.parallel({
      genre: function(callback) {
        Genre.findById(req.body.genreid).exec(callback)
      },
      genre_books: function(callback) {
        Book.find( {'genre': req.body.genreid} ).exec(callback)
      },
    }, function(err, results) {
      if (err) { return next(err); }
      if (results.genre_books.length > 0) {
        res.render('genre_delete', { title: 'Delete Genre', genre: results.genre, genre_books: results.genre_books, user: req.cookies['user']});
        return;
      } else {
        Genre.findByIdAndRemove(req.body.genreid, function deleteGenre(err) {
          if (err) {return next(err);};
          res.redirect('/catalog/genres');
        });
      };
    });
  };
  
  // Display Genre update form on GET.
  exports.genre_update_get = function(req, res, next) {
  
    Genre.findById(req.params.id).exec( function(err, genre) {
      if (err) {
        return next(err);
      };
      if (genre == null) {
        var err = new Error('Genre not found');
        err.status = 404;
        return next(err);
      };
  
      res.render('genre_form', { title: 'Update Genre', genre: genre, user: req.cookies['user']});
    });
  };
  
  // Handle Genre update on POST.
  exports.genre_update_post = [
  
    body('name', 'Genre name required').isLength({ min: 1 }).trim(),
  
    sanitizeBody('name').escape(),
  
    (req, res, next) => {
      const errors = validationResult(req);
  
      var genre = new Genre ({
        name: req.body.name,
        _id: req.params.id
      });
  
      if (!errors.isEmpty()) {
        res.render('genre_form', { title: 'Update Genre', genre: genre, errors: errors.array(), user: req.cookies['user']});
        return;
      } else {
        Genre.findByIdAndUpdate(req.params.id, genre, {}, function (err, thegenre) {
          if (err) {
            return next(err); }
            
          res.redirect(thegenre.url);
        });
      };
    }
  ];