text: const router = require('express').Router();
  const { Post, User, Comment } = require('../models');
  const withAuth = require('../utils/auth');
  
  router.get('/', async (req, res) => {
    try {
      // Get all posts and JOIN with user data
      const postData = await Post.findAll({
        include: [
          { all: true, nested: true },
        ],
      });
    
      // Serialize data so the template can read it
      const posts = postData.map((post) => post.get({ plain: true }));
      // Pass serialized data and session flag into template
      res.render('homepage', {
        posts,
        logged_in: req.session.logged_in
      });
    } catch (err) {
      res.status(500).json(err);
    }
  });
  
  router.get('/post/:id', async (req, res) => {
    try {
      const postData = await Post.findByPk(req.params.id, {
        include: [
          {
            model: User,
            attributes: ['username'],
          },
          {
            model: Comment,
            include: [
              {
                model: User
              }
            ]
          }
        ],
      });
  
      const post = postData.get({ plain: true });
      res.render('post', {
        ...post,
        logged_in: req.session.logged_in
      });
    } catch (err) {
      res.status(500).json(err);
    }
  });
  
  // Use withAuth middleware to prevent access to route
  router.get('/dashboard', withAuth, async (req, res) => {
    try {
      // Find the logged in user based on the session ID
      const userData = await User.findByPk(req.session.user_id, {
        attributes: { exclude: ['password'] },
        include: [{ model: Post }],
      });
  
      const user = userData.get({ plain: true });
      
      res.render('dashboard', {
        ...user,
        logged_in: true
      });
    } catch (err) {
      res.status(500).json(err);
    }
  });
  
  router.get('/login', (req, res) => {
    // If the user is already logged in, redirect the request to another route
    if (req.session.logged_in) {
      res.redirect('/');
      return;
    }
    res.render('login');
  });
  
  router.get('/newpost', withAuth, (req, res) => {
    res.render('newPost', {
      // can only go on new post page if the user is logged in
      // therefore, logged_in has to be true
      logged_in: true
    });
  });
  
  router.get('/edit/:id', withAuth, async (req, res) => {
    try {
      const postData = await Post.findByPk(req.params.id, {
        include: [
          {
            model: User,
            attributes: ['username'],
          }
        ],
      });
  
      const post = postData.get({ plain: true });
      if (post.user_id == req.session.user_id) {
        res.render('edit', {
          ...post,
          logged_in: req.session.logged_in
        });
      }
      else {
        res.render('error', {
          message: "You do not have access"
        });
      }
    } catch (err) {
      res.status(500).json(err);
    }
  });
  
  module.exports = router;
  