Browse files

Added post and story models. Added basic functionality to Posts. Adde…

…d ignore file
  • Loading branch information...
1 parent 238c07c commit a92848295a5a28ef5789ec9c428442b02b32906b Adrian Kokobobo committed Jun 25, 2011
Showing with 156 additions and 5 deletions.
  1. +1 −0 .gitignore
  2. +24 −5 app.js
  3. +43 −0 db.js
  4. +76 −0 models/post.js
  5. +12 −0 models/story.js
View
1 .gitignore
@@ -0,0 +1 @@
+*.komodoproject
View
29 app.js
@@ -1,12 +1,13 @@
-
/**
* Module dependencies.
*/
var express = require('express');
+
var redisModule = require('redis');
var redis = redisModule.createClient();
redis.select(1);
+this.redis = redis;
var app = module.exports = express.createServer();
@@ -33,10 +34,28 @@ app.configure('production', function(){
// Routes
app.get('/', function(req, res){
- res.render('index', {
- locals: {
- title: 'Express'
- }
+ redis.get('index:some', function(e, r) {
+ console.log("r: ", r);
+ if(r !== null) {
+ var id = parseInt(r.toString());
+ id++;
+ redis.set("index:some", id, function() {
+ console.log("id:", id);
+ res.render('index');
+ });
+ } else {
+ redis.set("index:some", 0, function(e, r) {
+ console.log("id:", id, r);
+ res.render('index');
+ });
+ }
+ });
+
+ redis.get('index:some', function(e, r) {
+
+ console.log(r);
+
+
});
});
View
43 db.js
@@ -0,0 +1,43 @@
+var app = require('../app');
+var redis = app.redis;
+
+this.save = function(model, cb) {
+ if(model.id() == null) {
+ getIndex(model.name(), function(index) {
+ model.setId(index);
+ save(model, cb);
+ });
+ } else {
+ save(model, cb);
+ }
+}
+
+this.find = function() { }
+
+function save(model, cb) {
+ redis.HMSET(
+ model.name() + ':' + model.id(), //Key
+ model.attr(), // Value
+ cb //callback
+ );
+}
+
+function getIndex(modelName, cb) {
+ getOrCreateIndex(modelName, function(index) {
+ updateIndexCount(modelName, index, cb);
+ });
+}
+
+function getOrCreateIndex(modelName, cb) {
+ redis.get('index:' + modelName, function(e, r) {
+ if(r !== null) {
+ cb(parseInt(r.toString()) + 1);
+ } else {
+ cb(1);
+ }
+ });
+}
+
+function updateIndexCount(modelName, index, cb) {
+ redis.set("index:" + modelName, index, function() { cb(index); });
+}
View
76 models/post.js
@@ -0,0 +1,76 @@
+var db = require('../db');
+var MODEL_NAME = 'Post';
+
+/**
+ * Creates and Saves a post.
+ * @param {String} message
+ * @param {Number} userId
+ * @param {Number} storyId
+ * @param {Function} cb the new post is passed as argument to callback
+ */
+this.create = function(message, userId, storyId, cb) {
+ var post = new Post({message: message, userId: userId, storyId: storyId});
+ post.save(function() {
+ cb(post);
+ });
+};
+
+function Post(attributes) {
+ var _id = attributes['id'] || null;
+ var _message = attributes['message'] || null;
+ var _userId = attributes['userId'] || null;
+ var _storyId = attributes['storyId'] || null;
+ var _voteCount = 0;
+
+ function id() { return id; }
+ function message() { return _message; }
+ function userId() { return _userId; }
+ function storyId() { return _storyId; }
+ function voteCount() { return _voteCount; }
+ function name() { return MODEL_NAME; }
+
+ var _submitedVotes = {};
+ function vote(uId) {
+ if(!hasVoted(uid)) {
+ _submitedVotes[uid] = true;
+ _voteCount++;
+ }
+ }
+
+ //returns true is user id is not in the list of people who have already voted.
+ //and user id is not the creator of the post
+ function hasVoted(uid) {
+ return (_submitedVotes[uid] === undefined && _submitedVotes[uid] !== userId());
+ }
+
+
+ function save(cb) {
+ db.save(this, cb);
+ }
+
+ function attrs() {
+ return {
+ id: id(),
+ userId: userId(),
+ message: message(),
+ storyId: storyId(),
+ voteCount: voteCount()
+ }
+ }
+
+ //Public Interface
+ return {
+ id: id,
+ userId: userId,
+ message: message,
+ storyId: storyId,
+ name: name,
+ vote: vote,
+ save: save,
+ attrs: attrs,
+ //to be used only when saving model
+ setID: function(__id) { _id = __id; }
+ }
+
+}
+
View
12 models/story.js
@@ -0,0 +1,12 @@
+var app = require('../app');
+var redis = app.redis;
+
+var MIN_PLAYERS = 6;
+var MAX_MAX_PLAYERS = 20;
+var defaultFirstPost = 'It was a dark and stormy night.';
+var POST_CHAR_LIMIT = 500;
+this.create = function(maxPlayers, firstPost, name) {
+ if(maxPlayers < MIN_PLAYERS || maxPlayers > MAX_MAX_PLAYERS) maxPlayers = MAX_MAX_PLAYERS;
+ if(!firstPost || firstPost.length > POST_CHAR_LIMIT) firstPost = defaultFirstPost;
+ if(!name) name = firstPost;
+}

0 comments on commit a928482

Please sign in to comment.