This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

added comments to fortunes

  • Loading branch information...
n1k0 committed Feb 10, 2012
1 parent aecc08a commit bee16eec0dd31a4523370b093e3fb9c7ddfc08e4
View
@@ -1,4 +1,4 @@
-# Adapted from https://github.com/twilson63/cakefile-template, thanks!
+# Adapted from https://github.com/twilson63/cakefile-template
mongoose = require('mongoose')
{print} = require('util')
@@ -80,7 +80,9 @@ exit = (status) ->
info = (message) -> log statuses.info, ansi.blue, message
load = (callback) ->
- Fortune.remove ->
+ Fortune.remove (err) ->
+ if err
+ return callback?(err)
processed = 0
info "loading fixtures…"
for ref, fortune of fortunes
@@ -92,7 +94,7 @@ load = (callback) ->
ko "- #{error}: #{err.errors[error].type}"
if ++processed == Object.keys(fortunes).length
ok "processed #{processed} fixtures."
- callback?(err, fortune)
+ return callback?(err, fortune)
log = (message, color, explanation) ->
color = ansi[color] || color || ''
@@ -136,7 +138,7 @@ task 'build', 'Build current project', ->
setup -> build (status) -> exit(1 if status)
task 'casper', 'Launches casperjs test suite', ->
- setup 'test', -> build -> load -> casper (status) -> exit(1 if status)
+ setup 'test', -> build -> connect -> load -> casper (status) -> exit(1 if status)
task 'docs', 'Generate annotated source code with Docco', ->
files = utils.findFiles "src",
@@ -148,7 +150,7 @@ task 'funk', 'Fantastic stuff', ->
setup 'test', -> build -> connect -> load -> test -> load -> casper (status) -> exit(1 if status)
task 'load', 'Load test fixtures', ->
- setup -> load (err) -> exit(1 if err)
+ setup -> connect -> load (err) -> exit(1 if err)
task 'test', 'Run test suite', ->
setup 'test', -> build -> connect -> load -> test (status) -> exit(1 if status)
View
@@ -3,17 +3,17 @@
module.exports = {
production: {
mongo: {
- uri: "mongodb://localhost/nodetunes"
+ uri: "mongodb://127.0.0.1/nodetunes"
}
},
development: {
mongo: {
- uri: "mongodb://localhost/nodetunes-dev"
+ uri: "mongodb://127.0.0.1/nodetunes-dev"
}
},
test: {
mongo: {
- uri: "mongodb://localhost/nodetunes-test"
+ uri: "mongodb://127.0.0.1/nodetunes-test"
}
}
};
View
@@ -4,7 +4,18 @@
f1: {
title: "First fortune",
content: "<niko> Hello!\n<dave> Hello.",
- votes: 10
+ votes: 10,
+ comments: [
+ {
+ author: "John",
+ email: "john@doe.com",
+ content: "Yes?"
+ }, {
+ author: "Bob",
+ email: "bob@doe.com",
+ content: "Yes."
+ }
+ ]
},
f2: {
title: "Second fortune",
View
@@ -132,21 +132,23 @@
});
});
- app.get("/", routes.index);
+ app.get("/", routes.fortune.index);
- app.get("/worst", routes.worst);
+ app.get("/worst", routes.fortune.worst);
- app.get("/new", routes.add);
+ app.get("/new", routes.fortune.add);
- app.post("/new", routes.add);
+ app.post("/new", routes.fortune.add);
- app.get("/fortune/:fortuneSlug", routes.show);
+ app.get("/fortune/:fortuneSlug", routes.fortune.show);
- app.get("/top", routes.top);
+ app.post("/fortune/:fortuneSlug", routes.fortune.show);
- app.get("/fortune/:fortuneSlug/down", routes.down);
+ app.get("/top", routes.fortune.top);
- app.get("/fortune/:fortuneSlug/up", routes.up);
+ app.get("/fortune/:fortuneSlug/down", routes.fortune.down);
+
+ app.get("/fortune/:fortuneSlug/up", routes.fortune.up);
app.get("*", function(req, res) {
return res.render('404', {
View
@@ -3,13 +3,13 @@
Form = (function() {
- function Form(model, data) {
- this.model = model;
+ function Form(options) {
+ this.model = options.model;
this.bound = false;
this.valid = void 0;
this.values = {};
this.errors = {};
- if (data) this.bind(data);
+ if (options.data) this.bind(options.data);
}
Form.prototype.bind = function(params) {
View
@@ -1,10 +1,57 @@
(function() {
- var Fortune, mongoose, slugify;
+ var Comment, Fortune, mongoose, slugify;
mongoose = require("mongoose");
slugify = require("../lib/helpers").slugify;
+ /* Comment model
+ */
+
+ Comment = new mongoose.Schema({
+ author: {
+ type: String,
+ trim: true,
+ required: true,
+ validate: [
+ function(v) {
+ return v.length >= 3 && v.length <= 50;
+ }, "Author name length must be comprised between 3 and 50 chars"
+ ]
+ },
+ email: {
+ type: String,
+ trim: true,
+ required: true
+ },
+ content: {
+ type: String,
+ trim: true,
+ required: true,
+ validate: [
+ function(v) {
+ return v.length < 500;
+ }, "Contents length must be less than 500 chars"
+ ]
+ },
+ date: {
+ type: Date,
+ "default": Date.now,
+ index: true
+ }
+ });
+
+ Comment.path('email').validate(function(v) {
+ return v.length >= 3 && v.length <= 255;
+ }, "length");
+
+ Comment.path('email').validate(function(v) {
+ return /^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(v);
+ }, "regexp");
+
+ /* Fortune model
+ */
+
Fortune = new mongoose.Schema({
title: {
type: String,
@@ -49,7 +96,8 @@
type: Date,
"default": Date.now,
index: true
- }
+ },
+ comments: [Comment]
});
Fortune.pre('validate', function(next) {
View
@@ -0,0 +1,7 @@
+(function() {
+
+ exports["new"] = function(req, res) {
+ return "plop";
+ };
+
+}).call(this);
View
@@ -0,0 +1,124 @@
+(function() {
+ var Form, Fortune, NotFound;
+
+ Fortune = require("../models/Fortune");
+
+ Form = require("../lib/form").Form;
+
+ NotFound = require("../lib/errors").NotFound;
+
+ exports.add = function(req, res) {
+ var form, stdRes;
+ form = new Form(Fortune);
+ stdRes = function(form) {
+ return res.render("add", {
+ form: form,
+ title: "Add a fortune"
+ });
+ };
+ if (req.method !== "POST") return stdRes(form);
+ form.bind(req.body.fortune);
+ return form.save(function(err) {
+ if (err) {
+ req.flash("warning", "Unable to save fortune");
+ return stdRes(form);
+ } else {
+ req.flash("info", "Fortune added");
+ return res.redirect("/");
+ }
+ });
+ };
+
+ exports.down = function(req, res, next) {
+ return req.fortune.voteDown(function(err) {
+ if (err) return next(err);
+ req.flash("info", "Fortune voted down");
+ return res.redirect("/fortune/" + req.fortune.slug);
+ });
+ };
+
+ exports.index = function(req, res, next) {
+ return Fortune.findLatest({
+ limit: 10
+ }, function(err, fortunes) {
+ if (err) return next(err);
+ return res.render("index", {
+ title: "Home",
+ fortunes: fortunes
+ });
+ });
+ };
+
+ exports.top = function(req, res, next) {
+ return Fortune.findTop({
+ limit: 10
+ }, function(err, fortunes) {
+ if (err) return next(err);
+ return res.render("index", {
+ title: "Top 10",
+ fortunes: fortunes
+ });
+ });
+ };
+
+ exports.show = function(req, res, next) {
+ var stdRes;
+ stdRes = function(fortune, form) {
+ if (form == null) {
+ form = {
+ errors: {},
+ values: {}
+ };
+ }
+ console.log(form);
+ return res.render("show", {
+ fortune: fortune,
+ form: form,
+ title: req.fortune.title
+ });
+ };
+ if (req.method !== "POST") return stdRes(req.fortune);
+ req.fortune.comments.push(req.body.comment);
+ return req.fortune.save(function(err) {
+ var error, errors, field, _ref;
+ if (err) {
+ errors = {};
+ _ref = err.errors;
+ for (field in _ref) {
+ error = _ref[field];
+ errors[field] = err.errors[field].type;
+ }
+ req.flash("warning", "Unable to save comment");
+ req.fortune.comments = req.fortune.comments.slice(0, req.fortune.comments.length - 1);
+ return stdRes(req.fortune, {
+ errors: errors,
+ values: req.body.comment
+ });
+ } else {
+ req.flash("info", "Comment added");
+ return res.redirect("/");
+ }
+ });
+ };
+
+ exports.up = function(req, res, next) {
+ return req.fortune.voteUp(function(err) {
+ if (err) return next(err);
+ req.flash("info", "Fortune voted up");
+ return res.redirect("/fortune/" + req.fortune.slug);
+ });
+ };
+
+ exports.worst = function(req, res, next) {
+ return Fortune.findWorst({
+ limit: 10
+ }, function(err, fortunes) {
+ if (err) return next(err);
+ return res.render("index", {
+ title: "Flop 10",
+ fortunes: fortunes
+ });
+ });
+ };
+
+}).call(this);
Oops, something went wrong.

0 comments on commit bee16ee

Please sign in to comment.