diff --git a/README.md b/README.md index 622c8d9..465dea2 100644 --- a/README.md +++ b/README.md @@ -21,34 +21,36 @@ To use this project, you'll need to do the following: * run `npm i` from the terminal (this will install the npm modules: dotenv, bcrypt, connect-session-sequelize, express, express-handlebars, express-session, mysql2, passport, sequelize and validator) * create the mysql database using the `schema.sql` file - * run 'server.js' to dynamically create the required tables - * seed the newly created database tables with the `seeds.sql` file * create a `.env` file with your MySQL Database password in the following format - * (this was included in the `.gitignore` file to prevent the password from being exposed on Github): - -``` -PASSWORD="your_database_password_here" -``` -This file will be imported by `config/index.js` On a local setup. - -This works because `config/index.js` was modified in the following way: - -```js -let sequelize; -if (config.use_env_variable) { - sequelize = new Sequelize(process.env[config.use_env_variable], config); -} else { - // assign the local password from the `.env` to a new password variable. - const password = process.env.PASSWORD; - sequelize = new Sequelize(config.database, config.username,/*config.password is now*/ password, config); -} -``` + (this was included in the `.gitignore` file to prevent the password from being exposed on Github): + ``` + DB_PASSWORD="your_database_password_here" + ``` + +This file will be imported by `config/index.js` while running on your computer locally because of the following changes to the `config/index.js` file. + + ```js + let sequelize; + if (config.use_env_variable) { + sequelize = new Sequelize(process.env[config.use_env_variable], config); + } else { + // assign the local password from the `.env` to a new password variable. + const password = process.env.DB_PASSWORD; + sequelize = new Sequelize(config.database, config.username,/*config.password is now*/ password, config); + } + ``` + +* run 'server.js' to dynamically create the required tables +* seed the newly created database tables with the `seeds.sql` file + + + --- ## Our MySQL database layout for this project: -![MySQL Layout](/public/images/project2_database.png) +![MySQL Layout](./public/images/project2_database.png) --- diff --git a/controllers/apiRoutes.js b/controllers/apiRoutes.js index 06876d1..bf849e5 100644 --- a/controllers/apiRoutes.js +++ b/controllers/apiRoutes.js @@ -1,13 +1,13 @@ "use strict"; -var express = require("express"); -var router = express.Router(); -var passport = require("passport"); +const express = require("express"); +const router = express.Router(); +const passport = require("passport"); const bcrypt = require('bcrypt'); const saltRounds = 10; -var db = require("../models"); +const db = require("../models"); // delete an item from the cart router.delete("/api/cart", (req, res) => { @@ -69,8 +69,8 @@ router.post("/api/cart", (req, res) => { // route for processing a submitted order // set variables for submitting an order -var orderId = 0; -var userId = 0; +let orderId = 0; +let userId = 0; // first find the cart that has been submitted router.post("/api/cart/submitted", (req, res, next) => { userId = req.user; @@ -93,7 +93,7 @@ router.post("/api/cart/submitted", (req, res, next) => { productId: element.productId }); }); - var order = { + let order = { orderId: orderId } res.send(order); @@ -165,7 +165,7 @@ router.post("/api/account/register", (req, res) => { }); // login with an existing username and password -var pwd = ""; +let pwd = ""; router.post("/api/account/login", (req, res) => { pwd = req.body.password; db.users.findOne({ diff --git a/controllers/htmlRoutes.js b/controllers/htmlRoutes.js index ab1f240..de0ad51 100644 --- a/controllers/htmlRoutes.js +++ b/controllers/htmlRoutes.js @@ -1,11 +1,11 @@ "use strict"; -var express = require("express"); -var router = express.Router(); +const express = require("express"); +const router = express.Router(); const Sequelize = require('sequelize'); const op = Sequelize.Op; -var db = require("../models"); +const db = require("../models"); // find out if the user is looged in router.get("/user/status", (req, res) => { @@ -29,10 +29,10 @@ router.get("/cart/info", (req, res) => { attributes: ['id', 'num', 'each_price', 'productId'], where: { userId: req.user } }).then(function (cart) { - var cartInfo = {}; - var totalCost = 0; - var uniqueItems = 0; - var totalItems = 0; + let cartInfo = {}; + let totalCost = 0; + let uniqueItems = 0; + let totalItems = 0; cart.forEach(function (element) { totalCost += element.num * element.each_price; uniqueItems++; @@ -180,11 +180,11 @@ router.get("/cart", (req, res) => { { model: db.products, attributes: ['name', 'description'] } ] }).then(function (data) { - var totalCost = 0; - var totalItems = 0; - var cart = []; + let totalCost = 0; + let totalItems = 0; + let cart = []; for (let i = 0; i < data.length; i++) { - var tempObj = {}; + let tempObj = {}; totalItems += data[i].num; totalCost += data[i].num * data[i].each_price; tempObj.id = data[i].id; diff --git a/models/cart_item.js b/models/cart_item.js index 068b19c..afe2ef3 100644 --- a/models/cart_item.js +++ b/models/cart_item.js @@ -1,5 +1,5 @@ module.exports = function (sequelize, DataTypes) { - var CartItem = sequelize.define("cart_items", { + let CartItem = sequelize.define("cart_items", { num: { type: DataTypes.INTEGER, allowNull: false diff --git a/models/category.js b/models/category.js index 9960d70..31a054e 100644 --- a/models/category.js +++ b/models/category.js @@ -1,5 +1,5 @@ module.exports = function (sequelize, DataTypes) { - var Category = sequelize.define("categories", { + let Category = sequelize.define("categories", { name: { type: DataTypes.STRING, allowNull: false diff --git a/models/index.js b/models/index.js index 74e6b44..3b74b00 100644 --- a/models/index.js +++ b/models/index.js @@ -12,7 +12,7 @@ let sequelize; if (config.use_env_variable) { sequelize = new Sequelize(process.env[config.use_env_variable], config); } else { - const password = process.env.PASSWORD; + const password = process.env.DB_PASSWORD; sequelize = new Sequelize(config.database, config.username, password, config); } diff --git a/models/order_item.js b/models/order_item.js index 077a2ca..e3f6cf3 100644 --- a/models/order_item.js +++ b/models/order_item.js @@ -1,5 +1,5 @@ module.exports = function (sequelize, DataTypes) { - var OrderItem = sequelize.define("order_items", { + let OrderItem = sequelize.define("order_items", { num: { type: DataTypes.INTEGER, allowNull: false diff --git a/server.js b/server.js index ba5d23e..15c657a 100644 --- a/server.js +++ b/server.js @@ -1,19 +1,19 @@ "use strict"; require('dotenv').config() -var express = require("express"); -var app = express(); +const express = require("express"); +const app = express(); -var PORT = process.env.PORT || 3000; +const PORT = process.env.PORT || 3000; -var db = require("./models"); +const db = require("./models"); app.use(express.urlencoded({ extended: true })); app.use(express.json()); app.use(express.static("public")); -var session = require("express-session"); -var passport = require("passport"); -var SequelizeStore = require('connect-session-sequelize')(session.Store); +const session = require("express-session"); +const passport = require("passport"); +const SequelizeStore = require('connect-session-sequelize')(session.Store); app.use(session({ secret: 'asdwelhjt', @@ -29,15 +29,15 @@ app.use(session({ app.use(passport.initialize()); app.use(passport.session()); -var exphbs = require("express-handlebars"); +const exphbs = require("express-handlebars"); app.engine("handlebars", exphbs({ defaultLayout: "main" })); app.set("view engine", "handlebars"); -var apiRoutes = require("./controllers/apiRoutes.js"); +const apiRoutes = require("./controllers/apiRoutes.js"); app.use(apiRoutes); -var htmlRoutes = require("./controllers/htmlRoutes.js"); +const htmlRoutes = require("./controllers/htmlRoutes.js"); app.use(htmlRoutes); db.sequelize.sync({ force: false }).then(function () { diff --git a/views/layouts/main.handlebars b/views/layouts/main.handlebars index c0e202d..6fbbc2c 100644 --- a/views/layouts/main.handlebars +++ b/views/layouts/main.handlebars @@ -31,9 +31,9 @@