forked from mschwarzmueller/nodejs-shopping-cart-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mschwarzmueller#3 from ishendyweb/02-mongoose-seed…
…-get Added mongoose, seed data and fetch + display data
- Loading branch information
Showing
6 changed files
with
92 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
var mongoose = require('mongoose'); | ||
var Schema = mongoose.Schema; | ||
|
||
var schema = new Schema({ | ||
imagePath: {type: String, required: true}, | ||
title: {type: String, required: true}, | ||
description: {type: String, required: true}, | ||
price: {type: Number, required: true} | ||
}); | ||
|
||
module.exports = mongoose.model('Product', schema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,17 @@ | ||
var express = require('express'); | ||
var router = express.Router(); | ||
var Product = require('../models/product'); | ||
|
||
/* GET home page. */ | ||
router.get('/', function(req, res, next) { | ||
res.render('shop/index', { title: 'Shopping Cart' }); | ||
Product.find(function(err, docs) { | ||
var productChunks = []; | ||
var chunkSize = 3; | ||
for (var i = 0; i < docs.length; i += chunkSize) { | ||
productChunks.push(docs.slice(i, i + chunkSize)); | ||
} | ||
res.render('shop/index', { title: 'Shopping Cart', products: productChunks }); | ||
}); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
var Product = require('../models/product'); | ||
|
||
var mongoose = require('mongoose'); | ||
|
||
mongoose.connect('localhost:27017/shopping'); | ||
|
||
var products = [ | ||
new Product({ | ||
imagePath: 'https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png', | ||
title: 'Gothic Video Game', | ||
description: 'Awesome Game!!!!', | ||
price: 10 | ||
}), | ||
new Product({ | ||
imagePath: 'http://eu.blizzard.com/static/_images/games/wow/wallpapers/wall2/wall2-1440x900.jpg', | ||
title: 'World of Warcraft Video Game', | ||
description: 'Also awesome? But of course it was better in vanilla ...', | ||
price: 20 | ||
}), | ||
new Product({ | ||
imagePath: 'https://support.activision.com/servlet/servlet.FileDownload?file=00PU000000Rq6tz', | ||
title: 'Call of Duty Video Game', | ||
description: 'Meh ... nah, it\'s okay I guess', | ||
price: 40 | ||
}), | ||
new Product({ | ||
imagePath: 'https://pmcdeadline2.files.wordpress.com/2014/02/minecraft__140227211000.jpg', | ||
title: 'Minecraft Video Game', | ||
description: 'Now that is super awesome!', | ||
price: 15 | ||
}), | ||
new Product({ | ||
imagePath: 'https://d1r7xvmnymv7kg.cloudfront.net/sites_products/darksouls3/assets/img/DARKSOUL_facebook_mini.jpg', | ||
title: 'Dark Souls 3 Video Game', | ||
description: 'I died!', | ||
price: 50 | ||
}) | ||
]; | ||
|
||
var done = 0; | ||
for (var i = 0; i < products.length; i++) { | ||
products[i].save(function(err, result) { | ||
done++; | ||
if (done === products.length) { | ||
exit(); | ||
} | ||
}); | ||
} | ||
|
||
function exit() { | ||
mongoose.disconnect(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,19 @@ | ||
<div class="row"> | ||
<div class="col-sm-6 col-md-4"> | ||
<div class="thumbnail"> | ||
<img src="https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png" alt="..." class="img-responsive"> | ||
<div class="caption"> | ||
<h3>Thumbnail label</h3> | ||
<p class="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A consequuntur debitis delectus deserunt dolore dolorem eum expedita, inventore nobis omnis perferendis possimus quas repellat soluta, sunt voluptate, voluptates! Assumenda, nulla?</p> | ||
<div class="clearfix"> | ||
<div class="price pull-left">$12</div> | ||
<a href="#" class="btn btn-success pull-right" role="button">Button</a> | ||
{{# each products }} | ||
<div class="row"> | ||
{{# each this }} | ||
<div class="col-sm-6 col-md-4"> | ||
<div class="thumbnail"> | ||
<img src="{{ this.imagePath }}" alt="..." class="img-responsive"> | ||
<div class="caption"> | ||
<h3>{{ this.title }}</h3> | ||
<p class="description">{{ this.description }}</p> | ||
<div class="clearfix"> | ||
<div class="price pull-left">${{ this.price }}</div> | ||
<a href="#" class="btn btn-success pull-right" role="button">Add to Shopping Cart</a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
{{/each}} | ||
</div> | ||
<div class="col-sm-6 col-md-4"> | ||
<div class="thumbnail"> | ||
<img src="https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png" alt="..." class="img-responsive"> | ||
<div class="caption"> | ||
<h3>Thumbnail label</h3> | ||
<p class="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A consequuntur debitis delectus deserunt dolore dolorem eum expedita, inventore nobis omnis perferendis possimus quas repellat soluta, sunt voluptate, voluptates! Assumenda, nulla?</p> | ||
<div class="clearfix"> | ||
<div class="price pull-left">$12</div> | ||
<a href="#" class="btn btn-success pull-right" role="button">Button</a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="col-sm-6 col-md-4"> | ||
<div class="thumbnail"> | ||
<img src="https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png" alt="..." class="img-responsive"> | ||
<div class="caption"> | ||
<h3>Thumbnail label</h3> | ||
<p class="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A consequuntur debitis delectus deserunt dolore dolorem eum expedita, inventore nobis omnis perferendis possimus quas repellat soluta, sunt voluptate, voluptates! Assumenda, nulla?</p> | ||
<div class="clearfix"> | ||
<div class="price pull-left">$12</div> | ||
<a href="#" class="btn btn-success pull-right" role="button">Button</a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="row"> | ||
<div class="col-sm-6 col-md-4"> | ||
<div class="thumbnail"> | ||
<img src="https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png" alt="..." class="img-responsive"> | ||
<div class="caption"> | ||
<h3>Thumbnail label</h3> | ||
<p class="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A consequuntur debitis delectus deserunt dolore dolorem eum expedita, inventore nobis omnis perferendis possimus quas repellat soluta, sunt voluptate, voluptates! Assumenda, nulla?</p> | ||
<div class="clearfix"> | ||
<div class="price pull-left">$12</div> | ||
<a href="#" class="btn btn-success pull-right" role="button">Button</a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="col-sm-6 col-md-4"> | ||
<div class="thumbnail"> | ||
<img src="https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png" alt="..." class="img-responsive"> | ||
<div class="caption"> | ||
<h3>Thumbnail label</h3> | ||
<p class="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A consequuntur debitis delectus deserunt dolore dolorem eum expedita, inventore nobis omnis perferendis possimus quas repellat soluta, sunt voluptate, voluptates! Assumenda, nulla?</p> | ||
<div class="clearfix"> | ||
<div class="price pull-left">$12</div> | ||
<a href="#" class="btn btn-success pull-right" role="button">Button</a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="col-sm-6 col-md-4"> | ||
<div class="thumbnail"> | ||
<img src="https://upload.wikimedia.org/wikipedia/en/5/5e/Gothiccover.png" alt="..." class="img-responsive"> | ||
<div class="caption"> | ||
<h3>Thumbnail label</h3> | ||
<p class="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit. A consequuntur debitis delectus deserunt dolore dolorem eum expedita, inventore nobis omnis perferendis possimus quas repellat soluta, sunt voluptate, voluptates! Assumenda, nulla?</p> | ||
<div class="clearfix"> | ||
<div class="price pull-left">$12</div> | ||
<a href="#" class="btn btn-success pull-right" role="button">Button</a> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
{{/each}} |