Skip to content

Commit

Permalink
updated routes files and added comments throughout
Browse files Browse the repository at this point in the history
  • Loading branch information
fredm23579 committed Apr 13, 2024
1 parent d8aa5d4 commit 375b911
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 230 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ node_modules
.DS_Store
.vscode
ensemble-blog
.gitignore
.github
78 changes: 39 additions & 39 deletions controllers/api-routes.js
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
const router = require('express').Router();
const { User, Post, Comment } = require('../models');
const router = require('express').Router(); // import express router (https://expressjs.com/en/4x/api.html#router)
const { User, Post, Comment } = require('../models'); // import models (https://sequelize.org/docs/v6/core-concepts/model-basics/)

// In one of the route files, like routes/authRoutes.js
// add new comments
router.post('/post/:id', async (req, res) => {
// In one of the route files, like routes/authRoutes.js we have an app.use() statement that calls app.use('/api', apiRoutes). This means that all the routes in apiRoutes will be added to the /api path. So, if we have a route in apiRoutes that looks like this: router.get('/users', (req, res) => { ... }), the full path to that route will be /api/users.
// In the same file, we have another app.use() statement that calls app.use('/auth', authRoutes). This means that all the routes in authRoutes will be added to the /auth path. So, if we have a route in authRoutes that looks like this: router.get('/login', (req, res) => { ... }), the full path to that route will be /auth/login.
router.post('/post/:id', async (req, res) => { // add comment to post (https://expressjs.com/en/4x/api.html#router.post) (https://sequelize.org/master/manual/model-querying-basics.html)
try {
const newComment = {
content: req.body.content,
creator: req.session.username,
post_id: req.params.id,
const newComment = { // create new comment (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html)
content: req.body.content, // content from request body (https://expressjs.com/en/4x/api.html#req.body)
creator: req.session.username, // creator from session (https://expressjs.com/en/4x/api.html#req.session) (https://sequelize.org/master/manual/model-querying-basics.html)
post_id: req.params.id, // post id from request params (https://expressjs.com/en/4x/api.html#req.params) (https://sequelize.org/master/manual/model-querying-basics.html)
}
await Comment.create(newComment);
res.redirect(`/post/${req.params.id}`);
await Comment.create(newComment); // create new comment (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html)
res.redirect(`/post/${req.params.id}`); // go back to post we just commented on (https://expressjs.com/en/4x/api.html#res.redirect) (https://sequelize.org/master/manual/model-querying-basics.html)
} catch (err) {
res.status(500).send(err);
res.status(500).send(err); // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html)
}
});

// create new post
router.post('/dashboard', async (req, res) => {
// create new post (https://expressjs.com/en/4x/api.html#router.post) (https://sequelize.org/master/manual/model-querying-basics.html) for more information on how to use the create method
router.post('/dashboard', async (req, res) => { // create new post (https://expressjs.com/en/4x/api.html#router.post) (https://sequelize.org/master/manual/model-querying-basics.html)
try {
const newPost = {
title: req.body.title,
content: req.body.content,
creator: req.session.username,
const newPost = { // create new post (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html)
title: req.body.title, // title from request body (https://expressjs.com/en/4x/api.html#req.body)
content: req.body.content, // content from request body (https://expressjs.com/en/4x/api.html#req.body)
creator: req.session.username, // creator from session (https://expressjs.com/en/4x/api.html#req.session)
}
await Post.create(newPost);
res.redirect(`/dashboard`);
} catch (err) {
res.status(500).send(err);
await Post.create(newPost); // create new post (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html)
res.redirect(`/dashboard`); // go back to dashboard (https://expressjs.com/en/4x/api.html#res.redirect) (https://sequelize.org/master/manual/model-querying-basics.html)
} catch (err) { // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html)
res.status(500).send(err); // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html)
}
});

// update post
router.put('/post/:id/edit', async (req, res) => {
// update post (https://expressjs.com/en/4x/api.html#router.put) (https://sequelize.org/master/manual/model-querying-basics.html) for more information on how to use the update method
router.put('/post/:id/edit', async (req, res) => { // update post (https://expressjs.com/en/4x/api.html#router.put) (https://sequelize.org/master/manual/model-querying-basics.html)
try {
await Post.update(
await Post.update( // update post (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html)
{
title: req.body.title,
content: req.body.content,
title: req.body.title, // title from request body (https://expressjs.com/en/4x/api.html#req.body)
content: req.body.content, // content from request body (https://expressjs.com/en/4x/api.html#req.body) (https://sequelize.org/master/manual/model-querying-basics.html)
},
{
where: {
id: req.params.id,
creator: req.session.username,
where: { // where condition for post (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html)
id: req.params.id, // post id from request params (https://expressjs.com/en/4x/api.html#req.params) (https://sequelize.org/master/manual/model-querying-basics.html)
creator: req.session.username, // make sure user owns the post (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html)
},
}
);
res.redirect(`/post/${req.params.id}`); // go back to post we just edited
} catch (err) {
res.status(500).json(err);
res.redirect(`/post/${req.params.id}`); // go back to post we just edited (https://expressjs.com/en/4x/api.html#res.redirect) (https://sequelize.org/master/manual/model-querying-basics.html)
} catch (err) { // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html)
res.status(500).json(err); // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html)
}
});

// delete post
router.delete('/post/:id/edit', async (req, res) => {
try {
await Post.destroy({
where: {
id: req.params.id,
// delete post (https://expressjs.com/en/4x/api.html#router.delete) (https://sequelize.org/master/manual/model-querying-basics.html) for more information on how to use the destroy method
router.delete('/post/:id/edit', async (req, res) => { // delete post (https://expressjs.com/en/4x/api.html#router.delete) (https://sequelize.org/master/manual/model-querying-basics.html)
try { // delete post (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html)
await Post.destroy({ // destroy post (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html)
where: { // where condition for post (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html)
id: req.params.id, // post id from request params (https://expressjs.com/en/4x/api.html#req.params) (https://sequelize.org/master/manual/model-querying-basics.html)
creator: req.session.username, // make sure user owns the post
}
});
Expand Down
Loading

0 comments on commit 375b911

Please sign in to comment.