Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

hook wods up to mongo

  • Loading branch information...
commit 57122bfca54b68fd488ed61db5e5316472491810 1 parent 15dbd71
Mike Munroe authored
Showing with 29 additions and 5 deletions.
  1. +16 −3 routes/wods.js
  2. +13 −2 server.js
19 routes/wods.js
View
@@ -1,7 +1,20 @@
+'use strict';
+
+var mongoose = require('mongoose'),
+ Wod = mongoose.model('Wod');
+
exports.findAll = function(req, res) {
- res.send([{name:'wod1'}, {name:'wod2'}, {name:'wod3'}]);
+ Wod.find().exec(function(err, lifts) {
+ if (err) return res.json(500, err);
+ res.json(lifts);
+ });
};
-exports.findById = function(req, res) {
- res.send({id:req.params.id, name: "Name", description: "description"});
+exports.findById = function(req, res, next, id) {
+ Wod.findById(id, function(err, lift) {
+ if (err) return next(err);
+ if (!lift) return next(new Error('Failed to load wod ' + id));
+ req.lift = lift;
+ next();
+ });
};
15 server.js
View
@@ -1,6 +1,16 @@
var express = require('express'),
- mongoose = require('mongoose'),
- wods = require('./routes/wods');
+ path = require('path'),
+ fs = require('fs'),
+ mongoose = require('mongoose');
+
+
+// Bootstrap models
+var modelsPath = path.join(__dirname, 'models');
+fs.readdirSync(modelsPath).forEach(function (file) {
+ if (/(.*)\.(js$|coffee$)/.test(file)) {
+ require(modelsPath + '/' + file);
+ }
+});
mongoose.connect('mongodb://localhost/dailywod');
var db = mongoose.connection;
@@ -10,6 +20,7 @@ db.once('open', function callback () {
var app = express();
+var wods = require('./routes/wods');
app.get('/crossfitwicked/wods', wods.findAll);
app.get('/crossfitwicked/wods/:id', wods.findById);
Please sign in to comment.
Something went wrong with that request. Please try again.