From 3a3b208a3a49f09e9c231a3c08098c71c538ac59 Mon Sep 17 00:00:00 2001 From: fredm23579 Date: Sat, 13 Apr 2024 13:21:31 -0700 Subject: [PATCH] updated routes files and added comments throughout --- config/connection.js | 28 +++++----- controllers/api-routes.js | 110 +++++++++++++++++++------------------- models/Comment.js | 50 ++++++++--------- models/Post.js | 50 ++++++++--------- models/User.js | 38 ++++++------- public/js/app.js | 24 ++++----- seeds/seed.js | 28 +++++----- 7 files changed, 164 insertions(+), 164 deletions(-) diff --git a/config/connection.js b/config/connection.js index e8150b5..fdc9b40 100755 --- a/config/connection.js +++ b/config/connection.js @@ -1,21 +1,21 @@ -const Sequelize = require('sequelize'); -require('dotenv').config(); +const Sequelize = require('sequelize'); // import sequelize (https://sequelize.org/docs/v6/getting-started/) +require('dotenv').config(); // import dotenv (https://www.npmjs.com/package/dotenv) (https://github.com/sindresorhus/dotenv) -let sequelize; +let sequelize; // sequelize object (https://sequelize.org/docs/v6/getting-started/) -if (process.env.JAWSDB_URL) { - sequelize = new Sequelize(process.env.JAWSDB_URL); -} else { - sequelize = new Sequelize( - process.env.DB_NAME, - process.env.DB_USER, - process.env.DB_PASSWORD, +if (process.env.JAWSDB_URL) { // if JAWSDB_URL is defined (https://devcenter.heroku.com/articles/jawsdb) + sequelize = new Sequelize(process.env.JAWSDB_URL); // use JAWSDB_URL (https://devcenter.heroku.com/articles/jawsdb) +} else { // if JAWSDB_URL is not defined (https://devcenter.heroku.com/articles/jawsdb) + sequelize = new Sequelize( // use local database (https://sequelize.org/docs/v6/getting-started/) + process.env.DB_NAME, // database name (https://sequelize.org/docs/v6/getting-started/) + process.env.DB_USER, // database user (https://sequelize.org/docs/v6/getting-started/) + process.env.DB_PASSWORD, // database password (https://sequelize.org/docs/v6/getting-started/) { - host: 'localhost', - dialect: 'mysql', - port: 3306 + host: 'localhost', // database host (https://sequelize.org/docs/v6/getting-started/) + dialect: 'mysql', // database dialect (https://sequelize.org/docs/v6/getting-started/) + port: 3306 // database port (https://sequelize.org/docs/v6/getting-started/) } ); } -module.exports = sequelize; \ No newline at end of file +module.exports = sequelize; // export sequelize object (https://sequelize.org/docs/v6/getting-started/) \ No newline at end of file diff --git a/controllers/api-routes.js b/controllers/api-routes.js index f72fc59..de40564 100755 --- a/controllers/api-routes.js +++ b/controllers/api-routes.js @@ -62,89 +62,89 @@ router.delete('/post/:id/edit', async (req, res) => { // delete post (https://ex creator: req.session.username, // make sure user owns the post } }); - res.redirect('/dashboard'); // redirect to dashboard - } catch (err) { - console.log(err); - res.status(500).json(err); + res.redirect('/dashboard'); // redirect 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) + console.log(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).json(err); // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) } }); -// login handling -router.post('/login', async (req, res) => { +// login handling (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 checkPassword method +router.post('/login', async (req, res) => { // login handling (https://expressjs.com/en/4x/api.html#router.post) (https://sequelize.org/master/manual/model-querying-basics.html) - // checking if login field was submitted because we are handling login/signup at once - if (req.body.loginuser) { + // checking if login field was submitted because we are handling login/signup at once and not separately (https://expressjs.com/en/4x/api.html#router.post) (https://sequelize.org/master/manual/model-querying-basics.html) + if (req.body.loginuser) { // if login submitted handle that first before signing up (https://expressjs.com/en/4x/api.html#router.post) (https://sequelize.org/master/manual/model-querying-basics.html) try { - const dbUserData = await User.findOne({ - where: { - username: req.body.loginuser, + const dbUserData = await User.findOne({ // find user (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html) + where: { // where condition for user (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html) + username: req.body.loginuser, // username from request body (https://expressjs.com/en/4x/api.html#req.body) (https://sequelize.org/master/manual/model-querying-basics.html) } }); - if (!dbUserData) { + if (!dbUserData) { // if no user found send error (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) res - .status(400).redirect('/login') - .json('Incorrect username or password. Please try again!'); - return; + .status(400).redirect('/login') // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) + .json('Incorrect username or password. Please try again!'); // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) + return; // return to prevent further execution (https://expressjs.com/en/4x/api.html#router.post) (https://sequelize.org/master/manual/model-querying-basics.html) } - const validPassword = await dbUserData.checkPassword(req.body.loginpassword); + const validPassword = await dbUserData.checkPassword(req.body.loginpassword); // check password (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html) - if (!validPassword) { + if (!validPassword) { // if password is incorrect send error (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) res - .status(400).redirect('/login') - .send('Incorrect username or password. Please try again!'); - return; + .status(400).redirect('/login') // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) + .send('Incorrect username or password. Please try again!'); // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) + return; // return to prevent further execution (https://expressjs.com/en/4x/api.html#router.post) (https://sequelize.org/master/manual/model-querying-basics.html) } - req.session.save(() => { - req.session.loggedIn = true; - req.session.username = dbUserData.username; - res.redirect('/login'); - }); - } catch (err) { - console.log(err); + req.session.save(() => { // save session (https://expressjs.com/en/4x/api.html#req.session.save) (https://sequelize.org/master/manual/model-querying-basics.html) + req.session.loggedIn = true; // set loggedIn to true (https://expressjs.com/en/4x/api.html#req.session) (https://sequelize.org/master/manual/model-querying-basics.html) + req.session.username = dbUserData.username; // set username (https://expressjs.com/en/4x/api.html#req.session) (https://sequelize.org/master/manual/model-querying-basics.html) + res.redirect('/login'); // redirect to login page (https://expressjs.com/en/4x/api.html#res.redirect) (https://sequelize.org/master/manual/model-querying-basics.html) + }); // end save session (https://expressjs.com/en/4x/api.html#req.session.save) (https://sequelize.org/master/manual/model-querying-basics.html) + } catch (err) { // catch error (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) + console.log(err); // log error (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) } - } else if (req.body.signupuser) { // if no login submitted, we know user is signing up instead so handle that - const newUser = { - username: req.body.signupuser, - password: req.body.signuppassword + } else if (req.body.signupuser) { // if no login submitted, we know user is signing up instead so handle that here (https://expressjs.com/en/4x/api.html#router.post) + const newUser = { // create new user object (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html) + username: req.body.signupuser, // username from request body (https://expressjs.com/en/4x/api.html#req.body) (https://sequelize.org/master/manual/model-querying-basics.html) + password: req.body.signuppassword // password from request body (https://expressjs.com/en/4x/api.html#req.body) (https://sequelize.org/master/manual/model-querying-basics.html) } try { - await User.create(newUser); - req.session.save(() => { - req.session.loggedIn = true; - req.session.username = newUser.username; - res.redirect('/dashboard'); + await User.create(newUser); // create new user (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html) + req.session.save(() => { // save session (https://expressjs.com/en/4x/api.html#req.session.save) (https://sequelize.org/master/manual/model-querying-basics.html) (https:// + req.session.loggedIn = true; // set loggedIn to true (https://expressjs.com/en/4x/api.html#req.session) (https://sequelize.org/master/manual/model-querying-basics.html) + req.session.username = newUser.username; // set username (https://expressjs.com/en/4x/api.html#req.session) (https://sequelize.org/master/manual/model-querying-basics.html) + res.redirect('/dashboard'); // redirect to dashboard page (https://expressjs.com/en/4x/api.html#res.redirect) (https://sequelize.org/master/manual/model-querying-basics.html) }); - } catch (err) { - if (err.name === 'SequelizeUniqueConstraintError') { // handle existing username + } catch (err) { // catch error (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) + if (err.name === 'SequelizeUniqueConstraintError') { // handle existing username error (https://sequelize.org/master/manual/model-querying-basics.html) res - .status(400) - .send('Username already exists! Please try again.'); - } else if (err.name === 'SequelizeValidationError') { // handle password length error + .status(400) // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) + .send('Username already exists! Please try again.'); // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) + } else if (err.name === 'SequelizeValidationError') { // handle password length error (https://sequelize.org/master/manual/model-querying-basics.html) res - .status(400) - .send('Password must be at least 8 characters long.'); - } else { + .status(400) // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) + .send('Password must be at least 8 characters long.'); // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) + } else { // handle other errors (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) res - .status(400) - .send(err); + .status(400) // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) + .send(err); // send error status code (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) } - return; + return; // return (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) } } }); -router.post('/logout', (req, res) => { - if (req.session.loggedIn) { - req.session.destroy(() => { - res.status(204).end(); +router.post('/logout', (req, res) => { // logout route (https://expressjs.com/en/4x/api.html#router.post) (https://sequelize.org/master/manual/model-querying-basics.html) + if (req.session.loggedIn) { // if user is logged in (https://expressjs.com/en/4x/api.html#req.session) (https://sequelize.org/master/manual/model-querying-basics.html) + req.session.destroy(() => { // destroy session (https://expressjs.com/en/4x/api.html#req.session) (https://sequelize.org/master/manual/model-querying-basics.html) + res.status(204).end(); // send status code 204 (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) }); - res.redirect('/login'); - } else { - res.status(404).end(); + res.redirect('/login'); // redirect to login page (https://expressjs.com/en/4x/api.html#res.redirect) (https://sequelize.org/master/manual/model-querying-basics.html) + } else { // if user is not logged in (https://expressjs.com/en/4x/api.html#req.session) (https://sequelize.org/master/manual/model-querying-basics.html) + res.status(404).end(); // send status code 404 (https://expressjs.com/en/4x/api.html#res.status) (https://sequelize.org/master/manual/model-querying-basics.html) } }); -module.exports = router; \ No newline at end of file +module.exports = router; // export router (https://expressjs.com/en/4x/api.html#router) (https://sequelize.org/master/manual/model-querying-basics.html) (https://sequelize.org/master/manual/model-querying-basics.html) \ No newline at end of file diff --git a/models/Comment.js b/models/Comment.js index e2cfd06..ce9c71b 100755 --- a/models/Comment.js +++ b/models/Comment.js @@ -3,36 +3,36 @@ const sequelize = require('../config/connection'); class Comment extends Model {} // Comment model (https://sequelize.org/docs/v6/core-concepts/model-basics/) -Comment.init( +Comment.init( // Comment model (https://sequelize.org/docs/v6/core-concepts/model-basics/) { - id: { - type: DataTypes.INTEGER, - allowNull: false, - primaryKey: true, - autoIncrement: true, + id: { // id column (https://sequelize.org/docs/v6/core-concepts/model-basics/#column-options) + type: DataTypes.INTEGER, // data type (https://sequelize.org/master/identifiers.html) + allowNull: false, // allow null (https://sequelize.org/docs/v6/core-concepts/model-basics/#allowing-nulls) + primaryKey: true, // primary key (https://sequelize.org/docs/v6/core-concepts/model-basics/#primary-keys) + autoIncrement: true, // auto increment (https://sequelize.org/docs/v6/core-concepts/model-basics/#auto-incrementing) }, - content: { - type: DataTypes.STRING, - allowNull: false, + content: { // content column (https://sequelize.org/docs/v6/core-concepts/model-basics/#column-options) + type: DataTypes.STRING, // data type (https://sequelize.org/master/identifiers.html) + allowNull: false, // allow null (https://sequelize.org/docs/v6/core-concepts/model-basics/#allowing-nulls) }, - creator: { - type: DataTypes.STRING, - references: { - model: 'user', - key: 'username', + creator: { // foreign key for username (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) + type: DataTypes.STRING, // data type (https://sequelize.org/master/identifiers.html) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) + references: { // foreign key for username (https //sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) + model: 'user', // model (https://sequelize.org/docs /v6/core-concepts/assocs/#foreign-keys) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) + key: 'username', // foreign key for username (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) (https://sequelize.org } }, - date_created: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, // records date/time at time of creation + date_created: { // https://sequelize.org/docs/v6/core-concepts/model-basics/#column-options (https://sequelize.org/docs/v6/core-concepts/model-basics/#column-options) + type: DataTypes.DATE, // data type (https://sequelize.org/master/identifiers.html) (https://sequelize.org/docs/v6/core-concepts/model-basics/#data-types) + allowNull: false, // allow null (https://sequelize.org/docs/v6/core-concepts/model-basics/#allowing-nulls) + defaultValue: DataTypes.NOW, // records date/time at time of creation (https://sequelize.org/docs/v6/core-concepts/model-basics/#default-values) }, - post_id: { // foreign key for post id (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) - type: DataTypes.INTEGER, - allowNull: false, - references: { - model: 'post', - key: 'id', // foreign key for post id + post_id: { // foreign key for post id (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) + type: DataTypes.INTEGER, // data type (https://sequelize.org/master/identifiers.html) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) + allowNull: false, // allow null (https://sequelize.org/docs/v6/core-concepts/model-basics/#allowing-nulls) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) + references: { // foreign key for post id (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) + model: 'post', // model (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) + key: 'id', // foreign key for post id (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) } } }, @@ -44,4 +44,4 @@ Comment.init( } ); -module.exports = Comment; \ No newline at end of file +module.exports = Comment; // export Comment model (https://sequelize.org/docs/v6/core-concepts/model-basics/) (https://sequelize.org/docs/v6/core-concepts/model-basics/) (https://github \ No newline at end of file diff --git a/models/Post.js b/models/Post.js index f695529..d2063d3 100755 --- a/models/Post.js +++ b/models/Post.js @@ -5,39 +5,39 @@ class Post extends Model {} // Post model (https://sequelize.org/docs/v6/core-co Post.init( // Post model (https://sequelize.org/docs/v6/core-concepts/model-basics/) { - id: { - type: DataTypes.INTEGER, - allowNull: false, - primaryKey: true, - autoIncrement: true, + id: { // Post model (https://sequelize.org/docs/v6/core-concepts/model-basics/) (https://sequelize.org/docs/v6/core-concepts/model-basics/#column-options) + type: DataTypes.INTEGER, // data type (https://sequelize.org/master/identifiers.html) (https://sequelize.org/docs/v6/core-concepts/model-basics/#data-types) + allowNull: false, // allow null (https://sequelize.org/docs/v6/core-concepts/model-basics/#allowing-nulls) (https://sequelize.org/docs/v6/core-concepts/model-basics/#primary-keys) + primaryKey: true, // primary key (https://sequelize.org/docs/v6/core-concepts/model-basics/#primary-keys) (https://sequelize.org/docs/v6/core-concepts/model-basics/#auto-incrementing) + autoIncrement: true, // auto increment (https://sequelize.org/docs/v6/core-concepts/model-basics/#auto-incrementing) }, - title: { - type: DataTypes.STRING, - allowNull: false, + title: { // title column (https://sequelize.org/docs/v6/core-concepts/model-basics/#column-options) (https://sequelize.org/docs/v6/core-concepts/model-basics/#uniques) + type: DataTypes.STRING, // data type (https://sequelize.org/master/identifiers.html) (https://sequelize.org/docs/v6/core-concepts/model-basics/#data-types) + allowNull: false, // allow null (https://sequelize.org/docs/v6/core-concepts/model-basics/#allowing-nulls) (https://sequelize.org/docs/v6/core-concepts/model-basics/#uniques) }, - content: { - type: DataTypes.STRING, - allowNull: false, + content: { // content column (https://sequelize.org/docs/v6/core-concepts/model-basics/#column-options) (https://sequelize.org/docs/v6/core-concepts/model-basics/#uniques) + type: DataTypes.STRING, // data type (https://sequelize.org/master/identifiers.html) (https://sequelize.org/docs/v6/core-concepts/model-basics/#data-types) + allowNull: false, // allow null (https://sequelize.org/docs/v6/core-concepts/model-basics/#allowing-nulls) (https://sequelize.org/docs/v6/core-concepts/model-basics/#uniques) }, - creator: { - type: DataTypes.STRING, - references: { - model: 'user', - key: 'username', + creator: { // foreign key for username (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) + type: DataTypes.STRING, // data type (https://sequelize.org/master/identifiers.html) (https://sequelize.org/docs/v6/core-concepts/model-basics/#data-types) + references: { // references (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) + model: 'user', // model (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) + key: 'username', // key (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) (https://sequelize.org/docs/v6/core-concepts/assocs/#foreign-keys) } }, - date_created: { - type: DataTypes.DATE, - allowNull: false, - defaultValue: DataTypes.NOW, // records date/time at time of creation + date_created: { // date created column (https://sequelize.org/docs/v6/core-concepts/model-basics/#column-options) (https://sequelize.org/docs/v6/core-concepts/model-basics/#uniques) + type: DataTypes.DATE, // data type (https://sequelize.org/master/identifiers.html) (https://sequelize.org/docs/v6/core-concepts/model-basics/#data-types) + allowNull: false, // allow null (https://sequelize.org/docs/v6/core-concepts/model-basics/#allowing-nulls) (https://sequelize.org/docs/v6/core-concepts/model-basics/#uniques) + defaultValue: DataTypes.NOW, // records date/time at time of creation (https://sequelize.org/docs/v6/core-concepts/model-basics/#default-values) }, }, { - sequelize, - freezeTableName: true, - underscored: true, - modelName: 'post', + sequelize, // for connecting to db with sequelize (default is ./config/connection.js) (https://sequelize.org/docs/v6/core-concepts/model-basics/) + freezeTableName: true, // for connecting to db with sequelize (default is ./config/connection.js) (https://sequelize.org/docs/v6/core-concepts/model-basics/) + underscored: true, // for connecting to db with sequelize (default is ./config/connection.js) (https://sequelize.org/docs/v6/core-concepts/model-basics/) + modelName: 'post', // for connecting to db with sequelize (default is ./config/connection.js) (https://sequelize.org/docs/v6/core-concepts/model-basics/) } ); -module.exports = Post; \ No newline at end of file +module.exports = Post; // export Post model (https://sequelize.org/docs/v6/core-concepts/model-basics/) (https://sequelize.org/docs/v6/core-concepts/model-basics/) \ No newline at end of file diff --git a/models/User.js b/models/User.js index 9a41a30..89e197e 100755 --- a/models/User.js +++ b/models/User.js @@ -10,22 +10,22 @@ class User extends Model { // User model (https://sequelize.org/docs/v6/core-con User.init( // User model (https://sequelize.org/docs/v6/core-concepts/model-basics/) { - id: { - type: DataTypes.INTEGER, - allowNull: false, - primaryKey: true, - autoIncrement: true, + id: { // id column (https://sequelize.org/docs/v6/core-concepts/model-basics/#column-options) + type: DataTypes.INTEGER, // data type (https://sequelize.org/master/identifiers.html) + allowNull: false, // allow null (https://sequelize.org/docs/v6/core-concepts/model-basics/#allowing-nulls) + primaryKey: true, // primary key (https://sequelize.org/docs/v6/core-concepts/model-basics/#primary-keys) + autoIncrement: true, // auto increment (https://sequelize.org/docs/v6/core-concepts/model-basics/#auto-incrementing) }, - username: { - type: DataTypes.STRING, - allowNull: false, - unique: true, + username: { // username column (https://sequelize.org/docs/v6/core-concepts/model-basics/#column-options) + type: DataTypes.STRING, // data type (https://sequelize.org/master/identifiers.html) (https://sequelize.org/docs/v6/core-concepts/model-basics/#data-types) + allowNull: false, // allow null (https://sequelize.org/docs/v6/core-concepts/model-basics/#allowing-nulls) + unique: true, // unique (https://sequelize.org/docs/v6/core-concepts/model-basics/#uniques) }, - password: { - type: DataTypes.TEXT, - allowNull: false, + password: { // password column (https://sequelize.org/docs/v6/core-concepts/model-basics/#column-options) + type: DataTypes.TEXT, // data type (https://sequelize.org/master/identifiers.html) + allowNull: false, // allow null (https://sequelize.org/docs/v6/core-concepts/model-basics/#allowing-nulls) validate: { // password validation (https://sequelize.org/docs/v6/core-concepts/model-basics/#validations) - len: [8], // password must be at least 8 characters long + len: [8], // password must be at least 8 characters long (https://sequelize.org/docs/v6/core-concepts/model-basics/#validations) }, }, }, @@ -33,14 +33,14 @@ User.init( // User model (https://sequelize.org/docs/v6/core-concepts/model-bas hooks: { // hooks (https://sequelize.org/master/manual/hooks.html) beforeCreate: async (newUserData) => { // beforeCreate hook (https://sequelize.org/master/manual/hooks.html) newUserData.password = await bcrypt.hash(newUserData.password, 10); // hash password (https://www.npmjs.com/package/bcrypt) - return newUserData; + return newUserData; // return newUserData (https://sequelize.org/master/manual/hooks.html) }, }, - sequelize, - freezeTableName: true, - underscored: true, - modelName: 'user', + sequelize, // for connecting to db with sequelize (default is ./config/connection.js) (https://sequelize.org/docs/v6/core-concepts/connections-and-transactions/) + freezeTableName: true, // for connecting to db with sequelize (default is ./config/connection.js) (https://sequelize.org/docs/v6/core-concepts/model-basics/) + underscored: true, + modelName: 'user', } ); -module.exports = User; \ No newline at end of file +module.exports = User; // export User model (https://sequelize.org/docs/v6/core-concepts/model-basics/) (https://sequelize.org/docs/v6/core-concepts/model-basics/#exporting-models) \ No newline at end of file diff --git a/public/js/app.js b/public/js/app.js index dc6fbad..0f8a9c0 100755 --- a/public/js/app.js +++ b/public/js/app.js @@ -9,20 +9,20 @@ cancelButton.classList.add("cancel-btn"); // add the cancel button class cancelButton.classList.add("button"); // add the button class cancelButton.style.display = "none"; // hide the cancel button -// show and hide new post form and cancel button -newPostButton.addEventListener("click", function() { - newPostButton.style.display = "none"; - cancelButton.style.display = "block"; - newPostForm.style.display = "block"; +// show and hide new post form and cancel button when clicked on the new post button +newPostButton.addEventListener("click", function() { // add an event listener to the new post button when clicked on the new post button + newPostButton.style.display = "none"; // hide the new post button when clicked on the new post button + cancelButton.style.display = "block"; // show the cancel button when clicked on the cancel button + newPostForm.style.display = "block"; // show the new post form when clicked on the cancel button or the new post button }); -// show and hide new post form and cancel button -cancelButton.addEventListener("click", function() { - newPostButton.style.display = "block"; - cancelButton.style.display = "none"; - newPostForm.style.display = "none"; +// show and hide new post form and cancel button when clicked on the new post button or the cancel button +cancelButton.addEventListener("click", function() { // add an event listener to the cancel button when clicked on the cancel button + newPostButton.style.display = "block"; // show the new post button when clicked on the cancel button + cancelButton.style.display = "none"; // hide the cancel button when clicked on the cancel button + newPostForm.style.display = "none"; // hide the new post form when clicked on the cancel button or the new post button }); -// insert cancel button before new post button -newPostButton.parentNode.insertBefore(cancelButton, newPostButton.nextSibling); \ No newline at end of file +// insert cancel button before new post button if it exists (https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore) +newPostButton.parentNode.insertBefore(cancelButton, newPostButton.nextSibling); // insert the cancel button before the new post button (https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore) \ No newline at end of file diff --git a/seeds/seed.js b/seeds/seed.js index 62390e7..976ed7c 100755 --- a/seeds/seed.js +++ b/seeds/seed.js @@ -5,26 +5,26 @@ const userData = require('./userData.json'); // Import the userData.json file const postData = require('./postData.json'); // Import the postData.json file const commentData = require('./commentData.json'); // Import the commentData.json file -// seed database function -const seedDatabase = async () => { - await sequelize.sync({ force: true }); +// seed database function (https://sequelize.org/docs/v6/core-concepts/assocs/#one-to-many) that synchronizes the database with the models and seeds the database with the data from the JSON files +const seedDatabase = async () => { // seedDatabase function (https://sequelize.org/docs/v6/core-concepts/assocs/#one-to-many) + await sequelize.sync({ force: true }); // sync method (https://sequelize.org/docs/v6/core-concepts/model-basics/#sync - await User.bulkCreate(userData, { - individualHooks: true, - returning: true, + await User.bulkCreate(userData, { // bulkCreate method (https://sequelize.org/docs/v6/core-concepts/model-basics/#bulk-create) + individualHooks: true, // individualHooks method (https://sequelize.org/docs/v6/core-concepts/model-basics/#individual-hooks) + returning: true, // returning method (https://sequelize.org/docs/v6/core-concepts/model-basics/#returning) }); - await Post.bulkCreate(postData, { - individualHooks: true, - returning: true, + await Post.bulkCreate(postData, { // bulkCreate method (https://sequelize.org/docs/v6/core-concepts/model-basics/#bulk-create) + individualHooks: true, // individualHooks method (https://sequelize.org/docs/v6/core-concepts/model-basics/#individual-hooks) + returning: true, // returning method (https://sequelize.org/docs/v6/core-concepts/model-basics/#returning) }); - await Comment.bulkCreate(commentData, { - individualHooks: true, - returning: true, + await Comment.bulkCreate(commentData, { // bulkCreate method (https://sequelize.org/docs/v6/core-concepts/model-basics/#bulk-create) + individualHooks: true, // individualHooks method (https://sequelize.org/docs/v6/core-concepts/model-basics/#individual-hooks) + returning: true, // returning method (https://sequelize.org/docs/v6/core-concepts/model-basics/#returning) }); - process.exit(0); + process.exit(0); // exit process (https://nodejs.org/api/process.html#processexitcodecode) }; -seedDatabase(); \ No newline at end of file +seedDatabase(); // seedDatabase function (https://sequelize.org/docs/v6/core-concepts/assocs/#one-to-many) \ No newline at end of file