Skip to content

Commit

Permalink
Initial full crud implementation of CouchDB persistence for texts for…
Browse files Browse the repository at this point in the history
… issue #7 on the basis of what has turned out to be a cleaner text prototype based on the Post prototype Express blog example.
  • Loading branch information
victorkane committed Aug 1, 2011
1 parent 44c8dc4 commit 8b1401f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
20 changes: 8 additions & 12 deletions models/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
var cradle = require('cradle');
var db = new(cradle.Connection)().database('lit_texts');

var Text = exports = module.exports = function Text(title, body, user) {
var Text = exports = module.exports = function Text(title, body, author) {
this.title = title;
this.body = body;
this.author = user.registration_email;
this.author = author;
this.createdAt = new Date;
};

Expand All @@ -21,6 +21,12 @@ Text.prototype.save = function(fn){
});
};

Text.prototype.update = function(fn){
this.updatedAt = new Date;
this.save(fn);
};


Text.prototype.validate = function(fn){
if (!this.title) return fn(new Error('_title_ required'));
if (!this.body) return fn(new Error('_body_ required'));
Expand Down Expand Up @@ -61,16 +67,6 @@ exports.byAuthor = function(author, fn) {
*****/

/*****************
Text.prototype.update = function(data, fn){
this.updatedAt = new Date;
for (var key in data) {
if (undefined != data[key]) {
this[key] = data[key];
}
}
this.save(fn);
};
Text.prototype.destroy = function(fn){
exports.destroy(this.id, fn);
};
Expand Down
1 change: 0 additions & 1 deletion models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ User.prototype.login = function(login_name, login_password, callback) {

User.prototype.register = function(data, callback) {
data.created_at = new Date();
console.log(data);
this.db.save(data, function(error, result) {
if(error) {
callback(error)
Expand Down
21 changes: 14 additions & 7 deletions routes/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function restrict(req, res, next) {
}

function accessLogger(req, res, next) {
console.log('/restricted accessed by %s', req.session.user.name);
console.log('/restricted accessed by %s', req.session.user.registration_email);
next();
}

Expand All @@ -30,9 +30,13 @@ module.exports = function(app){
Text.get(id, function(err, text){
if (err) return next(err);
if (!text) return next(new Error('failed to load text ' + id));
//var doc = new Text(text.);
console.log(text);
req.text = text;
var doc = new Text(text.title, text.body, text.author);
//doc._id = text._id;
doc._id = id;
doc._rev = text._rev;
doc.createdAt = text.createdAt;
doc.updatedAt = text.updatedAt;
req.text = doc;
next();
});
});
Expand All @@ -51,7 +55,7 @@ module.exports = function(app){

app.post('/text', restrict, accessLogger, function(req, res){
var data = req.body.text
, text = new Text(data.title, data.body);
, text = new Text(data.title, data.body, req.session.user.registration_email);

text.validate(function(err){
if (err) {
Expand Down Expand Up @@ -87,13 +91,16 @@ module.exports = function(app){
*/

app.put('/text/:text', restrict, accessLogger, function(req, res, next){
var text = req.text;
var data = req.body.text;
text = req.text;
text.title = data.title;
text.body = data.body;
text.validate(function(err){
if (err) {
req.flash('error', err.message);
return res.redirect('back');
}
text.update(req.body.text, function(err){
text.update(function(err){
if (err) return next(err);
req.flash('info', 'Successfully updated text');
res.redirect('/textview');
Expand Down
4 changes: 2 additions & 2 deletions routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ function getCount (req, res, next) {
function getTexts (req, res, next) {
if (req.session.user && req.session.user.registration_email) {
Text.byAuthor(req.session.user.registration_email, function(err, texts){
console.log(texts);
req.texts = texts;
req.count = texts.length;
//req.count = texts.length;
req.count = (texts)?texts.length:0;
next();
});
}else{
Expand Down
2 changes: 1 addition & 1 deletion views/text/form.jade
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

!= messages()

form#text(action=base + '/text' + (text.title ? '/' + text.id : ''), method='post')
form#text(action=base + '/text' + (text.title ? '/' + text._id : ''), method='post')
- if (text.title)
input(type='hidden', name='_method', value='put')
p
Expand Down
4 changes: 2 additions & 2 deletions views/text/index.jade
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
.text
// title
a.view(href=base + '/text/' + text.id)
a.view(href=base + '/text/' + text._id)
h2
= text.title
a.edit(href=base + '/text/' + text.id + '/edit') Edit
a.edit(href=base + '/text/' + text._id + '/edit') Edit

// flash messages
!= messages()
Expand Down

0 comments on commit 8b1401f

Please sign in to comment.