Skip to content

Commit

Permalink
file structure changes, building new js code for event handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
RodBennett committed Oct 16, 2022
1 parent ac68016 commit 167285d
Show file tree
Hide file tree
Showing 22 changed files with 154 additions and 211 deletions.
27 changes: 13 additions & 14 deletions controllers/api/comment-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const withAuth = require("../../utils/auth");
router.get("/", async (req, res) => {
try {
const dbCommentData = await Comment.findAll({});

res.json(dbCommentData);
} catch (err) {
console.log(err);
Expand All @@ -17,19 +16,19 @@ router.get("/", async (req, res) => {
});

// GET route for one single comment
router.get("/:id", withAuth, async (req, res) => {
try {
const dbCommentData = await Comment.findByPk({
where: {
id: req.params.id,
},
});
res.json(dbCommentData);
} catch (err) {
console.log(err);
res.status(500).json(err);
}
});
// router.get("/:id", withAuth, async (req, res) => {
// try {
// const dbCommentData = await Comment.findByPk({
// where: {
// id: req.params.id,
// },
// });
// res.json(dbCommentData);
// } catch (err) {
// console.log(err);
// res.status(500).json(err);
// }
// });

// POST route to create a new comment
router.post("/", withAuth, async (req, res) => {
Expand Down
2 changes: 1 addition & 1 deletion controllers/api/post-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ router.get("/:id", withAuth, async (req, res) => {
comment,
logged_in: req.session.logged_in,
});
// res.status(200).json(dbPostData);
res.status(200).json(dbPostData);
} catch (err) {
console.log(err);
res.status(501).json(err);
Expand Down
20 changes: 9 additions & 11 deletions controllers/api/user-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@ const { User, Post, Comment } = require('../../models');
// });

// GET route to find one user
router.get("/:id", async (req, res) => {
router.get("/login", async (req, res) => {
try {
const userData = await User.findByPk({
attributes: { exclude: ["password"] },
const userData = await User.findOne({
where: {
id: req.params.id,
id: req.body.username,
},
include: [
{ model: Post, include: { model: User } }, { model: Post}, { model: Comment }],
Expand All @@ -42,11 +41,10 @@ router.post('/', async (req, res) => {
try {
const userData = await User.create({
username: req.body.username,
email: req.body.email,
password: req.body.password,
});
req.session.save(() => {
req.session.user_id = userData.user_id;
req.session.user_id = userData.id;
req.session.username = userData.username;
req.session.logged_in = true;

Expand All @@ -58,7 +56,7 @@ router.post('/', async (req, res) => {
});

router.get("/signup", (req, res) => {
if (req.session.loggedIn) {
if (req.session.logged_in) {
res.redirect("/");
return;
}
Expand All @@ -68,7 +66,7 @@ router.get("/signup", (req, res) => {
// after login, this redirects user to homepage
router.get("/login", (req, res) => {

if (req.session.loggedIn) {
if (req.session.logged_in) {
res.redirect("/");
return;
}
Expand Down Expand Up @@ -106,11 +104,11 @@ router.post('/login', async (req, res) => {
.json({ message: "Please create user account"})
return;
}
// Once the user successfully logs in, set up the sessions variable 'loggedIn'
// Once the user successfully logs in, set up the sessions variable 'logged_in'
req.session.save(() => {
req.session.user_id = userData.id;
req.session.username = userData.username;
req.session.loggedIn = true;
req.session.logged_in = true;

res
.status(200)
Expand All @@ -126,7 +124,7 @@ router.post('/login', async (req, res) => {
// Logout
router.post('/logout', (req, res) => {
// When the user logs out, destroy the session
if (req.session.loggedIn) {
if (req.session.logged_in) {
req.session.destroy(() => {
res.status(204).end();
});
Expand Down
61 changes: 32 additions & 29 deletions controllers/homeRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,46 @@ router.get("/", async (req, res) => {

res.render("homepage", {
posts,
loggedIn: req.session.loggedIn,
logged_in: req.session.logged_in,
});
} catch (err) {
console.log(err);
res.status(500).json(err);
}
});

// GET one post by id
// router.get(":id", withAuth, async (req, res) => {
// try {
// const dbPostData = await Post.findOne({
// where: {
// id: req.params.id,
// },
// include: [
// { model: User}, { model: Comment, include: {model: User } }],
// });
// if (!dbPostData) {
// res.status(404).json({ message: "No post found with this id" });
// return;
// }

// const post = dbPostData.get({ plain: true });
// const comment = post.comment;
// res.render('readpost', {
// post,
// comment,
// logged_in: req.session.logged_in,
// });
// res.status(200).json(dbPostData);
// } catch (err) {
// console.log(err);
// res.status(501).json(err);
// }
// });

// GET route to redierct user to homepage after signing up
router.get("/signup", (req, res) => {
if (req.session.loggedIn) {
if (req.session.logged_in) {
res.redirect("/");
return;
}
Expand All @@ -32,38 +61,12 @@ router.get("/signup", (req, res) => {

// GET route for loggin in and redirecting to homepage if logged-in
router.get("/login", (req, res) => {
if (req.session.loggedIn) {
if (req.session.logged_in) {
res.redirect("/");
return;
}
res.render("login");
});

// This route allows user to get all data after login and to search posts with comments
// router.get('/posts/:id', withAuth, async (req, res) => {
// try {
// const postData = await Post.findByPk(req.params.id, {
// include:
// [{ model: User }, { model: Comment, include: { model: User } }],
// });

// if (!postData) {
// res.status(400).json({ message: "No post found with this id" });
// return;
// }

// const post = postData.get({ plain: true });
// const comment = post.comment;

// res.render('readpost', {
// post,
// comment,
// logged_in: req.session.logged_in,
// user_id
// });
// } catch (err) {
// res.status(502).json(err);
// }
// });

module.exports = router;
48 changes: 0 additions & 48 deletions public/css/js/createComment.js

This file was deleted.

28 changes: 0 additions & 28 deletions public/css/js/createPost.js

This file was deleted.

24 changes: 0 additions & 24 deletions public/css/js/login.js

This file was deleted.

15 changes: 0 additions & 15 deletions public/css/js/logout.js

This file was deleted.

25 changes: 0 additions & 25 deletions public/css/js/signup.js

This file was deleted.

5 changes: 5 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ main {
margin: 10px 50px;
}

#username-input{
width: 40%;
height: 5vh;
}

#username-signup {
width: 40%;
height: 5vh;
Expand Down
26 changes: 26 additions & 0 deletions public/js/comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const commentFormHandler = async function(event) {
event.preventDefault();

const postId = document.querySelector('input[name="post-id"]').value;
const body = document.querySelector('textarea[name="comment-body"]').value;

if (body) {
await fetch('/api/comment', {
method: 'POST',
body: JSON.stringify({
postId,
body
}),
headers: {
'Content-Type': 'application/json'
}
});

document.location.reload();
}
};

document
.querySelector('#new-comment-form')
.addEventListener('submit', commentFormHandler);

1 change: 1 addition & 0 deletions public/js/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
//const postID = document.querySelector('input[name="post-id")
Loading

0 comments on commit 167285d

Please sign in to comment.