Skip to content

Commit

Permalink
Add seeding of database, orders icon, and remove comments
Browse files Browse the repository at this point in the history
  • Loading branch information
allensarmiento committed Jul 23, 2019
1 parent 7d7a0e7 commit 407633e
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 6 deletions.
4 changes: 3 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ var express = require("express"),

User = require("./models/user"),
Shop = require("./models/shop"),
Stores = require("./models/stores");
Stores = require("./models/stores"),
seedDB = require("./seeds");

// ROUTES
var indexRoutes = require("./routes/index"),
Expand All @@ -30,6 +31,7 @@ app.set("view engine", "ejs"); // Expect ejs files
app.use(express.static(__dirname + "/public")); // Public folder for css styles
app.use(methodOverride("_method"));
app.use(flash());
seedDB(); // seed the database

// PASSPORT CONFIGURATION
app.use(require("express-session")({
Expand Down
1 change: 1 addition & 0 deletions public/images/shopping-bag-solid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions routes/auth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var express = require("express");
var router = express.Router();
var User = require("../models/user");
var Invoice = require("../models/invoice");

var passport = require("passport");

Expand All @@ -15,6 +16,28 @@ router.get("/account", function(req, res) {

// handling users
router.post("/account/signUp", function(req, res) {
// NOTE: Since this is a mockup application, to prevent the number of users being infinite, only 100 users can be registered
User.countDocuments({}, function(err, count) {
console.log("Number of users before: ", count);
if (count >= 100) {
Invoice.deleteMany({}, function(err, count) {
if (err) {
console.log(err);
} else {
console.log("Too many users, deleting all invoices before all users");
User.deleteMany({}, function(err, count) {
if (err) {
console.log(err);
} else {
console.log("Too many users, reset user database");
}
});
}
});
}
});

// Register new user
User.register(new User({username: req.body.username}), req.body.password, function(err, user) {
if (err) {
req.flash("error", err.message);
Expand Down
99 changes: 99 additions & 0 deletions seeds.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// Seeds the database.
//
// This seeds the entire database so that the website starts with no users
// and no invoices.

var mongoose = require("mongoose");
var Shop = require("./models/shop");
var Stores = require("./models/stores");
var Invoice = require("./models/invoice");
var User = require("./models/user");

var items = [
{blend: "Dark", name: "Dark Expresso", price: "8.99", image: "https://cdn.shopify.com/s/files/1/0002/1115/7052/products/brazil1_dkroast_400.jpg?v=1549596309"},
{blend: "Dark", name: "Haitian", price: "7.99", image: "https://www.holybeanscafe.com/holybeanscafe/wp-content/uploads/2015/11/dark-roast-coffee.jpg"},
{blend: "Dark", name: "Paris", price: "7.99", image: "https://images.unsplash.com/photo-1447933601403-0c6688de566e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1912&q=80"},
{blend: "Dark", name: "Mocha Java", price: "6.99", image: "https://sep.yimg.com/ay/yhst-133788258843600/mocha-java-organic-coffee-beans-41.gif"},

{blend: "Medium", name: "Medium Expresso", price: "7.99", image: "https://www.coffeebeandirect.com/media/catalog/product/cache/1/image/0dc6da5b2fe15547a809e30525508fd7/m/e/medium-roast-espresso-wide.jpg"},
{blend: "Medium", name: "Coffee Club", price: "8.99", image: "https://static1.squarespace.com/static/59d9113c90bade192af3e3a0/t/5a2dc13b53450ab787d59f99/1512948028366/food-beans-coffee-drink.jpg"},
{blend: "Medium", name: "Colombia", price: "7.99", image: "http://www.topratedcoffeebeans.com/wp-content/uploads/2014/11/Vienna-roast-beans-300x300.png"},

{blend: "Light", name: "House Blend", price: "8.99", image: "https://www.coffeebeandirect.com/media/catalog/product/cache/1/image/0dc6da5b2fe15547a809e30525508fd7/l/i/light-house-blend-wide.jpg"},
{blend: "Light", name: "Colombian Supremo", price: "8.99", image: "https://www.coffeebeandirect.com/media/catalog/product/cache/1/image/0dc6da5b2fe15547a809e30525508fd7/c/o/colombian-supremo-wide.jpg"},

{blend: "Decaf", name: "Brazilian", price: "7.99", image: "https://www.azorieblue.co.uk/wp-content/uploads/2017/04/whole-coffee-beans.png"},
{blend: "Decaf", name: "Costa Rica", price: "8.99", image: "https://www.friedrichscoffee.com/wp-content/uploads/2018/03/Decaf-Costa-Rica-Vienna.jpg"},
{blend: "Decaf", name: "Cinnamon Pecan", price: "6.99", image: "https://i0.wp.com/ottosgranary.com/wp-content/uploads/2019/04/Decaf-Cinnamon-Pecan-Praline-Coffee-Beans.jpg?fit=600%2C600&ssl=1"}
];

var stores = [
{city: "San Francisco", street: "Golden Gate", bgImage: "https://images.unsplash.com/photo-1487132906645-8e0fbba067e0?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80"},
{city: "San Francisco", street: "Mission District", bgImage: "https://images.unsplash.com/photo-1439396087961-98bc12c21176?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1780&q=80"},
{city: "San Francisco", street: "Midtown Terrace", bgImage: "https://images.unsplash.com/photo-1445359179985-460648949e10?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1334&q=80"},
{city: "San Francisco", street: "University Mound", bgImage: "https://images.unsplash.com/photo-1458169495136-854e4c39548a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60"},

{city: "Los Angeles", street: "Chinatown", bgImage: "https://images.unsplash.com/photo-1521123493239-e0f9ae0445ae?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80"},
{city: "Los Angeles", street: "Fashion District", bgImage: "https://images.unsplash.com/photo-1557882518-693670985a99?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1334&q=80"},
{city: "Los Angeles", street: "Rampart Village", bgImage: "https://images.unsplash.com/photo-1471039497385-b6d6ba609f9c?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80"},

{city: "New York", street: "East Village", bgImage: "https://images.unsplash.com/photo-1512486046-0d05000458ef?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1334&q=80"},
{city: "New York", street: "Brooklyn Heights", bgImage: "https://images.unsplash.com/photo-1484861671664-4ebd42ced711?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60"},
{city: "New York", street: "Greenpoint", bgImage: "https://images.unsplash.com/photo-1450705354661-932f7b21aa51?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1500&q=80"}
];

function seedDB() {
// Remove shop items
Shop.deleteMany({}, function(err) {
if (err) {
console.log(err);
}
console.log("removed shop items");
// Add shop items
items.forEach(function(item) {
Shop.create(item, function(err, shop) {
if (err) {
console.log(err);
} else {
console.log("Added a shop item");
}
});
});
});

// Remove stores
Stores.deleteMany({}, function(err) {
if (err) {
console.log(err);
}
console.log("removed stores");
// Add stores
stores.forEach(function(store) {
Stores.create(store, function(err, stores) {
if (err) {
console.log(err);
} else {
console.log("Added a store");
}
});
});
});

// Remove invoices
Invoice.deleteMany({}, function(err) {
if (err) {
console.log(err);
}
console.log("removed invoices");
});

// Remove users
User.deleteMany({}, function(err) {
if (err) {
console.log(err);
}
console.log("removed users");
})
}

module.exports = seedDB;
4 changes: 0 additions & 4 deletions views/account.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password" name="password" required>
</div>
<div class="form-group">
<label for="exampleInputPassword2">Verify Password</label>
<input type="password" class="form-control" id="exampleInputPassword2" placeholder=""> <!-- Need script to check that the passwords match -->
</div>
<button type="submit" class="btn btn-primary">Sign up</button>
</form>
</div>
Expand Down
2 changes: 1 addition & 1 deletion views/partials/header.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
<li id="ordersNav" class="nav-item">
<a class="nav-link border-right" href="/orders">
<span>
<img src="" style="width: 15px;">
<img src="./images/shopping-bag-solid.svg" style="width: 15px;">
Orders
</span>
</a>
Expand Down

0 comments on commit 407633e

Please sign in to comment.