text: const router = require( 'express' ).Router();
  const { Post, User, Comment } = require( '../../models' );
  const sequelize = require( '../../config/connection' );
  const withAuth = require( '../../utils/auth' );
  
  // GET all posts - /api/posts
  router.get('/', ( req, res ) => {
      Post.findAll( {
              attributes: [ 
                  'id', 
                  'title',
                  'content', 
                  'created_at',
                  'updated_at',
                  [ sequelize.literal( '(SELECT COUNT(*) FROM comment WHERE post.id = comment.post_id)' ), 'num_comments' ],
                  [ sequelize.literal( '(SELECT MAX(created_at) FROM comment WHERE post.id = comment.post_id)' ), 'recent_post' ]   
              ],
              include: [
                  {
                      model: Comment,
                      attributes: ['id', 'comment_text', 'post_id', 'user_id', 'created_at'],
                      include: {
                          model: User,
                          attributes: ['username']
                      }
                  },
                  {
                      model: User,
                      attributes: ['username']
                  }
              ],
              order: [ [ 'updated_at', 'DESC' ] ]
          } )
          .then( dbPostData => res.json( dbPostData ) )
          .catch( err => {
              console.log( err );
              res.status( 500 ).json( err );
          } );
  } );
  
      // GET one post /api/posts/1
  router.get('/:id', ( req, res ) => {
      Post.findOne( {
          where: {
              id: req.params.id
          },
          attributes: [ 
              'id', 
              'title',
              'content', 
              'created_at',
              'updated_at',
              [ sequelize.literal( '(SELECT COUNT(*) FROM comment WHERE post.id = comment.post_id)' ), 'num_comments' ],
              [ sequelize.literal( '(SELECT MAX(created_at) FROM comment WHERE post.id = comment.post_id)' ), 'recent_post' ]
          ],
          include: [
              {
                  model: Comment,
                  attributes: ['id', 'comment_text', 'post_id', 'user_id', 'created_at'],
                  include: {
                      model: User,
                      attributes: ['username']
                  }
              },
              {
                  model: User,
                  attributes: ['username']
              }
          ]
      } )
      .then( dbPostData => {
          if( !dbPostData ) {
              res.status( 404 ).json( { message: 'No post found with this id' } );
              return;
          }
          res.json( dbPostData );
      } )
      .catch( err => {
          console.log( err );
          res.status( 500 ).json( err );
      } )
  } );
  
      // POST new post /api/posts
  router.post('/', withAuth, ( req, res ) => {
      // expects { title: 'bbb', content: 'bbb', user_id: 'bbb' }
      Post.create( { 
          title: req.body.title,
          content: req.body.content,
          user_id: req.session.user_id
      } )
      .then( dbPostData => {
          res.json( dbPostData )
      } )
      .catch( err => {
          console.log( err )
          res.status( 500 ).json( err );
      } );
  } );
  
  
      // UPDATE post title
  router.put( '/', withAuth, ( req, res ) => {
      Post.update( 
          {
              title: req.body.title,
              content: req.body.content
          } ,
          {
              where: {
                  id: req.body.post_id
              }
          }
      )
      .then( dbPostData => {
          if( !dbPostData ) {
              res.status( 404 ).json( { message: 'No post found with this id' } );
              return;
          }
          res.json( dbPostData );
      } )
      .catch( err => {
          console.log( err );
          res.status( 500 ).json( err );
      } );
  } );
  
      // DELETE /api/posts/1
  router.delete('/:id', withAuth, ( req, res ) => {
      Post.destroy( {
          where: {
              id: req.params.id
          }
      } )
      .then( dbPostData => {
          if( !dbPostData ) {
              res.status( 404 ).json( { message: 'No post found with this id' } );
              return;
          }
          res.json( dbPostData );
      } )
      .catch( err => {
          console.log( err )
          res.status( 500 ).json( err )
      } );
  } );
  
  module.exports = router
  