text: const router = require("express").Router();
  const { User, Post, Comment } = require("../models");
  const withAuth = require("../utils/auth");
  
  router.get("/", withAuth, async (req, res) => {
    try {
      const postData = await Post.findAll({
        include: [
          {
            model: User,
            attributes: ["name"],
          },
        ],
      });
      const posts = postData.map((post) => post.get({ plain: true }));
      res.render("homepage", {
        posts,
        logged_in: req.session.logged_in,
      });
    } catch (err) {
      res.status(500).json(err);
    }
  });
  
  router.get("/login", (req, res) => {
    if (req.session.logged_in) {
      res.redirect("/");
      return;
    }
    res.render("login");
  });
  
  router.get("/posts/:id", withAuth, async (req, res) => {
    try {
      const postData = await Post.findByPk(req.params.id, {
        attributes: ["id", "title", "post", "user_id"],
        include: [
          {
            model: User,
            attributes: ["name"],
          },
          {
            model: Comment,
            attributes: ["comment"],
            include: {
              model: User,
              attributes: ["name"],
            },
          },
        ],
      });
  
      const post = postData.get({ plain: true });
  
      res.render("single-post", {
        ...post,
        user_id: req.session.user_id,
        logged_in: req.session.logged_in,
        logged_name: req.session.logged_name,
      });
    } catch (error) {
      console.log(error);
      res.status(500).json(error);
    }
  });
  
  router.get("/signup", (req, res) => {
    if (req.session.logged_in) {
      res.redirect("/");
      return;
    }
    res.render("signup");
  });
  
  module.exports = router;
  