'use strict';

var express = require('express');
var router = express.Router();
var methodOverride = require('method-override');

var Feed = require('../models/feed.js')

// router middleware
router.use(methodOverride(function(req, res) {
  // check for _method property on form submit
  if (req.body && typeof req.body === 'object' && '_method' in req.body) {
    var method = req.body._method;
    delete req.body._method;
    return method;
  }
}));

// base page
router.get('/', function(req, res) {
  res.render('/index', { title: 'Feed Reader'})
});


// create
router.post('/new', function(req, res) {
  var feed = new Feed({
    name: req.body.name,
    source: req.body.source
  });

  feed.save(function(err) {
    if (err) {
      return res.send(err);
    }

    res.redirect('/feeds');
  });
});

// update
router.post('/:id/edit', function(req, res) {
  Feed.findById(req.params.id, function(err, feed) {
    if (err) {
      return res.send(err);
    }

    for (var key in req.body) {
      feed[key] = req.body[key];
    }

    feed.save(function(err) {
      if (err) {
        return res.send(err)
      }

      res.redirect('feeds/' + feed._id);
    });
  });
});

// delete
router.delete('/:id', function(req, res) {
  Feed.remove({ _id: req.params.id }, function(err, feed) {
    if (err) {
      return res.send(err);
    }

    res.redirect('/feeds');
  });
});

@@ -450,938 +450,4 @@
 ));%0A
-%0A// base page%0Arouter.get('/', function(req, res) %7B%0A  res.render('/index', %7B title: 'Feed Reader'%7D)%0A%7D);%0A%0A%0A// create%0Arouter.post('/new', function(req, res) %7B%0A  var feed = new Feed(%7B%0A    name: req.body.name,%0A    source: req.body.source%0A  %7D);%0A%0A  feed.save(function(err) %7B%0A    if (err) %7B%0A      return res.send(err);%0A    %7D%0A%0A    res.redirect('/feeds');%0A  %7D);%0A%7D);%0A%0A// update%0Arouter.post('/:id/edit', function(req, res) %7B%0A  Feed.findById(req.params.id, function(err, feed) %7B%0A    if (err) %7B%0A      return res.send(err);%0A    %7D%0A%0A    for (var key in req.body) %7B%0A      feed%5Bkey%5D = req.body%5Bkey%5D;%0A    %7D%0A%0A    feed.save(function(err) %7B%0A      if (err) %7B%0A        return res.send(err)%0A      %7D%0A%0A      res.redirect('feeds/' + feed._id);%0A    %7D);%0A  %7D);%0A%7D);%0A%0A// delete%0Arouter.delete('/:id', function(req, res) %7B%0A  Feed.remove(%7B _id: req.params.id %7D, function(err, feed) %7B%0A    if (err) %7B%0A      return res.send(err);%0A    %7D%0A%0A    res.redirect('/feeds');%0A  %7D);%0A%7D);%0A

remove routes to rebuild them