text: const { body,validationResult } = require("express-validator");
  var async = require('async');
  var Book = require('../models/book');
  var Genre = require('../models/genre');
  
  // Display list of all Genre.
  exports.genre_list = function(req, res, next) {
      
      Genre.find()
        .sort([['name', 'ascending']])
        .exec(function (err, list_genres) {
          if (err) { return next(err); }
          res.render('genre_list', { title: 'Genre List', genres_list: list_genres });
        });
  };
  
  // 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('Genre not found');
              err.status = 404;
              return next(err);
          }
          // Successful
          res.render('genre_detail', { title: 'Genre Detail', genre: results.genre, genre_books: results.genre_books });
      });
  };
  
  // Display Genre create form on GET.
  exports.genre_create_get = function(req, res, next) {
      res.render('genre_form', { title: 'Create Genre' });
  };
  
  // Handle Genre create on POST.
  exports.genre_create_post = [
      // 驗證與清消的 name fields
      body('name', 'Genre name required').trim().isLength({ min: 1 }).escape(),
  
      // 在驗證與清消後處理 request
      (req, res, next) => {
          // 提取 request的驗證錯誤
          const errors = validationResult(req);
  
          // 使用 escaped和 trimmed的資料來建立 genre
          var genre = new Genre(
              { name: req.body.name }
          );
  
          if (!errors.isEmpty()) {
              // 如果有 errors，使用清理過的值與 error message在 render一次 form
              res.render('genre_form', {title: 'Create Genre', genre: genre, errors: errors.array()});
              return;
          }
          else {
              // Form的資料通過驗證
              // 檢查 genre是否已經存在
              Genre.findOne({ 'name': req.body.name })
                .exec( function(err, found_genre) {
                    if (err) { return next(err); }
  
                    if (found_genre) {
                        // Genre已經存在，則 redirect到該 genre的 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) {
      res.send('NOT IMPLEMENTED: Genre delete GET');
  };
  
  // Handle Genre delete on POST.
  exports.genre_delete_post = function(req, res) {
      res.send('NOT IMPLEMENTED: Genre delete POST');
  };
  
  // Display Genre update form on GET.
  exports.genre_update_get = function(req, res) {
      res.send('NOT IMPLEMENTED: Genre update GET');
  };
  
  // Handle Genre update on POST.
  exports.genre_update_post = function(req, res) {
      res.send('NOT IMPLEMENTED: Genre update POST');
  };
  