text: const router = require('express').Router();
  const sequelize = require('../config/connection');
  const { Post, User, Comment } = require('../models');
  const auth = require('../utils/auth')
  
  //render the dashboard with the users posts
  router.get('/', auth, (req, res) => {
      Post.findAll({
          where: {
              // use the ID from the session
              user_id: req.session.user_id
          },
          attributes: [
              'id',
              'post_content',
              'title',
              'created_at',
              [sequelize.literal('(SELECT COUNT(*) FROM vote WHERE post.id = vote.post_id)'), 'vote_count']
          ],
          include: [
              {
                  model: Comment,
                  attributes: ['id', 'comment_text', 'post_id', 'user_id', 'created_at'],
                  include: {
                      model: User,
                      attributes: ['username']
                  }
              },
              {
                  model: User,
                  attributes: ['username']
              }
          ]
      })
          .then(dbPostData => {
              // serialize data before passing to template
              const posts = dbPostData.map(post => post.get({ plain: true }));
              res.render('dashboard', { posts, loggedIn: true });
          })
          .catch(err => {
              console.log(err);
              res.status(500).json(err);
          });
  });
  
  //route for editing posts
  router.get('/edit/:id', auth, (req, res) => {
      Post.findOne({
          where: {
              id: req.params.id
          },
          attributes: [
              'id',
              'post_content',
              'title',
              'created_at',
              [sequelize.literal('(SELECT COUNT(*) FROM vote WHERE post.id = vote.post_id)'), 'vote_count']
          ],
          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;
              }
  
              // serialize the data
              const post = dbPostData.get({ plain: true });
  
              // pass data to template
              res.render('edit-post', {
                  post,
                  loggedIn: req.session.loggedIn
              });
          })
          .catch(err => {
              console.log(err);
              res.status(500).json(err);
          });
  });
  
  module.exports = router;