Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EdTriplett's Solution' #13

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
14 changes: 14 additions & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require("path");

const config = {
config: "./config/sequelize.js",
"migrations-path": "./migrations/sequelize",
"seeders-path": "./seeds/sequelize",
"models-path": "./models/sequelize"
};

Object.keys(config).forEach(key => {
config[key] = path.resolve(config[key]);
});

module.exports = config;
27 changes: 27 additions & 0 deletions MongoSchema.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Order: (collection)

Product: [ productId, productId]

Customer:
fname:
lname:
email
street:
city:
state:
zip:

Metadata:
dateTime:
stripeToken:
cardType:
total:


Product: (collection)
Name:
Price:
SKU:
Description:
Categories:
Quantity:
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# project_mimirs_market
A Viking eCommerce store for Thunder Gods that like to buy "Antique Wooden Pizzas"

Ed and Will
20 changes: 20 additions & 0 deletions RelationalSchema.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Product:
sku: string
name: string
description: string
price: integer
CategoryId: Integer

sequelize model:create --name Product --attributes "sku:string name:string description:string price:integer CategoryId:integer"


Category:
name: string

sequelize model:create --name Category --attributes "name:string"


State:
name: string

sequelize model:create --name State --attributes "name:string"
26 changes: 26 additions & 0 deletions Views.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Product:
index
single
(edit)

Category:
index
single
(edit)

User:
single

Address:
index
edit

Cart:
index
checkout

Admin:
User index
Orders index
Orders single
Analytics
68 changes: 68 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const express = require("express");
const app = express();

// Templates
const expressHandlebars = require("express-handlebars");
const hbs = expressHandlebars.create({
partialsDir: "views/",
defaultLayout: "application",
helpers: require("./helpers")
});
app.engine("handlebars", hbs.engine);
app.set("view engine", "handlebars");

// Post Data
const bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));

// Session
const cookieSession = require("cookie-session");
app.use(
cookieSession({
name: "session",
keys: ["as908ffa9d08sadf89sadf89qjqwjkl"]
})
);

// Flash
const flash = require("express-flash-messages");
app.use(flash());

// Log Request Info
const morgan = require("morgan");
const morganToolkit = require("morgan-toolkit")(morgan);
app.use(morganToolkit());

// Method Overriding
const methodOverride = require("method-override");
const getPostSupport = require("express-method-override-get-post-support");
app.use(methodOverride(getPostSupport.callback, getPostSupport.options));

// Connect to Mongoose
const mongoose = require("mongoose");
app.use((req, res, next) => {
if (mongoose.connection.readyState) next();
else require("./mongo")().then(() => next());
});

// Routes
app.all("/", (req, res) => {return res.redirect("/products")});

app.use("/products", require("./routers/products"));

// app.use("/users", require("./routers/users"));

// app.use("/comments", require("./routers/comments"));

// Set up port/host
const port = process.env.PORT || process.argv[2] || 3000;
const host = "localhost";
let args = process.env.NODE_ENV === "production" ? [port] : [port, host];

// helpful log when the server starts
args.push(() => {
console.log(`Listening: http://${host}:${port}`);
});

// Use apply to pass the args to listen
app.listen.apply(app, args);
6 changes: 6 additions & 0 deletions config/mongoUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const env = process.env.NODE_ENV || "development";
const config = require("./mongoose.json")[env];
module.exports =
process.env.NODE_ENV === "production"
? process.env[config.use_env_variable]
: `mongodb://${config.host}/${config.database}`;
13 changes: 13 additions & 0 deletions config/mongoose.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"development": {
"database": "project_mimirs_market_development",
"host": "localhost"
},
"test": {
"database": "project_mimirs_market_test",
"host": "localhost"
},
"production": {
"use_env_variable": "MONGO_URL"
}
}
21 changes: 21 additions & 0 deletions config/sequelize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require("dotenv").config();
module.exports = {
development: {
username: process.env.USERNAME,
password: null,
database: "project_mimirs_market_development",
host: "127.0.0.1",
dialect: "postgres"
},
test: {
username: process.env.USERNAME,
password: null,
database: "project_mimirs_market_test",
host: "127.0.0.1",
dialect: "postgres"
},
production: {
use_env_variable: "POSTGRES_URL",
dialect: "postgres"
}
};
9 changes: 9 additions & 0 deletions helpers/CategoryHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let CategoryHelper = {};

