Geekwise2 Day 5
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
(function(angular) { | |
"use strict"; | |
var app = angular.module('Swagwise', ['ngResource', 'ui.router', 'ui.bootstrap']); | |
})(window.angular); |
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
/* ============== MODELS ========================== */ | |
fs.readdirSync(__dirname + '/models').forEach(function(filename) { | |
if(~filename.indexOf('.js')) require(__dirname + '/models/' + filename) | |
}); |
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
/* | |
* Mongoose by default sets the auto_reconnect option to true. | |
* We recommend setting socket options at both the server and replica set level. | |
* We recommend a 30 second connection timeout because it allows for | |
* plenty of time in most operating environments. | |
*/ | |
var options = { | |
server: { | |
socketOptions: { | |
keepAlive: 1, | |
connectTimeoutMS: 30000 | |
} | |
}, | |
replset: { | |
socketOptions: { | |
keepAlive: 1, | |
connectTimeoutMS : 30000 | |
} | |
} | |
}; | |
/* | |
* Mongoose uses a different connection string format than MongoDB's standard. | |
* Use the mongodb-uri library to help you convert from the standard format to | |
* Mongoose's format. | |
*/ | |
var mongodbUri = 'mongodb://[user]:[db]@[host]:[protocol]/[collection]'; | |
var mongooseUri = uriUtil.formatMongoose(mongodbUri); | |
var conn = mongoose.connection; | |
mongoose.connect(mongooseUri, options); | |
conn.on('error', console.error.bind(console, 'connection error:')); | |
conn.once('open', function() { | |
// Wait for the database connection to establish, then start the app. | |
}); |
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
(function(angular) { | |
"use strict"; | |
var app = angular.module('Swagwise'); | |
app.factory('SwagService', function($resource) { | |
return $resource('/api/swag/:id'); | |
}); | |
})(window.angular); |
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
module.exports = function(app) { | |
// Require mongoose dependency | |
var mongoose = require('mongoose'); | |
/* ======================= REST ROUTES ====================== */ | |
// Handle API calls | |
// Swag API route | |
app.route('/api/swag') | |
.get(function(req, res) { | |
// use mongoose to get all products in the database | |
mongoose.model('Swag').find(req.query, function(err, swag) { | |
// if there is an error retrieving, send the error. nothing after res.send(err) will execute | |
if (err) | |
res.send(err); | |
res.send(swag); // return products in JSON format | |
}); | |
}); | |
app.route('/api/swag/:id') | |
.get(function(req, res) { | |
// use mongoose to get a product in the database by id | |
mongoose.model('Swag').findOne({id: req.params.id}, function(err, product) { | |
// if there is an error retrieving, send the error. nothing after res.send(err) will execute | |
if (err) | |
res.send(err); | |
res.send(product); // return the product in JSON format | |
}); | |
}); | |
/* ========================= FRONT-END ROUTES ======================= */ | |
// Route to handle all angular requests | |
app.get('*', function(req, res) { | |
res.sendfile('./app/index.html'); // load our public/index.html file | |
}); | |
}; |
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
(function(angular) { | |
"use strict"; | |
var app = angular.module('Swagwise'); | |
app.factory('SwagService', function($http) { | |
return { | |
swag: function() { | |
return $http.get('/api/swag'); | |
} | |
}; | |
}); | |
})(window.angular); |
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
// Require mongoose dependency | |
var mongoose = require('mongoose'); | |
// Create a swag schema | |
var swagSchema = mongoose.Schema({ | |
id: Number, | |
isFeatured: Boolean, | |
isActive: Boolean, | |
price: Number, | |
specialPrice: Number, | |
inventory: Number, | |
title: String, | |
manufacturer: String, | |
description: String, | |
dateCreated: { type: Date, default: Date.now }, | |
dateUpdated: { type: Date, default: Date.now }, | |
images: Array, | |
colors: Array, | |
tags: Array | |
}); | |
// Register the Product model and schema with mongoose | |
mongoose.model('Swag', swagSchema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment