Skip to content
Permalink
Browse files

Added basic interface

  • Loading branch information...
Alex Young
Alex Young committed Nov 22, 2010
1 parent 39e66cb commit de51d040ae7255c661bed4e3b36010cac2d879a1
71 app.js
@@ -26,22 +26,19 @@ app.configure('production', function() {
});

app.configure('test', function() {
app.use(express.logger());
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
db = mongoose.connect('mongodb://localhost/nodepad-test');
});

app.Document = Document = require('./models.js').Document(db);

app.get('/', function(req, res) {
res.render('index.jade', {
locals: {
title: 'Express'
}
});
res.redirect('/documents')
});

// Document list
app.get('/documents.:format', function(req, res) {
app.get('/documents.:format?', function(req, res) {
Document.find().all(function(documents) {
switch (req.params.format) {
case 'json':
@@ -51,18 +48,34 @@ app.get('/documents.:format', function(req, res) {
break;

default:
res.render('documents/index.jade');
res.render('documents/index.jade', {
locals: { documents: documents }
});
}
});
});

app.get('/documents/:id.:format?/edit', function(req, res) {
Document.findById(req.params.id, function(d) {
res.render('documents/edit.jade', {
locals: { d: d }
});
});
});

app.get('/documents/new', function(req, res) {
res.render('documents/new.jade', {
locals: { d: new Document() }
});
});

// Create document
app.post('/documents.:format?', function(req, res) {
var document = new Document(req.body['document']);
document.save(function() {
var d = new Document(req.body.document);
d.save(function() {
switch (req.params.format) {
case 'json':
res.send(document.__doc);
res.send(d.__doc);
break;

default:
@@ -73,14 +86,52 @@ app.post('/documents.:format?', function(req, res) {

// Read document
app.get('/documents/:id.:format?', function(req, res) {
Document.findById(req.params.id, function(d) {
switch (req.params.format) {
case 'json':
res.send(d.__doc);
break;

default:
res.render('documents/show.jade', {
locals: { d: d }
});
}
});
});

// Update document
app.put('/documents/:id.:format?', function(req, res) {
Document.findById(req.body.document.id, function(d) {
d.title = req.body.document.title;
d.data = req.body.document.data;
d.save(function() {
switch (req.params.format) {
case 'json':
res.send(d.__doc);
break;

default:
res.redirect('/documents');
}
});
});
});

// Delete document
app.del('/documents/:id.:format?', function(req, res) {
Document.findById(req.params.id, function(d) {
d.remove(function() {
switch (req.params.format) {
case 'json':
res.send('true');
break;

default:
res.redirect('/documents');
}
});
});
});

if (!module.parent) {
@@ -6,7 +6,13 @@ mongoose.model('Document', {
indexes: [
'title',
'user_id'
]
],

getters: {
id: function() {
return this._id.toHexString();
}
}
});

exports.Document = function(db) {
@@ -0,0 +1,21 @@
(function() {
$('.destroy').live('click', function(e) {
var element = $(this),
form = $('<form></form>');
e.preventDefault();
form
.attr({
method: 'POST',
action: element.attr('href')
})
.hide()
.append('<input type="hidden" />')
.find('input')
.attr({
'name': '_method',
'value': 'delete'
})
.end()
.submit();
});
})();
@@ -2,3 +2,35 @@ body {
padding: 50px;
font: 14px "Lucida Grande", "Helvetica Nueue", Arial, sans-serif;
}
h1,
h2,
h3,
h4,
h5,
h6,
p,
ul,
li {
margin: 0;
padding: 0 0 20px 0;
}
ul {
list-style-type: none;
}
ul li {
padding-bottom: 20px;
}
a.button {
margin: 0 10px 0 0;
padding: 2px 4px;
text-shadow: #faf9f9 0 1px 1px;
background-color: #ccc;
text-decoration: none;
color: #000;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
}
a.button:hover {
background-color: #efefef;
}
@@ -1,4 +1,30 @@
body {
padding: 50px;
font: 14px "Lucida Grande", "Helvetica Nueue", Arial, sans-serif;
}
}

h1, h2, h3, h4, h5, h6, p, ul, li { margin: 0; padding: 0 0 20px 0 }

ul {
list-style-type: none
}

ul li {
padding-bottom: 20px;
}

a.button {
margin: 0 10px 0 0;
padding: 2px 4px;
text-shadow: #faf9f9 0 1px 1px;
background-color: #ccc;
text-decoration: none;
color: #000;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
border-radius: 4px;
}

a.button:hover {
background-color: #efefef;
}
@@ -5,10 +5,18 @@
process.env.NODE_ENV = 'test';

var app = require('../app'),
lastID = '';
assert = require('assert');

function createDocument(title, after) {
var d = new app.Document({ title: title });
d.save(function() {
var lastID = d._id.toHexString();
after(lastID);
});
}

module.exports = {
'POST /documents.json': function(assert) {
'POST /documents.json': function(beforeExit) {
assert.response(app, {
url: '/documents.json',
method: 'POST',
@@ -22,11 +30,11 @@ module.exports = {
function(res) {
var document = JSON.parse(res.body);
assert.equal('Test', document.title);
lastID = document._id;
});
}
);
},

'HTML POST /documents': function(assert) {
'HTML POST /documents': function(beforeExit) {
assert.response(app, {
url: '/documents',
method: 'POST',
@@ -38,23 +46,26 @@ module.exports = {
});
},

'GET /documents.json': function(assert) {
'GET /documents/id.json': function(beforeExit) {
},

'GET /documents.json and delete them all': function(beforeExit) {
assert.response(app,
{ url: '/documents.json' },
{ status: 200, headers: { 'Content-Type': 'application/json' }},
function(res) {
var documents = JSON.parse(res.body);
assert.type(documents, 'object')
assert.type(documents, 'object');

documents.forEach(function(d) {
app.Document.findById(d._id, function(document) {
document.remove();
})
});
});
});
},

'GET /': function(assert) {
'GET /': function(beforeExit) {
assert.response(app,
{ url: '/' },
{ status: 200, headers: { 'Content-Type': 'text/html; charset=utf-8' }},
@@ -0,0 +1,6 @@
h2 Edit Document
form(method='post', action='/documents/' + d.id)
input(name='document[id]', value=d.id, type='hidden')
input(name='_method', value='PUT', type='hidden')
!=partial('documents/fields', { locals: { d: d } })

@@ -0,0 +1,13 @@
h1 Your Documents

p
a(class='button', href='/documents/new') + New Document

ul
- for (var d in documents)
li
a(class='button', href='/documents/' + documents[d].id + '/edit') Edit
a(class='button destroy', href='/documents/' + documents[d].id) Delete
a(href='/documents/' + documents[d].id)
=documents[d].title

@@ -0,0 +1,4 @@
h2 New Document
form(method='post', action='/documents')
!=partial('documents/fields', { locals: { d: d } })

@@ -0,0 +1,3 @@
h2=d.title
a(class='button', href='/documents/' + d.id + '/edit') Edit
p=d.data
@@ -1,6 +1,8 @@
!!!
html
head
title= title
title= 'Nodepad'
link(rel='stylesheet', href='/stylesheets/style.css')
body!= body
script(type='text/javascript', src='https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js')
body!= body
script(type='text/javascript', src='/javascripts/application.js')
@@ -0,0 +1,10 @@
div
label Title:
input(name='document[title]', value=d.title || '')
div
label Note:
textarea(name='document[data]')
=d.data || ''
div
input(type='submit', value='Save')

0 comments on commit de51d04

Please sign in to comment.
You can’t perform that action at this time.