Permalink
Browse files

crud for lifts

  • Loading branch information...
1 parent 4f963e3 commit 0ce5334690cae18d777bec00a73a807ea0eba87d @mikepmunroe committed Feb 26, 2014
Showing with 51 additions and 8 deletions.
  1. +44 −7 lib/controllers/lifts.js
  2. +7 −1 lib/routes.js
View
@@ -3,12 +3,49 @@
var mongoose = require('mongoose'),
Lift = mongoose.model('Lift');
-exports.index = function(req, res) {
- return Lift.find(function (err, things) {
- if (!err) {
- return res.json(things);
- } else {
- return res.send(err);
- }
+exports.lift = function(req, res, next, id) {
+ Lift.findById(id, function(err, lift) {
+ if (err) return next(err);
+ if (!lift) return next(new Error('Failed to load lift ' + id));
+ req.lift = lift;
+ next();
+ });
+};
+
+exports.query = function(req, res) {
+ Lift.find().sort('date').exec(function(err, lifts) {
+ if (err) return res.json(500, err);
+ res.json(lifts);
+ });
+};
+
+exports.show = function(req, res) {
+ res.json(req.lift);
+};
+
+exports.create = function(req, res) {
+ var lift = new Lift(req.body);
+
+ lift.save(function(err) {
+ if (err) return res.json(500, err);
+ res.json(lift);
+ });
+};
+
+exports.update = function(req, res) {
+ var updatedLift = _.extend(req.lift, req.body);
+
+ updatedLift.save(function(err) {
+ if (err) return res.json(500, err);
+ res.json(updatedLift);
+ });
+};
+
+exports.remove = function(req, res) {
+ var lift = req.lift;
+
+ lift.remove(function(err) {
+ if (err) return res.json(500, err);
+ res.json(lift);
});
};
View
@@ -17,7 +17,13 @@ module.exports = function(app) {
app.get('/api/users/me', users.me);
app.get('/api/users/:id', users.show);
- app.get('/api/lifts', lifts.index);
+ app.param('liftId', lifts.lift);
+
+ app.post('/api/lifts', lifts.create);
+ app.get('/api/lifts', lifts.query);
+ app.get('/api/lifts/:liftId', lifts.show);
+ app.put('/api/lifts/:liftId', lifts.update);
+ app.del('/api/lifts/:liftId', lifts.remove);
app.post('/api/session', session.login);
app.del('/api/session', session.logout);

0 comments on commit 0ce5334

Please sign in to comment.