Skip to content

Commit

Permalink
Added nblog
Browse files Browse the repository at this point in the history
  • Loading branch information
Alasdair Stalker committed Jan 19, 2013
1 parent b243bfc commit 20ac6d5
Show file tree
Hide file tree
Showing 17 changed files with 6,685 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -12,3 +12,4 @@ logs
results

npm-debug.log
node_modules/
50 changes: 47 additions & 3 deletions README.md
@@ -1,4 +1,48 @@
nblog
=====

A Simple CMS module for Node Js using Express, EJS and Mongo
# NBLOG

Simple Blog CMS using Mongoose, Express and EJS.

## Installation

$ npm install nblog

## Usage

Simply add the following to your main script

var nblog = require('nblog');
nblog.init('user', 'password');

Nblog requires Mongo DB so make sure to start it before running your App.

## Features

* Add, edit and delete articles
* Articles displayed in a blog layout with links to each article
* Urls built based on date and alias

## License

(The MIT License)

Copyright (c) 2013 Alasdair Stalker alasdair@asta.org.uk

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
134 changes: 134 additions & 0 deletions lib/mongoose.js
@@ -0,0 +1,134 @@
var util = require('util');
var moment = require('moment');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

exports.connect = function(dburl, callback) {
mongoose.connect(dburl);
}

exports.disconnect = function(callback) {
mongoose.disconnect(callback);
}

exports.setup = function(callback) { callback(null); }

var ArticleSchema = new Schema({
ts : { type: Date, default: Date.now },
title : String,
article : String,
description : String,
keywords : String,
alias : String
});

mongoose.model('Article', ArticleSchema);
var Article = mongoose.model('Article');

exports.emptyArticle = { "_id": "", title: "", article: "", alias: "", description: "", keywords: "" };

exports.add = function(title, ts, article, description, keywords, alias, callback) {
var newArticle = new Article();
newArticle.title = title;
if (ts !== '') {
var date_fields = ts.split("/");
newArticle.ts = new Date(date_fields[2], date_fields[0]-1, date_fields[1]);
} else {
newArticle.ts = new Date();
}
newArticle.article = article;
newArticle.description = description;
newArticle.keywords = keywords;
newArticle.alias = alias;
newArticle.save(function(err) {
if (err) {
util.log('FATAL '+ err);
callback(err);
} else
callback(null);
});
}

exports.delete = function(id, callback) {
exports.findArticleById(id, function(err, doc) {
if (err)
callback(err);
else {
util.log(util.inspect(doc));
doc.remove();
callback(null);
}
});
}

exports.edit = function(id, title, ts, article, description, keywords, alias, callback) {
exports.findArticleById(id, function(err, doc) {
if (err)
callback(err);
else {
if (ts !== undefined) {
var date_fields = ts.split("/");
doc.ts = new Date(date_fields[2], date_fields[0]-1, date_fields[1]);
} else {
doc.ts = new Date();
}
doc.title = title;
doc.article = article;
doc.description = description;
doc.keywords = keywords;
doc.alias = alias;
doc.save(function(err) {
if (err) {
util.log('FATAL '+ err);
callback(err);
} else
callback(null);
});
}
});
}

exports.allArticles = function(num, callback) {
Article.find({}).limit(num).sort("-ts").execFind(callback);
}

exports.forAll = function(doEach, done) {
Article.find({}, function(err, docs) {
if (err) {
util.log('FATAL '+ err);
done(err, null);
}
docs.forEach(function(doc) {
doEach(null, doc);
});
done(null);
});
}

var findArticleById = exports.findArticleById = function(id, callback) {
Article.findOne({ _id: id }, function(err, doc) {
if (err) {
util.log('FATAL '+ err);
callback(err, null);
}
callback(null, doc);
});
}
var findArticleByDate = exports.findArticleByDate = function(year, month, day, title, callback) {

var start = new Date(year, month-1, day); //months are 0 based so Jan equals 0
var end = new Date(year, month-1, day);
end.setMonth( end.getMonth( ) + 1 );
util.log(start);
util.log(end);
util.log(title);
Article.findOne({ts: {$gte: start, $lt: end}, alias: title}, function(err, doc) {
if (err) {
util.log('FATAL '+ err);
callback(err, null);
}
util.log(doc);
callback(null, doc);
});
}

0 comments on commit 20ac6d5

Please sign in to comment.