Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Commit

Permalink
major overhaul
Browse files Browse the repository at this point in the history
  • Loading branch information
=frank tree committed Dec 10, 2014
1 parent 24358dd commit 512e1a9
Show file tree
Hide file tree
Showing 191 changed files with 111,550 additions and 327 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
Expand Up @@ -9,7 +9,7 @@
"boss": true,
"eqnull": true,
"expr": true,
"globalstrict": true,
"globalstrict": false,
"laxbreak": true,
"loopfunc": true,
"sub": true,
Expand Down
11 changes: 6 additions & 5 deletions app/controllers/articles.js
Expand Up @@ -2,11 +2,12 @@
/**
* Module dependencies.
*/
"use strict";

var mongoose = require('mongoose')
var Article = mongoose.model('Article')
var utils = require('../../lib/utils')
var extend = require('util')._extend
var mongoose = require('mongoose');
var Article = mongoose.model('Article');
var utils = require('../../lib/utils');
var extend = require('util')._extend;

/**
* Load
Expand Down Expand Up @@ -39,7 +40,7 @@ exports.index = function (req, res){
if (err) return res.render('500');
Article.count().exec(function (err, count) {
res.render('articles/index', {
title: 'Articles',
title: 'News',
articles: articles,
page: page + 1,
pages: Math.ceil(count / perPage)
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/comments.js
Expand Up @@ -2,6 +2,8 @@
/**
* Module dependencies.
*/
"use strict";


var mongoose = require('mongoose');
var utils = require('../../lib/utils');
Expand Down Expand Up @@ -33,7 +35,7 @@ exports.create = function (req, res) {
if (err) return res.render('500');
res.redirect('/articles/'+ article.id);
});
}
};

/**
* Delete comment
Expand Down
37 changes: 37 additions & 0 deletions app/controllers/home.js
@@ -0,0 +1,37 @@

/**
* Module dependencies.
*/
"use strict";

var mongoose = require('mongoose');
var Article = mongoose.model('Article');
var Table = mongoose.model('Table');
var utils = require('../../lib/utils');
var extend = require('util')._extend;

exports.index = function (req, res){
var tableOptions = {
perPage: 8,
criteria: {
isPrivate: false
}
};

Table.list(tableOptions, function (err, tables) {
if (err) return res.render('500');
Table.count().exec(function (err, count) {
Article.list({}, function (err, articles) {
if (err) return res.render('500');
Article.count().exec(function (err, count) {
res.render('home/index', {
title: 'Tablemania',
articles: articles,
tables: tables
});
});
});
});
});

};
103 changes: 103 additions & 0 deletions app/controllers/tables.js
@@ -0,0 +1,103 @@
/**
* Module dependencies.
*/
"use strict";

var mongoose = require('mongoose');
var Table = mongoose.model('Table');
var utils = require('../../lib/utils');


/**
* Load
*/

exports.load = function (req, res, next, id){
Table.load(id, function (err, table) {
if (err) return next(err);
if (!table) return next(new Error('not found'));
req.table = table;
next();
});
};

/**
* List
*/

exports.index = function (req, res){
var page = (req.param('page') > 0 ? req.param('page') : 1) - 1;
var perPage = 30;
var options = {
perPage: perPage,
page: page
};
options.criteria = res.locals.isAdmin ? {} : { isPrivate: false};

// res.render('game/index', {layout: false, title: 'game room'});

Table.list(options, function (err, tables) {
if (err) return res.render('500');
Table.count().exec(function (err, count) {
res.render('tables/index', {
title: 'Public Tables',
tables: tables,
page: page + 1,
pages: Math.ceil(count / perPage)
});
});
});


};


/**
* New table
*/

exports.new = function (req, res){
res.render('tables/new', {
title: 'New Table',
table: new Table({})
});
};



/**
* Create a table
*/

exports.create = function (req, res) {
console.log('create table', req.body);
var table = new Table(req.body);

table.save(function (err) {
if (!err) {
req.flash('success', 'Successfully created table!');
return res.redirect('/tables/'+table._id);
}
console.log(err);
res.render('tables/new', {
title: 'New Table',
table: table,
errors: utils.errors(err.errors || err)
});
});
};


/**
* Show
*/

exports.show = function (req, res){
var table = req.table;

console.log('tables show');
res.render('tables/show', {
title: 'Table Lobby',
table: table
});
};
42 changes: 41 additions & 1 deletion app/controllers/users.js
Expand Up @@ -6,6 +6,8 @@
var mongoose = require('mongoose');
var User = mongoose.model('User');
var utils = require('../../lib/utils');
var extend = require('util')._extend;


/**
* Load
Expand Down Expand Up @@ -54,11 +56,49 @@ exports.create = function (req, res) {
exports.show = function (req, res) {
var user = req.profile;
res.render('users/show', {
title: user.name,
title: 'User: ' + user.username,
user: user
});
};


/**
* Edit user
*/

exports.edit = function (req, res) {
res.render('users/edit', {
title: 'Edit ' + req.user.username,
user: req.user
});
};


/**
* Update user
*/

exports.update = function (req, res){
var user = req.user;

// make sure no one changes the user
delete req.body.user;
user = extend(user, req.body);

user.save(function (err) {
if (!err) {
return res.redirect('/users/' + user._id);
}

res.render('users/edit', {
title: 'Edit ' + user.username,
user: user,
errors: utils.errors(err.errors || err)
});
});
};


exports.signin = function (req, res) {};

/**
Expand Down
95 changes: 95 additions & 0 deletions app/models/table.js
@@ -0,0 +1,95 @@

/**
* Module dependencies.
*/

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

/**
* Getters
*/

var getTags = function (tags) {
return tags.join(',');
};

/**
* Setters
*/

var setTags = function (tags) {
return tags.split(',');
};

/**
* Table Schema
*/

var TableSchema = new Schema({
title: {type : String, default : '', trim : true},
isPrivate: {type : Boolean, default : false},
users: [{
user: {type : Schema.ObjectId, ref : 'User'}
}],
tags: {type: [], get: getTags, set: setTags},
createdAt : {type : Date, default : Date.now}
});

/**
* Validations
*/

TableSchema.path('title').required(true, 'Table title cannot be blank');



/**
* Methods
*/

TableSchema.methods = {


};

/**
* Statics
*/

TableSchema.statics = {

/**
* Find table by id
*
* @param {ObjectId} id
* @param {Function} cb
* @api private
*/

load: function (id, cb) {
this.findOne({ _id : id })
.exec(cb);
},

/**
* List tables
*
* @param {Object} options
* @param {Function} cb
* @api private
*/

list: function (options, cb) {
var criteria = options.criteria || {};

this.find(criteria)
.populate('user', 'name username')
.sort({'createdAt': -1}) // sort by date
.limit(options.perPage)
.skip(options.perPage * options.page)
.exec(cb);
}
};

mongoose.model('Table', TableSchema);

0 comments on commit 512e1a9

Please sign in to comment.