CategoryHelper.categoriesPath = () => "/categories/";
CategoryHelper.categoryPath = id => `/categories/${id}`;
// CategoryHelper.addProductPath = () => "/products/add";
// CategoryHelper.editProductPath = (id) => `/products/${ id }/edit`;
// CategoryHelper.destroyProductPath = (id) => `/products/${ id }/?_method=delete`;

module.exports = CategoryHelper;
11 changes: 11 additions & 0 deletions helpers/FlashHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
bootstrapAlertClassFor: key => {
return (
{
error: "danger",
alert: "danger",
notice: "info"
}[key] || key
);
}
};
9 changes: 9 additions & 0 deletions helpers/ProductHelper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
let ProductHelper = {};

ProductHelper.productsPath = () => "/products/";
ProductHelper.productPath = id => `/products/${id}`;
ProductHelper.addProductPath = id => `/products/add/${id}`;
// ProductHelper.editProductPath = (id) => `/products/${ id }/edit`;
// ProductHelper.destroyProductPath = (id) => `/products/${ id }/?_method=delete`;

module.exports = ProductHelper;
62 changes: 62 additions & 0 deletions helpers/filterHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
function filterHandler(query) {
let searchObj;
let categoryObj;
// let sortObj;

if (query.search) {
searchObj = { name: { $iLike: `%${query.search}%` } };
}

let min = query.min || 0;
let max = query.max || 999;
let rangeObj = {price: {$gte: min, $lte: max}};

if (query.category) {
let categoriesArray =
query.category === "Any"
? [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
: [query.category];
categoryObj = {CategoryId: {$in: categoriesArray}};
}

// if (query.sort) {
// sortObj = query.sort;
// }

let queryObj = {
include: [{ all: true }],
order: _sortHandler(query.sort),
where: (searchObj && categoryObj) ? {$and: [searchObj, rangeObj, categoryObj]} : {$and: [rangeObj, categoryObj]}
};

return queryObj;
}

function _sortHandler (sort){
let sortValue;
switch (sort){
case "nameAsc":
sortValue=["name"];
break;
case "nameDesc":
sortValue=[["name", "DESC"]];
break;
case "priceAsc":
sortValue=["price"];
break;
case "priceDesc":
sortValue=[["price", "DESC"]];
break;
case "createdAsc":
sortValue=["createdAt"];
break;
case "createdDesc":
sortValue=[['createdAt', 'DESC']];
break;
default:
sortValue=""
}
return sortValue
}

module.exports = filterHandler;
5 changes: 5 additions & 0 deletions helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const LoadHelpers = require("load-helpers");
const helperLoader = new LoadHelpers();
const helpers = helperLoader.load("helpers/*Helper.js").cache;

module.exports = helpers;
50 changes: 50 additions & 0 deletions migrations/sequelize/20170809195343-create-product.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use strict";
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface
.createTable("Products", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
sku: {
allowNull: false,
type: Sequelize.STRING
},
name: {
allowNull: false,
type: Sequelize.STRING
},
description: {
type: Sequelize.STRING
},
price: {
allowNull: false,
type: Sequelize.INTEGER
},
CategoryId: {
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn("NOW")
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn("NOW")
}
})
.then(() => {
return queryInterface.addIndex("Products", ["name", "sku"], {
unique: true
});
});
},
down: function(queryInterface, Sequelize) {
return queryInterface.dropTable("Products");
}
};
36 changes: 36 additions & 0 deletions migrations/sequelize/20170809195350-create-category.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use strict";
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface
.createTable("Categories", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
allowNull: false,
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn("NOW")
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.fn("NOW")
}
})
.then(() => {
return queryInterface.addIndex("Categories", ["name"], {
unique: true
});
});
},
down: function(queryInterface, Sequelize) {
return queryInterface.dropTable("Categories");
}
};
Loading