-
-
Save AdrianRossouw/1280515 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 app = require('common').app, | |
common = require('common'), | |
db = require('common').db, | |
io = require('common').socket, | |
//form = require('common').form; | |
formidable = require('formidable'), | |
util = require('util'), | |
fs = require('fs'); | |
var DreamModel = require('./models/dreamModel').DreamModel; | |
var dreamModel = new DreamModel(); | |
// Routes | |
// Index page | |
app.get('/', function(req, res) { | |
res.render('index.jade', { | |
locals: { | |
title: 'Didit.co.za', | |
boilerplate: 'We all need magic elves to peice together our dreams and make them a reality. Be your own elf. Didit.co.za' | |
} | |
}); | |
}); | |
// | |
// User Routes | |
// | |
app.post('/register', function(req, res) { | |
var data = req.body; | |
// Check if username is in use | |
db.get(data.username, function(err, doc) { | |
if (doc) { | |
res.render('index', {flash: 'Username is already taken'}); | |
} else if (data.password != data.confirm_password) { | |
res.render('index', {flash: 'Passwords do not match'}); | |
} else { | |
//Create User in database | |
db.save(data.username, data, | |
function(db_err, db_res) { | |
res.render('index', {flash: 'User created'}); | |
}); | |
} | |
}); | |
}); | |
app.post('/login', function(req, res) { | |
var data = req.body; | |
db.get(data.username, function(err, doc) { | |
if (!doc) { | |
res.render('index', {flash: 'No user found'}); | |
} else if (doc.password != data.password) { | |
// Check if passwords match | |
res.render('index', {flash: 'Wrong password'}); | |
} else { | |
// Log user in | |
res.render('index', {flash: 'Logged in!'}); | |
} | |
}); | |
}); | |
// | |
// Dream Routes | |
// | |
// List dreams | |
app.get('/dreams.:format?', function(req, res) { | |
dreamModel.findAll(function(err, docs) { | |
switch (req.query.format) { | |
case 'json': | |
sys.puts('JSON Request to get dreams'); | |
res.send(docs.map(function(d) { | |
return d.title; | |
})); | |
break; | |
default : | |
res.render('dreams/index.jade', { | |
locals: { | |
title: 'Dream List', | |
docs: docs | |
} | |
}); | |
} | |
}); | |
}); | |
// List completed dreams | |
app.get('/dreams/completed.:format?', function(req, res) { | |
dreamModel.findDone(function(err, docs) { | |
switch (req.query.format) { | |
case 'json': | |
sys.puts('JSON Request to get dreams'); | |
res.send(docs.map(function(d) { | |
return d.title; | |
})); | |
break; | |
default : | |
res.render('dreams/index.jade', { | |
locals: { | |
title: 'Dream List', | |
docs: docs | |
} | |
}); | |
} | |
}); | |
}); | |
// Create a dream | |
app.post('/dreams/dreams', function(req, res) { | |
var document = new DreamModel(req.body['dream']); | |
document.save(req.body['dream'], function(err, docs) { | |
res.redirect('/dreams'); | |
}); | |
}); | |
app.get('/dreams/new', function(req, res) { | |
res.render('dreams/new.jade', { | |
locals: { | |
title: 'Add a Dream', | |
doc: new DreamModel() | |
} | |
}); | |
}); | |
// Update a dream | |
app.get('/dreams/:id.:format?/edit', function(req, res) { | |
dreamModel.findById(req.params.id, function(err, doc) { | |
res.render('dreams/edit.jade', { | |
locals: { title: 'Keep your dream up to date', | |
doc: doc } | |
}); | |
}); | |
}); | |
app.post('/dreams/:id.:format?/edit', function(req, res) { | |
req.on('data', function(chunk) { | |
console.log('Received body data:'); | |
}); | |
var form = new formidable.IncomingForm(); | |
form.uploadDir = __dirname + '/public/files'; | |
//form.on('end', function() { | |
// console.log('-> upload done'); | |
//}); | |
form.parse(req, function(err, fields, files) { | |
console.log('Fields: ' + fields); | |
/*fs.writeFile(files.upload.name, files.upload,'utf8', function (err) { | |
if (err) throw err; | |
console.log('It\'s saved!'); | |
});*/ | |
//res.redirect('back'); | |
dreamModel.findById(fields['dream[id]'], function(err, doc) { | |
// Do something with it | |
doc.id = fields['dream[id]']; | |
doc.title = fields['dream[title]']; | |
doc.description = fields['dream[description]']; | |
doc.cost = parseFloat(fields['dream[cost]']); | |
doc.context = fields['dream[context]']; | |
doc.targetDate = fields['dream[target]']; | |
doc.image = files['dream[image]']; | |
doc.imagedata = fs.createReadStream(files['dream[image]'].path); | |
doc.done = fields['dream[done]']; | |
console.log('Doc: ' + doc); | |
// Add Attachment | |
// Persist the changes | |
// req.on('end', function(err, fields, files) { | |
dreamModel.update(fields['dream[id]'], doc, function(err, docs) { | |
// Respond according to the request format | |
if (files['dream[image]']) { | |
doc.image = files['dream[image]']; | |
//imagedata = fs.createReadStream(files['dream[image]'].path); | |
console.log('Path: ' + doc.image.path); | |
db.saveAttachment( | |
doc._id, | |
doc._rev, | |
doc.image.name, | |
doc.image.mime, | |
fs.createReadStream(doc.image.path) | |
//fs.createReadStream(doc.image.path, function(err, data) { | |
// if (err) throw err; | |
//}) | |
); | |
} | |
/* | |
switch (req.query.format) { | |
case 'json': | |
//res.send(d.__doc); | |
break; | |
default: | |
default: | |
res.header('Content-Length', 123); | |
res.write('received upload:\n\n'); | |
res.redirect('/dreams'); | |
} */ | |
//}); | |
}); | |
}); | |
}); | |
}); | |
/* | |
form.on('progress', function(bytesReceived, bytesExpected) { | |
var percent = (bytesReceived / bytesExpected * 100) | 0; | |
process.stdout.write('Uploading: %' + percent + '\r'); | |
}); | |
/* | |
// Load the document | |
dreamModel.findById(fields['dream[id]'], function(err, doc) { | |
// Do something with it | |
doc.title = req.body.dream.title; | |
doc.description = req.body.dream.description; | |
doc.cost = parseFloat(req.body.dream.cost); | |
doc.context = req.body.dream.context; | |
doc.targetDate = req.body.dream.target; | |
doc.image = req.body.dream.image; | |
doc.done = req.body.dream.done; | |
// Add Attachment | |
// Persist the changes | |
dreamModel.update(form.fields.dream[id], req.body['dream'], function(err, docs) { | |
// Respond according to the request format | |
switch (req.query.format) { | |
case 'json': | |
//res.send(d.__doc); | |
break; | |
default: | |
res.redirect('/dreams'); | |
} | |
}); | |
}); | |
}); | |
*/ | |
/* | |
app.post('/dreams/:id', function(req, res) { | |
console.log(req.body); | |
req.form.complete(function(err, fields, files) { | |
if(err) { | |
throw(err); | |
} else { | |
console.log(files); | |
res.redirect('back'); | |
} | |
}); | |
req.form.on('progress', function(bytesReceived, bytesExpected) { | |
var percent = (bytesReceived / bytesExpected * 100) | 0; | |
process.stdout.write('Uploading: %' + percent + '\r'); | |
}); | |
}); | |
*/ | |
// Delete | |
app.del('/dreams/:id?', function(req, res) { | |
// Load the document | |
dreamModel.findById(req.params.id, function(err, doc) { | |
// Do something with it | |
// Persist the changes | |
dreamModel.remove(doc._id, doc._rev, function(error, result) { | |
res.redirect('/dreams'); | |
}); | |
}); | |
}); | |
// | |
// Error handle on unfound pages | |
// | |
/* | |
app.get('*', function(req, res) { | |
res.send("Your dreams are lost", 404); | |
}); | |
*/ | |
module.exports = {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment