Skip to content

Instantly share code, notes, and snippets.

@mikevalstar
Created April 16, 2012 19:29
Show Gist options
  • Select an option

  • Save mikevalstar/2400942 to your computer and use it in GitHub Desktop.

Select an option

Save mikevalstar/2400942 to your computer and use it in GitHub Desktop.
Coding with Node.js Part 4 - Section 3
// Includes
var crypto = require('crypto');
function hashString(value) {
hash = crypto.createHash('sha1');
hash.update(value);
return hash.digest('hex');
}
var AdminPages = module.exports = function AdminPages(){};
AdminPages.prototype = {
db: null
, initPages: function(app, db){
this.db = db;
var self = this;
/* ... */
app.get ('/Admin/NewPost', function(req, res) { self.pagePost(req, res); } );
app.post('/Admin/Post', function(req, res) { self.pagePostPost(req, res); } );
app.get ('/Admin/Post/:id', function(req, res) { self.pagePost(req, res); } );
/* ... */
}
/* ... */
, pagePost: function(req, res){ // Edit/New Page
if( !this._checkLogin(req, res) ) return;
if(req.params.id){ // Old Post
var blogpost = this.db.model('blogPost');
blogpost.findOne({sid: req.params.id}, function(err, row){
if(!row){
res.redirect('/Admin/NewPost');
return;
}
res.render('admin/post', {
title: req.params.id + ' - ' + row.title,
post: row,
showFullNav: false
});
});
}else{ // New Post
var newid = 1;
var blogpost = this.db.model('blogPost');
blogpost.find().sort("sid", -1).limit(1).exec(function(err, doc){
if(err)
console.info(err);
if(!doc || doc.length == 0){
newid = 1;
}else{
newid = doc[0].sid + 1;
}
res.render('admin/post', {
title: 'New Blog Post',
post: { id : "",
sid : newid,
author : "mikevalstar@gmail.com",
title : "",
img_lg : "",
img_sm : "",
content : "",
short : "",
ext_link: "",
posted : "",
edited : "",
},
showFullNav: false
});
});
}
}
, pagePostPost: function(req, res){
if( !this._checkLogin(req, res) ) return;
if(req.body.id && req.body.id != ""){
// old page
var blogpost = this.db.model('blogPost');
blogpost.update(
{_id: req.body.id},
{
sid : req.body.sid,
title : req.body.title,
img_lg : req.body.img_lg,
img_sm : req.body.img_sm,
content : req.body.content,
short : req.body.short,
ext_link: req.body.ext_link,
},
{ multi: false },
function(err, numrows){
if(err){
console.log(err);
}else{
console.log("Updated ("+numrows+") blog post(s) at internal id: " + req.body.id);
}
res.redirect('/Admin/Post/' + req.body.sid);
});
}else{
// new page
var blogpost = this.db.model('blogPost');
// max id + 1
var newid = 1;
blogpost.find().sort("sid", -1).limit(1).exec(function(err, doc){
if(err)
console.info(err);
if(!doc || doc.length == 0){
newid = 1;
}else{
newid = doc[0].sid + 1;
}
// new blog post
var post = new blogpost({
sid : req.body.sid == "" ? parseInt(newid) : req.body.sid,
author : "mikevalstar@gmail.com",
title : req.body.title,
img_lg : req.body.img_lg,
img_sm : req.body.img_sm,
content : req.body.content,
short : req.body.short,
ext_link: req.body.ext_link,
});
post.save(function(err){
if(err){
console.log(err);
}else{
console.log("Inserted new blog post at sid: " + post.sid + " at internal id: " + post.id);
}
res.redirect('/Admin/Post/' + post.sid);
});
});
}
}
};
h2 Edit Post
span.postid= post.id
form(method="post", action="/Admin/Post")
input(type="hidden", name="id", value=post.id)
h3 ID
input(type="text", name="sid", value=post.sid)
h3 Title
input(type="text", name="title", style="width: 100%;", value=post.title)
h4 Image Large
input(type="text", name="img_lg", style="width: 50%;", value=post.img_lg)
h4 Image Small
input(type="text", name="img_sm", style="width: 50%;", value=post.img_sm)
h3 Content
textarea(name="short", style="width: 100%; height: 5em;")= post.short
textarea(name="content", style="width: 100%; height: 20em;")= post.content
span.i All content is in jade format
h4 Link to external content
input(type="text", name="ext_link", style="width: 100%;", value=post.ext_link)
br
input(type="submit", value="Save")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment