-
-
Save huttj/bb84295557f7796e1298 to your computer and use it in GitHub Desktop.
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
{ | |
"segredo": { | |
"key": "1234567890", | |
"secret": "asdfghjkl" | |
}, | |
"port": 3000, | |
"db": { | |
"path": "db.connection.string" | |
} | |
} |
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
var Promise = require('bluebird'); | |
var MongoClient = require('mongodb').MongoClient | |
module.exports = function activateMongo(config){ | |
return MongoClient.connectAsync(config.db.path).then(function(conn) { | |
var collections = ['users', 'quizzes', 'presentations']; | |
return collections.reduce(function(list, key) { | |
list[key] = Promise.promisifyAll(conn[key]); | |
return list; | |
}, {}) | |
}); | |
}; |
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
var config = require('./config.json'); | |
var db = require('./db')(config); | |
var models = require('./models')(db) | |
var app = require('./prezo')(config, models); |
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(db) { | |
function User(uname, password, firstName, lastName){ | |
if (notInstanceOf(this, User)){ | |
return new User(uname, password, firstName, lastName); | |
} | |
this.uname = uname; | |
this.password = password; | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
User.prototype.save = function saveUserAsync(){ | |
var self = this; | |
// Get users collection | |
return db.then(function(conn) { | |
// Need some logic to check if it exists and update | |
if (this.id) { | |
// update db entry | |
return conn.users.updateAsync({ id: self.id }, { | |
userName : this.uname, | |
password : this.password, | |
firstName : this.firstName, | |
lastName : this.lastName | |
}); | |
} else { | |
// Don't forget to return the promise | |
return conn.users.insertAsync({ | |
userName : this.uname, | |
password : this.password, // You'd better hash this!!! | |
firstName : this.firstName, | |
lastName : this.lastName | |
}).then(function(res) { | |
// It's like res.rows[0], or something | |
self.id = res.rows[0].id; | |
}); | |
} | |
}); | |
}; | |
function findUserById(id) { | |
// Get users collection | |
return db.then(function(conn) { | |
return conn.users.findByIdAsync(id); | |
}); | |
}; | |
function findUserByName(name) { | |
var query = { name: name }; | |
// Get users collection | |
return db.then(function(conn) { | |
return conn.users.findByIdAsync(query); | |
}); | |
}; | |
function assertType(obj, type) { | |
var msg; | |
if (obj === null) { | |
msg = 'null'; | |
} else if (obj === undefined) { | |
msg = 'undefined'; | |
} else if (notInstanceOf(obj, type)) { | |
msg = obj.constructor.name; | |
} | |
if (msg) { | |
throw new Error(msg + ' not instance of ' + type.name); | |
} | |
} | |
function notInstanceOf(obj, type) { | |
return !instanceOf(obj, type); | |
} | |
function instanceOf(obj, type) { | |
return (obj.constructor === type); | |
} | |
return { | |
Users: { | |
create: User, | |
find: { | |
byId: findUserById, | |
byName: findUserByName, | |
} | |
} | |
} | |
} |
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
var SlideShare = require('slideshare'), | |
express = require('express'), | |
bodyParser = require("body-parser"), | |
fs = require('fs'), | |
http = require('http'), | |
https = require('https'), | |
tag = process.argv[2] || 'angular'; | |
var routes = require('./routes/route'); | |
module.exports = function(config, models) { | |
var ss = new SlideShare(config.segredo.key, config.segredo.secret); | |
var app = express(); | |
var server = http.createServer(app); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: true})); | |
app.set('views', './views'); | |
app.set('view engine', 'jade'); | |
app.use(express.static(__dirname + '/client')); | |
app.use(function(req, res) { | |
req._models = models; | |
req._ss = ss; | |
}); | |
app.use('/', routes); | |
server.listen(config.port|| 3000, function(){ | |
var addr = server.address(); | |
console.log('Server listening on', addr.port); | |
}); | |
} |
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
var router = require('express').Router(); | |
var Promise = require('bluebird'); | |
var models = require('./models'); | |
router.route('/presentation') | |
.get(function getIndex(req, res){ | |
// What about errors? | |
req.ss.getSlideshowsByTag(tag, {limit: 10}, renderSlideShow); | |
function renderSlideShow(result){ | |
var embed = result.Tag.Slideshow[0].Embed; | |
res.render('index', {embed: embed}); | |
} | |
}); | |
router.route('/quiz') | |
.get(function getQuiz(req, res){ | |
res.render('quiz', {questions: questions}); | |
res.end(); | |
}); | |
router.route('/ng-quiz') | |
.get(function getNgQuiz(req, res){ | |
res.render('ng-quiz'); | |
res.end(); | |
}); | |
router.route('/signup') | |
.get(function(req, res) { | |
res.render('signup', {form:{}}); | |
res.end(); | |
}) | |
.post(function(req, res) { | |
// check input | |
// if good, save to db | |
var b = req.body; | |
try { | |
var user = models.User.create(b.user_name, b.pass_word, b.first_name, b.last_name); | |
user.save() | |
// Have to bind to console, or it throws an error | |
.then(console.log.bind(console)) | |
// redirect to success page | |
.then(function(){ | |
res.render('thank-you') | |
}) | |
// otherwise, send back to form with errors | |
.catch(function(e){ | |
var err = e; | |
res.render('signup', {form: req.body, message: err}); | |
}); | |
} catch (e) { | |
res.end('Missing required field ' + JSON.stringify(b)); | |
} | |
}); | |
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
doctype html | |
html | |
head | |
title Sign Up | |
link(rel="stylesheet", href="/css/main.css") | |
body | |
form(action="/signup", method="post") | |
label(for="firstName") First Name | |
input#firstName(name="first_name", required, value="#{form.first_name || ''}") | |
br | |
label(for="lastName") Last Name | |
input#lastName(name="last_name", required) | |
br | |
label(for="username") Username | |
input#username(name="user_name", required) | |
br | |
label(for="password") Password | |
input#password(type="password", name="pass_word", required) | |
.button | |
button(type="submit") Send away! | |
.error #{message} |
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
doctype html | |
html | |
head | |
title Thank You | |
link(rel="stylesheet", href="/css/main.css") | |
body Thanks for signing up. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment