Skip to content

Commit

Permalink
models/index.js fixed, but not seeding properly
Browse files Browse the repository at this point in the history
  • Loading branch information
RodBennett committed Oct 9, 2022
1 parent e494fae commit 5ca713b
Show file tree
Hide file tree
Showing 25 changed files with 861 additions and 195 deletions.
4 changes: 2 additions & 2 deletions controllers/api/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const router = require('express').Router();
const userRoutes = require('./userRoutes');
const postRoutes = require('./postRoutes');
const userRoutes = require('./user-routes');
const postRoutes = require('./post-routes');

router.use('/users', userRoutes);
router.use('/posts', postRoutes);
Expand Down
39 changes: 39 additions & 0 deletions controllers/api/post-routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const router = require('express').Router();
const { Post, User } = require('../../models');
const withAuth = require('../../utils/auth');

router.post('/', withAuth, async (req, res) => {
try {
const newPost = await Post.create({
...req.body,
user_id: req.session.user_id,
});

res.status(200).json(newPost);
} catch (err) {
res.status(400).json(err);
}
});

router.delete('/posts/:id', withAuth, async (req, res) => {
try {
const postData = await Post.destroy({
where: {
id: req.params.id,
user_id: req.session.user_id,
},
});

if (!postData) {
res.status(404).json({ message: 'No project found with this id!' });
return;
}

res.status(200).json(projectData);
} catch (err) {
res.status(500).json(err);
}
});


module.exports = router;
79 changes: 0 additions & 79 deletions controllers/api/postRoutes.js

This file was deleted.

File renamed without changes.
110 changes: 71 additions & 39 deletions controllers/homeRoutes.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@
const router = require('express').Router();
const { Posts, User } = require('../models')
const { Post, User } = require('../models')
const withAuth = require('../utils/auth')

// endpoint /login

// route for redirecting user to the homepage after account is created
router.get("/signup", (req, res) => {
if (req.session.loggedIn) {
res.redirect("/");
return;
}
res.render("signup");
});

// GET data for homepage
router.get('/', async (req, res) => {
try {
const postData = await Posts.findAll({
include: [
{
model: User,
attributes: ['username']
}
]
const postData = await Post.findAll({
include:
[{ model: User }],
});

// after login, this redirects user to homepage
router.get("/login", (req, res) => {
if (req.session.loggedIn) {
res.redirect("/");
return;
}
console.log("++++++++++++You are Logged in++++++++++++++")
res.render("login");
});

// creates an array / map of all posts and lists them on homepage
const posts = postData.map((post) => post.get({ plain: true }));
res.render('homepage', {
Expand All @@ -24,65 +42,79 @@ router.get('/', async (req, res) => {
}
});

// Login route

// This route allows user to get all data after login and to search posts
router.get('/posts/:id', withAuth, async (req, res) => {
try {
const postdata = await Posts.findByPk(req.params.id, {
include: [
{
model: User,
attributes: ['name'],
},
],
const postData = await Post.findByPk(req.params.id, {
include:
[{ model: Post, include: { model: User } }, { model: User}],
});
const post = postdata.get({ plain: true });
res.render('readPost', {
...post,
const posts = postData.get({ plain: true });
res.render('readpost', {
posts,
logged_in: req.session.logged_in,
user
});
} catch (err) {
res.status(500).json(err);
}
});

router.get('/homepage', withAuth, async (req, res) => {
// GET enitre posts for users who are logged in
router.get('/posts', withAuth, async (req, res) => {
try {
// Find the logged in user based on the session ID
const userData = await User.findByPk(req.session.user_id, {
const postData = await User.findByPk(req.session.user_id, {
attributes: { exclude: ['password'] },
include: [{ model: Posts }],
include: [{ model: Post, include: {model: User } }, { model: User }],
});

const user = userData.get({ plain: true });
const posts = postData.get({ plain: true });

res.render('homepage', {
...user,
res.render('readpost', {
...posts,
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('/homepage');
return;
router.post('/', withAuth, async (req, res) => {
try {
const newPost = await Post.create({
...req.body,
user_id: req.session.user_id,
});

res.status(200).json(newPost);
} catch (err) {
res.status(400).json(err);
}
res.render('login');
});

// route for redirecting user to sign up page if no account exists, and then to homepage after creating login
router.delete('/posts/:id', withAuth, async (req, res) => {
try {
const postData = await Post.destroy({
where: {
id: req.params.id,
user_id: req.session.user_id,
},
});

router.get("/signup", (req, res) => {
if (req.session.loggedIn) {
res.redirect("/");
return;
}
if (!postData) {
res.status(404).json({ message: 'No project found with this id!' });
return;
}

res.render("signup");
res.status(200).json(projectData);
} catch (err) {
res.status(500).json(err);
}
});


module.exports = router;


module.exports = router;
41 changes: 41 additions & 0 deletions models/Comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const { Model, DataTypes } = require('sequelize');
const sequelize = require('../config/connection');

class Comment extends Model {}

Comment.init(
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
content: {
type: DataTypes.STRING,
allowNull: false,
},
post_id: {
type: DataTypes.INTEGER,
references: {
model: 'post',
key: 'id',
},
},
user_id: {
type: DataTypes.INTEGER,
references: {
model: 'user',
key: 'id',
},
},
},
{
sequelize,
freezeTableName: true,
underscored: true,
modelName: 'comment',
}
);

module.exports = Comment;
10 changes: 5 additions & 5 deletions models/Posts.js → models/Post.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const { Model, DataTypes } = require('sequelize');
const sequelize = require('../config/connection');

class Posts extends Model {}
class Post extends Model {}

Posts.init(
Post.init(
{
id: {
type: DataTypes.INTEGER,
Expand Down Expand Up @@ -32,16 +32,16 @@ Posts.init(
type: DataTypes.INTEGER,
references: {
model: 'user',
key: 'id',
key: 'id'
},
},
},
{
sequelize,
freezeTableName: true,
underscored: true,
modelName: 'posts',
modelName: 'post',
}
);

module.exports = Posts;
module.exports = Post;
Loading

0 comments on commit 5ca713b

Please sign in to comment.