Skip to content

Instantly share code, notes, and snippets.

@aaronroberson
Last active August 29, 2015 14:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save aaronroberson/74e27e57013e467c0fde to your computer and use it in GitHub Desktop.
Geekwise2 Day 5
(function(angular) {
"use strict";
var app = angular.module('Swagwise', ['ngResource', 'ui.router', 'ui.bootstrap']);
})(window.angular);
/* ============== MODELS ========================== */
fs.readdirSync(__dirname + '/models').forEach(function(filename) {
if(~filename.indexOf('.js')) require(__dirname + '/models/' + filename)
});
/*
* 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.
});
(function(angular) {
"use strict";
var app = angular.module('Swagwise');
app.factory('SwagService', function($resource) {
return $resource('/api/swag/:id');
});
})(window.angular);
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
});
};
(function(angular) {
"use strict";
var app = angular.module('Swagwise');
app.factory('SwagService', function($http) {
return {
swag: function() {
return $http.get('/api/swag');
}
};
});
})(window.angular);
// 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